Пример #1
0
        public void TestAlmostIntersectingRulingsShouldIntersect()
        {
            Ruling v = new Ruling(new PdfPoint(555.960876f, 271.569641f), new PdfPoint(555.960876f, 786.899902f));
            Ruling h = new Ruling(new PdfPoint(25.620499f, 786.899902f), new PdfPoint(555.960754f, 786.899902f));
            SortedDictionary <PdfPoint, Ruling[]> m = Ruling.FindIntersections(new Ruling[] { h }.ToList(), new Ruling[] { v }.ToList());

            Assert.Single(m.Values);
        }
Пример #2
0
        /// <summary>
        /// Find cells from horizontal and vertical ruling lines.
        /// </summary>
        /// <param name="horizontalRulingLines"></param>
        /// <param name="verticalRulingLines"></param>
        public static List <Cell> FindCells(IReadOnlyList <Ruling> horizontalRulingLines, IReadOnlyList <Ruling> verticalRulingLines)
        {
            List <Cell> cellsFound = new List <Cell>();
            SortedDictionary <PdfPoint, Ruling[]> intersectionPoints = Ruling.FindIntersections(horizontalRulingLines, verticalRulingLines);
            List <PdfPoint> intersectionPointsList = new List <PdfPoint>(intersectionPoints.Keys);

            intersectionPointsList.Sort(new POINT_COMPARER());

            for (int i = 0; i < intersectionPointsList.Count; i++)
            {
                PdfPoint topLeft = intersectionPointsList[i];
                Ruling[] hv      = intersectionPoints[topLeft];
                bool     doBreak = false;

                // CrossingPointsDirectlyBelow( topLeft );
                List <PdfPoint> xPoints = new List <PdfPoint>();
                // CrossingPointsDirectlyToTheRight( topLeft );
                List <PdfPoint> yPoints = new List <PdfPoint>();

                foreach (PdfPoint p in intersectionPointsList.SubList(i, intersectionPointsList.Count))
                {
                    if (p.X == topLeft.X && p.Y < topLeft.Y) //  p.Y > topLeft.Y
                    {
                        xPoints.Add(p);
                    }
                    if (p.Y == topLeft.Y && p.X > topLeft.X)
                    {
                        yPoints.Add(p);
                    }
                }

                //outer:
                foreach (PdfPoint xPoint in xPoints)
                {
                    if (doBreak)
                    {
                        break;
                    }

                    // is there a vertical edge b/w topLeft and xPoint?
                    if (!hv[1].Equals(intersectionPoints[xPoint][1]))
                    {
                        continue;
                    }

                    foreach (PdfPoint yPoint in yPoints)
                    {
                        // is there an horizontal edge b/w topLeft and yPoint ?
                        if (!hv[0].Equals(intersectionPoints[yPoint][0]))
                        {
                            continue;
                        }

                        PdfPoint btmRight = new PdfPoint(yPoint.X, xPoint.Y);
                        if (intersectionPoints.ContainsKey(btmRight) &&
                            intersectionPoints[btmRight][0].Equals(intersectionPoints[xPoint][0]) &&
                            intersectionPoints[btmRight][1].Equals(intersectionPoints[yPoint][1]))
                        {
                            cellsFound.Add(new Cell(topLeft, btmRight));
                            doBreak = true;
                            //break outer;
                            break;
                        }
                    }
                }
            }

            // TODO create cells for vertical ruling lines with aligned endpoints at the top/bottom of a grid
            // that aren't connected with an horizontal ruler?
            // see: https://github.com/jazzido/tabula-extractor/issues/78#issuecomment-41481207

            return(cellsFound);
        }