示例#1
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);
            }
        }
示例#2
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);
            }
        }
示例#3
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);
        }
示例#4
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();
        }