Ejemplo n.º 1
0
        /// <summary>
        /// Extracts text fragments from the 3rd page and highlights the glyphs in the fragment.
        /// </summary>
        /// <param name="document"></param>
        private static void ExtractImagesAndHighlight(PDFFixedDocument document)
        {
            PDFPen                     pen       = new PDFPen(new PDFRgbColor(255, 0, 192), 0.5);
            PDFBrush                   brush     = new PDFBrush(new PDFRgbColor(0, 0, 0));
            PDFStandardFont            helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 8);
            PDFStringAppearanceOptions sao       = new PDFStringAppearanceOptions();

            sao.Brush = brush;
            sao.Font  = helvetica;
            PDFStringLayoutOptions slo = new PDFStringLayoutOptions();

            slo.Width = 1000;

            PDFContentExtractor      ce  = new PDFContentExtractor(document.Pages[2]);
            PDFVisualImageCollection eic = ce.ExtractImages(false);

            for (int i = 0; i < eic.Count; i++)
            {
                string imageProperties = string.Format("Image ID: {0}\nPixel width: {1} pixels\nPixel height: {2} pixels\n" +
                                                       "Display width: {3} points\nDisplay height: {4} points\nHorizonal Resolution: {5} dpi\nVertical Resolution: {6} dpi",
                                                       eic[i].ImageID, eic[i].Width, eic[i].Height, eic[i].DisplayWidth, eic[i].DisplayHeight, eic[i].DpiX, eic[i].DpiY);

                PDFPath boundingPath = new PDFPath();
                boundingPath.StartSubpath(eic[i].ImageCorners[0].X, eic[i].ImageCorners[0].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[1].X, eic[i].ImageCorners[1].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[2].X, eic[i].ImageCorners[2].Y);
                boundingPath.AddLineTo(eic[i].ImageCorners[3].X, eic[i].ImageCorners[3].Y);
                boundingPath.CloseSubpath();

                document.Pages[2].Canvas.DrawPath(pen, boundingPath);
                slo.X = eic[i].ImageCorners[3].X + 1;
                slo.Y = eic[i].ImageCorners[3].Y + 1;
                document.Pages[2].Canvas.DrawString(imageProperties, sao, slo);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Extracts text fragments from the 2nd page and highlights the glyphs in the fragment.
        /// </summary>
        /// <param name="document"></param>
        private static void ExtractTextAndHighlightGlyphs(PDFFixedDocument document)
        {
            PDFRgbColor penColor = new PDFRgbColor();
            PDFPen      pen      = new PDFPen(penColor, 0.5);
            Random      rnd      = new Random();

            byte[] rgb = new byte[3];

            PDFContentExtractor  ce  = new PDFContentExtractor(document.Pages[1]);
            PDFTextRunCollection trc = ce.ExtractTextRuns();
            PDFTextRun           tr  = trc[1];

            for (int i = 0; i < tr.Glyphs.Count; i++)
            {
                rnd.NextBytes(rgb);
                penColor.R = rgb[0];
                penColor.G = rgb[1];
                penColor.B = rgb[2];

                PDFPath boundingPath = new PDFPath();
                boundingPath.StartSubpath(tr.Glyphs[i].GlyphCorners[0].X, tr.Glyphs[i].GlyphCorners[0].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[1].X, tr.Glyphs[i].GlyphCorners[1].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[2].X, tr.Glyphs[i].GlyphCorners[2].Y);
                boundingPath.AddLineTo(tr.Glyphs[i].GlyphCorners[3].X, tr.Glyphs[i].GlyphCorners[3].Y);
                boundingPath.CloseSubpath();

                document.Pages[1].Canvas.DrawPath(pen, boundingPath);
            }
        }
Ejemplo n.º 3
0
        private static void HighlightSearchResults(PDFPage page, PDFTextSearchResultCollection searchResults, PDFColor color)
        {
            PDFPen pen = new PDFPen(color, 0.5);

            for (int i = 0; i < searchResults.Count; i++)
            {
                PDFTextFragmentCollection tfc = searchResults[i].TextFragments;
                for (int j = 0; j < tfc.Count; j++)
                {
                    PDFPath path = new PDFPath();

                    path.StartSubpath(tfc[j].FragmentCorners[0].X, tfc[j].FragmentCorners[0].Y);
                    path.AddPolygon(tfc[j].FragmentCorners);

                    page.Canvas.DrawPath(pen, path);
                }
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Draws the Bezier connected lines on the page.
        /// </summary>
        /// <param name="page">Page where to draw the lines.</param>
        /// <param name="points">List of points representing the connected lines.</param>
        /// <param name="pen">Pen to draw the final path.</param>
        /// <param name="smoothFactor">Smooth factor for computing the Bezier curve</param>
        /// <param name="font"></param>
        private static void DrawBezierConnectedLines(PDFPage page, PDFPoint[] points, PDFPen pen, double smoothFactor, PDFFont font)
        {
            PDFPath path = new PDFPath();

            path.StartSubpath(points[0].X, points[0].Y);

            for (int i = 0; i < points.Length - 2; i++)
            {
                PDFPoint[] pts = ComputeBezierConnectedLines(points[i], points[i + 1], points[i + 2], smoothFactor, i == 0, i == points.Length - 3);
                switch (pts.Length)
                {
                case 2:     // Intermediate/last section - straight lines
                    path.AddLineTo(pts[0].X, pts[0].Y);
                    path.AddLineTo(pts[1].X, pts[1].Y);
                    break;

                case 3:     // First section - straight lines
                    path.AddLineTo(pts[0].X, pts[0].Y);
                    path.AddLineTo(pts[1].X, pts[1].Y);
                    path.AddLineTo(pts[2].X, pts[2].Y);
                    break;

                case 4:     // Intermediate/last section
                    path.AddLineTo(pts[0].X, pts[0].Y);
                    path.AddBezierTo(pts[1].X, pts[1].Y, pts[1].X, pts[1].Y, pts[2].X, pts[2].Y);
                    path.AddLineTo(pts[3].X, pts[3].Y);
                    break;

                case 5:     // First section
                    path.AddLineTo(pts[0].X, pts[0].Y);
                    path.AddLineTo(pts[1].X, pts[1].Y);
                    path.AddBezierTo(pts[2].X, pts[2].Y, pts[2].X, pts[2].Y, pts[3].X, pts[3].Y);
                    path.AddLineTo(pts[4].X, pts[4].Y);
                    break;
                }
            }

            page.Canvas.DrawPath(pen, path);

            page.Canvas.DrawString($"Smooth factor = {smoothFactor}", font, new PDFBrush(), points[points.Length - 1].X, points[0].Y);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream input)
        {
            PDFBrush        brush     = new PDFBrush();
            PDFPen          redPen    = new PDFPen(PDFRgbColor.Red, 1);
            PDFStandardFont helvetica = new PDFStandardFont(PDFStandardFontFace.Helvetica, 10);

            PDFFixedDocument document = new PDFFixedDocument(input);

            PDFContentExtractor       ce  = new PDFContentExtractor(document.Pages[0]);
            PDFVisualObjectCollection voc = ce.ExtractVisualObjects(false);

            PDFPath contour = null;

            for (int i = 0; i < voc.Count; i++)
            {
                switch (voc[i].Type)
                {
                case PDFVisualObjectType.Image:
                    PDFImageVisualObject ivo = voc[i] as PDFImageVisualObject;
                    contour = new PDFPath();
                    contour.StartSubpath(ivo.Image.ImageCorners[0].X - 5, ivo.Image.ImageCorners[0].Y + 5);
                    contour.AddLineTo(ivo.Image.ImageCorners[1].X + 5, ivo.Image.ImageCorners[1].Y + 5);
                    contour.AddLineTo(ivo.Image.ImageCorners[2].X + 5, ivo.Image.ImageCorners[2].Y - 5);
                    contour.AddLineTo(ivo.Image.ImageCorners[3].X - 5, ivo.Image.ImageCorners[3].Y - 5);
                    contour.CloseSubpath();
                    document.Pages[0].Canvas.DrawPath(redPen, contour);

                    document.Pages[0].Canvas.DrawString("Image", helvetica, brush,
                                                        ivo.Image.ImageCorners[0].X - 5, ivo.Image.ImageCorners[0].Y + 5);
                    break;

                case PDFVisualObjectType.Text:
                    PDFTextVisualObject tvo = voc[i] as PDFTextVisualObject;
                    contour = new PDFPath();
                    contour.StartSubpath(tvo.TextFragment.FragmentCorners[0].X - 5, tvo.TextFragment.FragmentCorners[0].Y + 5);
                    contour.AddLineTo(tvo.TextFragment.FragmentCorners[1].X + 5, tvo.TextFragment.FragmentCorners[1].Y + 5);
                    contour.AddLineTo(tvo.TextFragment.FragmentCorners[2].X + 5, tvo.TextFragment.FragmentCorners[2].Y - 5);
                    contour.AddLineTo(tvo.TextFragment.FragmentCorners[3].X - 5, tvo.TextFragment.FragmentCorners[3].Y - 5);
                    contour.CloseSubpath();
                    document.Pages[0].Canvas.DrawPath(redPen, contour);

                    document.Pages[0].Canvas.DrawString("Text", helvetica, brush,
                                                        tvo.TextFragment.FragmentCorners[0].X - 5, tvo.TextFragment.FragmentCorners[0].Y + 5);
                    break;

                case PDFVisualObjectType.Path:
                    PDFPathVisualObject pvo = voc[i] as PDFPathVisualObject;
                    // Examine all the path points and determine the minimum rectangle that bounds the path.
                    double minX = 999999, minY = 999999, maxX = -999999, maxY = -999999;
                    for (int j = 0; j < pvo.PathItems.Count; j++)
                    {
                        PDFPathItem pi = pvo.PathItems[j];
                        if (pi.Points != null)
                        {
                            for (int k = 0; k < pi.Points.Length; k++)
                            {
                                if (minX >= pi.Points[k].X)
                                {
                                    minX = pi.Points[k].X;
                                }
                                if (minY >= pi.Points[k].Y)
                                {
                                    minY = pi.Points[k].Y;
                                }
                                if (maxX <= pi.Points[k].X)
                                {
                                    maxX = pi.Points[k].X;
                                }
                                if (maxY <= pi.Points[k].Y)
                                {
                                    maxY = pi.Points[k].Y;
                                }
                            }
                        }
                    }

                    contour = new PDFPath();
                    contour.StartSubpath(minX - 5, minY - 5);
                    contour.AddLineTo(maxX + 5, minY - 5);
                    contour.AddLineTo(maxX + 5, maxY + 5);
                    contour.AddLineTo(minX - 5, maxY + 5);
                    contour.CloseSubpath();
                    document.Pages[0].Canvas.DrawPath(redPen, contour);

                    document.Pages[0].Canvas.DrawString("Path", helvetica, brush, minX - 5, maxY + 5);
                    // Skip the rest of path objects, they are the evaluation message
                    i = voc.Count;
                    break;
                }
            }

            SampleOutputInfo[] output = new SampleOutputInfo[] { new SampleOutputInfo(document, "pageobjects.pdf") };
            return(output);
        }
Ejemplo n.º 6
0
        private static void DrawPatterns(PDFPage page, PDFFont titleFont, PDFFont sectionFont)
        {
            PDFBrush brush    = new PDFBrush();
            PDFPen   blackPen = new PDFPen(PDFRgbColor.Black, 1);

            PDFPen   darkRedPen       = new PDFPen(new PDFRgbColor(0xFF, 0x40, 0x40), 0.8);
            PDFPen   darkOrangePen    = new PDFPen(new PDFRgbColor(0xA6, 0x4B, 0x00), 0.8);
            PDFPen   darkCyanPen      = new PDFPen(new PDFRgbColor(0x00, 0x63, 0x63), 0.8);
            PDFPen   darkGreenPen     = new PDFPen(new PDFRgbColor(0x00, 0x85, 0x00), 0.8);
            PDFBrush lightRedBrush    = new PDFBrush(new PDFRgbColor(0xFF, 0x73, 0x73));
            PDFBrush lightOrangeBrush = new PDFBrush(new PDFRgbColor(0xFF, 0x96, 0x40));
            PDFBrush lightCyanBrush   = new PDFBrush(new PDFRgbColor(0x33, 0xCC, 0xCC));
            PDFBrush lightGreenBrush  = new PDFBrush(new PDFRgbColor(0x67, 0xE6, 0x67));

            page.Canvas.DrawString("Patterns", titleFont, brush, 20, 50);

            page.Canvas.DrawString("Colored patterns", sectionFont, brush, 25, 70);

            // Create the pattern visual appearance.
            PDFColoredTilingPattern ctp = new PDFColoredTilingPattern(20, 20);

            // Red circle
            ctp.Canvas.DrawEllipse(darkRedPen, lightRedBrush, 1, 1, 8, 8);
            // Cyan square
            ctp.Canvas.DrawRectangle(darkCyanPen, lightCyanBrush, 11, 1, 8, 8);
            // Green diamond
            PDFPath diamondPath = new PDFPath();

            diamondPath.StartSubpath(1, 15);
            diamondPath.AddPolyLineTo(new PDFPoint[] { new PDFPoint(5, 11), new PDFPoint(9, 15), new PDFPoint(5, 19) });
            diamondPath.CloseSubpath();
            ctp.Canvas.DrawPath(darkGreenPen, lightGreenBrush, diamondPath);
            // Orange triangle
            PDFPath trianglePath = new PDFPath();

            trianglePath.StartSubpath(11, 19);
            trianglePath.AddPolyLineTo(new PDFPoint[] { new PDFPoint(15, 11), new PDFPoint(19, 19) });
            trianglePath.CloseSubpath();
            ctp.Canvas.DrawPath(darkOrangePen, lightOrangeBrush, trianglePath);

            // Create a pattern colorspace from the pattern object.
            PDFPatternColorSpace coloredPatternColorSpace = new PDFPatternColorSpace(ctp);
            // Create a color based on the pattern colorspace.
            PDFPatternColor coloredPatternColor = new PDFPatternColor(coloredPatternColorSpace);
            // The pen and brush use the pattern color like any other color.
            PDFPatternBrush patternBrush = new PDFPatternBrush(coloredPatternColor);
            PDFPatternPen   patternPen   = new PDFPatternPen(coloredPatternColor, 40);

            page.Canvas.DrawEllipse(patternBrush, 25, 90, 250, 200);
            page.Canvas.DrawRoundRectangle(patternPen, 310, 110, 250, 160, 100, 100);

            page.Canvas.DrawString("Uncolored patterns", sectionFont, brush, 25, 300);

            // Create the pattern visual appearance.
            PDFUncoloredTilingPattern uctp = new PDFUncoloredTilingPattern(20, 20);
            // A pen without color is used to create the pattern content.
            PDFPen noColorPen = new PDFPen(null, 0.8);

            // Circle
            uctp.Canvas.DrawEllipse(noColorPen, 1, 1, 8, 8);
            // Square
            uctp.Canvas.DrawRectangle(noColorPen, 11, 1, 8, 8);
            // Diamond
            diamondPath = new PDFPath();
            diamondPath.StartSubpath(1, 15);
            diamondPath.AddPolyLineTo(new PDFPoint[] { new PDFPoint(5, 11), new PDFPoint(9, 15), new PDFPoint(5, 19) });
            diamondPath.CloseSubpath();
            uctp.Canvas.DrawPath(noColorPen, diamondPath);
            // Triangle
            trianglePath = new PDFPath();
            trianglePath.StartSubpath(11, 19);
            trianglePath.AddPolyLineTo(new PDFPoint[] { new PDFPoint(15, 11), new PDFPoint(19, 19) });
            trianglePath.CloseSubpath();
            uctp.Canvas.DrawPath(noColorPen, trianglePath);

            // Create a pattern colorspace from the pattern object.
            PDFPatternColorSpace uncoloredPatternColorSpace = new PDFPatternColorSpace(uctp);
            // Create a color based on the pattern colorspace.
            PDFPatternColor uncoloredPatternColor = new PDFPatternColor(uncoloredPatternColorSpace);

            // The pen and brush use the pattern color like any other color.
            patternBrush = new PDFPatternBrush(uncoloredPatternColor);

            // Before using the uncolored pattern set the color that will be used to paint the pattern.
            patternBrush.UncoloredPatternPaintColor = new PDFRgbColor(0xFF, 0x40, 0x40);
            page.Canvas.DrawEllipse(patternBrush, 25, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PDFRgbColor(0xA6, 0x4B, 0x00);
            page.Canvas.DrawEllipse(patternBrush, 175, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PDFRgbColor(0x00, 0x63, 0x63);
            page.Canvas.DrawEllipse(patternBrush, 325, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PDFRgbColor(0x00, 0x85, 0x00);
            page.Canvas.DrawEllipse(patternBrush, 475, 320, 125, 200);

            page.Canvas.DrawString("Shading patterns", sectionFont, brush, 25, 550);

            // Create the pattern visual appearance.
            PDFAxialShading horizontalShading = new PDFAxialShading();

            horizontalShading.StartColor = new PDFRgbColor(255, 0, 0);
            horizontalShading.EndColor   = new PDFRgbColor(0, 0, 255);
            horizontalShading.StartPoint = new PDFPoint(25, 600);
            horizontalShading.EndPoint   = new PDFPoint(575, 600);
            PDFShadingPattern sp = new PDFShadingPattern(horizontalShading);

            // Create a pattern colorspace from the pattern object.
            PDFPatternColorSpace shadingPatternColorSpace = new PDFPatternColorSpace(sp);
            // Create a color based on the pattern colorspace.
            PDFPatternColor shadingPatternColor = new PDFPatternColor(shadingPatternColorSpace);

            // The pen and brush use the pattern color like any other color.
            patternPen = new PDFPatternPen(shadingPatternColor, 40);

            page.Canvas.DrawEllipse(patternPen, 50, 600, 500, 150);

            page.Canvas.CompressAndClose();
        }
Ejemplo n.º 7
0
        private static void DrawLines(PDFPage page, PDFFont titleFont, PDFFont sectionFont)
        {
            PDFBrush brush    = new PDFBrush();
            PDFPen   blackPen = new PDFPen(PDFRgbColor.Black, 1);
            PDFPen   bluePen  = new PDFPen(PDFRgbColor.LightBlue, 16);

            page.Canvas.DrawString("Lines", titleFont, brush, 20, 50);

            page.Canvas.DrawString("Line styles:", sectionFont, brush, 20, 70);
            page.Canvas.DrawString("Solid", sectionFont, brush, 20, 90);
            page.Canvas.DrawLine(blackPen, 100, 95, 400, 95);
            page.Canvas.DrawString("Dashed", sectionFont, brush, 20, 110);
            blackPen.DashPattern = new double[] { 3, 3 };
            page.Canvas.DrawLine(blackPen, 100, 115, 400, 115);

            page.Canvas.DrawString("Line cap styles:", sectionFont, brush, 20, 150);
            page.Canvas.DrawString("Flat", sectionFont, brush, 20, 175);
            page.Canvas.DrawLine(bluePen, 100, 180, 400, 180);
            blackPen.DashPattern = null;
            page.Canvas.DrawLine(blackPen, 100, 180, 400, 180);
            page.Canvas.DrawString("Square", sectionFont, brush, 20, 195);
            bluePen.LineCap = PDFLineCap.Square;
            page.Canvas.DrawLine(bluePen, 100, 200, 400, 200);
            blackPen.DashPattern = null;
            page.Canvas.DrawLine(blackPen, 100, 200, 400, 200);
            page.Canvas.DrawString("Round", sectionFont, brush, 20, 215);
            bluePen.LineCap = PDFLineCap.Round;
            page.Canvas.DrawLine(bluePen, 100, 220, 400, 220);
            blackPen.DashPattern = null;
            page.Canvas.DrawLine(blackPen, 100, 220, 400, 220);

            page.Canvas.DrawString("Line join styles:", sectionFont, brush, 20, 250);
            page.Canvas.DrawString("Miter", sectionFont, brush, 20, 280);
            PDFPath miterPath = new PDFPath();

            miterPath.StartSubpath(150, 320);
            miterPath.AddLineTo(250, 260);
            miterPath.AddLineTo(350, 320);
            bluePen.LineCap  = PDFLineCap.Flat;
            bluePen.LineJoin = PDFLineJoin.Miter;
            page.Canvas.DrawPath(bluePen, miterPath);

            page.Canvas.DrawString("Bevel", sectionFont, brush, 20, 360);
            PDFPath bevelPath = new PDFPath();

            bevelPath.StartSubpath(150, 400);
            bevelPath.AddLineTo(250, 340);
            bevelPath.AddLineTo(350, 400);
            bluePen.LineCap  = PDFLineCap.Flat;
            bluePen.LineJoin = PDFLineJoin.Bevel;
            page.Canvas.DrawPath(bluePen, bevelPath);

            page.Canvas.DrawString("Round", sectionFont, brush, 20, 440);
            PDFPath roundPath = new PDFPath();

            roundPath.StartSubpath(150, 480);
            roundPath.AddLineTo(250, 420);
            roundPath.AddLineTo(350, 480);
            bluePen.LineCap  = PDFLineCap.Flat;
            bluePen.LineJoin = PDFLineJoin.Round;
            page.Canvas.DrawPath(bluePen, roundPath);

            page.Canvas.DrawString("Random lines clipped to rectangle", sectionFont, brush, 20, 520);
            PDFPath clipPath = new PDFPath();

            clipPath.AddRectangle(20, 550, 570, 230);

            page.Canvas.SaveGraphicsState();
            page.Canvas.SetClip(clipPath);

            PDFRgbColor randomColor = new PDFRgbColor();
            PDFPen      randomPen   = new PDFPen(randomColor, 1);
            Random      rnd         = new Random();

            for (int i = 0; i < 100; i++)
            {
                randomColor.R = (byte)rnd.Next(256);
                randomColor.G = (byte)rnd.Next(256);
                randomColor.B = (byte)rnd.Next(256);

                page.Canvas.DrawLine(randomPen, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250);
            }

            page.Canvas.RestoreGraphicsState();

            page.Canvas.DrawPath(blackPen, clipPath);

            page.Canvas.CompressAndClose();
        }