/// <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);
            }
        }
        /// <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);
            }
        }
        /// <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);
        }
Example #4
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);
        }
Example #5
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();
        }