Exemplo n.º 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]);
            PdfTextFragmentCollection tfc = ce.ExtractTextFragments();
            PdfTextFragment           tf  = tfc[1];

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

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

                document.Pages[1].Graphics.DrawPath(pen, boundingPath);
            }
        }
Exemplo n.º 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].Graphics.DrawPath(pen, boundingPath);
                slo.X = eic[i].ImageCorners[3].X + 1;
                slo.Y = eic[i].ImageCorners[3].Y + 1;
                document.Pages[2].Graphics.DrawString(imageProperties, sao, slo);
            }
        }
Exemplo 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.Graphics.DrawPath(pen, path);
                }
            }
        }
Exemplo n.º 4
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.Graphics.DrawPath(pen, path);
                }
            }
        }
Exemplo n.º 5
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.Graphics.DrawString("Patterns", titleFont, brush, 20, 50);

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

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

            // Red circle
            ctp.Graphics.DrawEllipse(darkRedPen, lightRedBrush, 1, 1, 8, 8);
            // Cyan square
            ctp.Graphics.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.Graphics.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.Graphics.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.Graphics.DrawEllipse(patternBrush, 25, 90, 250, 200);
            page.Graphics.DrawRoundRectangle(patternPen, 310, 110, 250, 160, 100, 100);

            page.Graphics.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.Graphics.DrawEllipse(noColorPen, 1, 1, 8, 8);
            // Square
            uctp.Graphics.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.Graphics.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.Graphics.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.Graphics.DrawEllipse(patternBrush, 25, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0xA6, 0x4B, 0x00);
            page.Graphics.DrawEllipse(patternBrush, 175, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x63, 0x63);
            page.Graphics.DrawEllipse(patternBrush, 325, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x85, 0x00);
            page.Graphics.DrawEllipse(patternBrush, 475, 320, 125, 200);

            page.Graphics.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.Graphics.DrawEllipse(patternPen, 50, 600, 500, 150);

            page.Graphics.CompressAndClose();
        }
Exemplo n.º 6
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.Graphics.DrawString("Lines", titleFont, brush, 20, 50);

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

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

            page.Graphics.DrawString("Line join styles:", sectionFont, brush, 20, 250);
            page.Graphics.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.Graphics.DrawPath(bluePen, miterPath);

            page.Graphics.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.Graphics.DrawPath(bluePen, bevelPath);

            page.Graphics.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.Graphics.DrawPath(bluePen, roundPath);

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

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

            page.Graphics.SaveGraphicsState();
            page.Graphics.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.Graphics.DrawLine(randomPen, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250);
            }

            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawPath(blackPen, clipPath);

            page.Graphics.CompressAndClose();
        }
Exemplo n.º 7
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.Graphics.DrawString("Patterns", titleFont, brush, 20, 50);

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

            // Create the pattern visual appearance.
            PdfColoredTilingPattern ctp = new PdfColoredTilingPattern(20, 20);
            // Red circle
            ctp.Graphics.DrawEllipse(darkRedPen, lightRedBrush, 1, 1, 8, 8);
            // Cyan square
            ctp.Graphics.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.Graphics.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.Graphics.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.Graphics.DrawEllipse(patternBrush, 25, 90, 250, 200);
            page.Graphics.DrawRoundRectangle(patternPen, 310, 110, 250, 160, 100, 100);

            page.Graphics.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.Graphics.DrawEllipse(noColorPen, 1, 1, 8, 8);
            // Square
            uctp.Graphics.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.Graphics.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.Graphics.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.Graphics.DrawEllipse(patternBrush, 25, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0xA6, 0x4B, 0x00);
            page.Graphics.DrawEllipse(patternBrush, 175, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x63, 0x63);
            page.Graphics.DrawEllipse(patternBrush, 325, 320, 125, 200);
            patternBrush.UncoloredPatternPaintColor = new PdfRgbColor(0x00, 0x85, 0x00);
            page.Graphics.DrawEllipse(patternBrush, 475, 320, 125, 200);

            page.Graphics.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.Graphics.DrawEllipse(patternPen, 50, 600, 500, 150);

            page.Graphics.CompressAndClose();
        }
Exemplo n.º 8
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.Graphics.DrawString("Lines", titleFont, brush, 20, 50);

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

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

            page.Graphics.DrawString("Line join styles:", sectionFont, brush, 20, 250);
            page.Graphics.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.Graphics.DrawPath(bluePen, miterPath);

            page.Graphics.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.Graphics.DrawPath(bluePen, bevelPath);

            page.Graphics.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.Graphics.DrawPath(bluePen, roundPath);

            page.Graphics.DrawString("Random lines clipped to rectangle", sectionFont, brush, 20, 520);
            PdfPath clipPath = new PdfPath();
            clipPath.AddRectangle(20, 550, 570, 230);

            page.Graphics.SaveGraphicsState();
            page.Graphics.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.Graphics.DrawLine(randomPen, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250, rnd.NextDouble() * page.Width, 550 + rnd.NextDouble() * 250);
            }

            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawPath(blackPen, clipPath);

            page.Graphics.CompressAndClose();
        }
Exemplo n.º 9
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].Graphics.DrawPath(redPen, contour);

                        document.Pages[0].Graphics.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].Graphics.DrawPath(redPen, contour);

                        document.Pages[0].Graphics.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].Graphics.DrawPath(redPen, contour);

                        document.Pages[0].Graphics.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, "xfinium.pdf.sample.pageobjects.pdf") };
            return output;
        }
Exemplo n.º 10
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].Graphics.DrawPath(redPen, contour);

                    document.Pages[0].Graphics.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].Graphics.DrawPath(redPen, contour);

                    document.Pages[0].Graphics.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].Graphics.DrawPath(redPen, contour);

                    document.Pages[0].Graphics.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, "xfinium.pdf.sample.pageobjects.pdf") };
            return(output);
        }