예제 #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);
            }
        }
예제 #2
0
        private static void fill(PdfTilingPattern pattern)
        {
            PdfCanvas canvas = pattern.Canvas;
            PdfColor  red    = new PdfRgbColor(255, 0, 0);

            canvas.Brush.Color = red;
            canvas.Pen.Color   = red;
            canvas.AppendCircle(new PointF(2, 2), 2);
            canvas.FillAndStrokePath(PdfFillMode.Winding);
        }
예제 #3
0
        private static void DrawBezierCurves(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush     = new PdfBrush();
            PdfPen   blackPen  = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen   redPen    = new PdfPen(PdfRgbColor.Red, 1);
            PdfBrush blueBrush = new PdfBrush(PdfRgbColor.DarkBlue);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen      randomPen      = new PdfPen(randomPenColor, 1);

            page.Graphics.DrawString("Bezier curves", titleFont, brush, 20, 50);

            page.Graphics.DrawLine(blackPen, 20, 210, 600, 210);
            page.Graphics.DrawLine(blackPen, 306, 70, 306, 350);
            page.Graphics.DrawRectangle(blueBrush, 39, 339, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 279, 79, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 499, 299, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 589, 69, 2, 2);
            page.Graphics.DrawBezier(redPen, 40, 340, 280, 80, 500, 300, 590, 70);

            page.Graphics.DrawString("Random bezier curves clipped to view", sectionFont, brush, 20, 385);
            PdfPath rectPath = new PdfPath();

            rectPath.AddRectangle(20, 400, 570, 300);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(rectPath);

            Random rnd = new Random();

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

                double x1 = rnd.NextDouble() * page.Width;
                double y1 = 380 + rnd.NextDouble() * 350;
                double x2 = rnd.NextDouble() * page.Width;
                double y2 = 380 + rnd.NextDouble() * 350;
                double x3 = rnd.NextDouble() * page.Width;
                double y3 = 380 + rnd.NextDouble() * 350;
                double x4 = rnd.NextDouble() * page.Width;
                double y4 = 380 + rnd.NextDouble() * 350;

                page.Graphics.DrawBezier(randomPen, x1, y1, x2, y2, x3, y3, x4, y4);
            }

            page.Graphics.RestoreGraphicsState();

            blackPen.DashPattern = null;
            page.Graphics.DrawPath(blackPen, rectPath);

            page.Graphics.CompressAndClose();
        }
예제 #4
0
        private static void DrawFormXObjects(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush    = new PdfBrush();
            PdfPen   blackPen = new PdfPen(PdfRgbColor.Black, 1);

            PdfRgbColor randomPenColor   = new PdfRgbColor();
            PdfPen      randomPen        = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush    randomBrush      = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Form XObjects", titleFont, brush, 20, 50);
            page.Graphics.DrawString("Scaling", sectionFont, brush, 20, 70);

            // Create the XObject content - random rectangles
            PdfFormXObject xobject = new PdfFormXObject(300, 300);
            Random         rnd     = new Random();

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

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                double left        = rnd.NextDouble() * xobject.Width;
                double top         = rnd.NextDouble() * xobject.Height;
                double width       = rnd.NextDouble() * xobject.Width;
                double height      = rnd.NextDouble() * xobject.Height;
                double orientation = rnd.Next(360);
                xobject.Graphics.DrawRectangle(randomPen, randomBrush, left, top, width, height, orientation);
            }

            xobject.Graphics.DrawRectangle(blackPen, 0, 0, xobject.Width, xobject.Height);
            xobject.Graphics.CompressAndClose();

            // Draw the form XObject 3 times on the page at different sizes.
            page.Graphics.DrawFormXObject(xobject, 3, 90, 100, 100);
            page.Graphics.DrawFormXObject(xobject, 106, 90, 200, 200);
            page.Graphics.DrawFormXObject(xobject, 309, 90, 300, 300);

            page.Graphics.DrawString("Flipping", sectionFont, brush, 20, 420);
            page.Graphics.DrawFormXObject(xobject, 20, 440, 150, 150);
            page.Graphics.DrawFormXObject(xobject, 200, 440, 150, 150, 0, PdfFlipDirection.VerticalFlip);
            page.Graphics.DrawFormXObject(xobject, 20, 620, 150, 150, 0, PdfFlipDirection.HorizontalFlip);
            page.Graphics.DrawFormXObject(xobject, 200, 620, 150, 150, 0, PdfFlipDirection.VerticalFlip | PdfFlipDirection.HorizontalFlip);

            page.Graphics.CompressAndClose();
        }
        /// <summary>
        /// Main method for running the sample.
        /// </summary>
        public static SampleOutputInfo[] Run(Stream output)
        {
            PdfFixedDocument document = new PdfFixedDocument();

            // Init the save operation
            document.BeginSave(output);

            PdfRgbColor color = new PdfRgbColor();
            PdfBrush brush = new PdfBrush(color);
            Random rnd = new Random();

            for (int i = 0; i < 3; i++)
            {
                PdfPage page = document.Pages.Add();
                page.Width = 1000;
                page.Height = 1000;

                for (int y = 1; y <= page.Height; y++)
                {
                    for (int x = 0; x < page.Width; x++)
                    {
                        color.R = (byte)rnd.Next(256);
                        color.G = (byte)rnd.Next(256);
                        color.B = (byte)rnd.Next(256);

                        page.Graphics.DrawRectangle(brush, x, y - 1, 1, 1);
                    }

                    if ((y % 100) == 0)
                    {
                        // Compress the graphics that have been drawn so far and save them.
                        page.Graphics.Compress();
                        page.SaveGraphics();
                    }
                }

                // Close the page graphics and save the page.
                page.Graphics.CompressAndClose();
                page.Save();
            }

            // Finish the document.
            document.EndSave();

            return null;
        }
        private static Color toGdiColor(PdfColor pdfColor, int opacityPercent)
        {
            if (pdfColor == null)
            {
                return(Color.Empty);
            }

            PdfRgbColor rgbColor = pdfColor.ToRgb();

            int alpha = 255;

            if (opacityPercent < 100 && opacityPercent >= 0)
            {
                alpha = (int)(255.0 * opacityPercent / 100.0);
            }

            return(Color.FromArgb(alpha, rgbColor.R, rgbColor.G, rgbColor.B));
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "TextMarkupAnnotations.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfPage   page   = pdf.Pages[0];
                PdfCanvas canvas = page.Canvas;

                // Draw text on the page
                const string Text         = "Highlighted text";
                PdfPoint     textPosition = new PdfPoint(10, 50);
                canvas.FontSize = 30;
                canvas.DrawString(textPosition, Text);

                // Get size of the drawn text
                PdfSize size   = canvas.MeasureText(Text);
                var     bounds = new PdfRectangle(textPosition, size);

                // Highlight and annotate the text
                var          color = new PdfRgbColor(0, 0, 255);
                const string AnnotationContents = "Lorem ipsum";
                page.AddHighlightAnnotation(AnnotationContents, bounds, color);

                // Or call these method to strike out or underline the text:
                // page.AddStrikeoutAnnotation(AnnotationContents, bounds, color);
                // page.AddJaggedUnderlineAnnotation(AnnotationContents, bounds, color);
                // page.AddUnderlineAnnotation(AnnotationContents, bounds, color);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
        public static void Main()
        {
            // NOTE:
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "FindAndHighlightText.pdf";

            using (var pdf = new PdfDocument(@"..\Sample Data\jfif3.pdf"))
            {
                const string           TextToFind = "JPEG File Interchange Format";
                const StringComparison Comparison = StringComparison.InvariantCultureIgnoreCase;
                var highlightColor = new PdfRgbColor(255, 255, 0);

                highlightPhrases(pdf, TextToFind, Comparison, highlightColor);

                pdf.Save(pathToFile);
            }

            Console.WriteLine($"The output is located in {Environment.CurrentDirectory}");
        }
예제 #9
0
        private static void DrawShadings(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush    = new PdfBrush();
            PdfPen   blackPen = new PdfPen(PdfRgbColor.Black, 1);

            PdfRgbColor randomPenColor   = new PdfRgbColor();
            PdfPen      randomPen        = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush    randomBrush      = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Shadings", titleFont, brush, 20, 50);

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

            PdfAxialShading horizontalShading = new PdfAxialShading();

            horizontalShading.StartColor = new PdfRgbColor(255, 0, 0);
            horizontalShading.EndColor   = new PdfRgbColor(0, 0, 255);
            horizontalShading.StartPoint = new PdfPoint(25, 90);
            horizontalShading.EndPoint   = new PdfPoint(175, 90);

            // Clip the shading to desired area.
            PdfPath hsArea = new PdfPath();

            hsArea.AddRectangle(25, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(hsArea);
            page.Graphics.DrawShading(horizontalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Vertical", sectionFont, brush, 225, 70);

            PdfAxialShading verticalShading = new PdfAxialShading();

            verticalShading.StartColor = new PdfRgbColor(255, 0, 0);
            verticalShading.EndColor   = new PdfRgbColor(0, 0, 255);
            verticalShading.StartPoint = new PdfPoint(225, 90);
            verticalShading.EndPoint   = new PdfPoint(225, 240);

            // Clip the shading to desired area.
            PdfPath vsArea = new PdfPath();

            vsArea.AddRectangle(225, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(vsArea);
            page.Graphics.DrawShading(verticalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Diagonal", sectionFont, brush, 425, 70);

            PdfAxialShading diagonalShading = new PdfAxialShading();

            diagonalShading.StartColor = new PdfRgbColor(255, 0, 0);
            diagonalShading.EndColor   = new PdfRgbColor(0, 0, 255);
            diagonalShading.StartPoint = new PdfPoint(425, 90);
            diagonalShading.EndPoint   = new PdfPoint(575, 240);

            // Clip the shading to desired area.
            PdfPath dsArea = new PdfPath();

            dsArea.AddRectangle(425, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(dsArea);
            page.Graphics.DrawShading(diagonalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Extended shading", sectionFont, brush, 25, 260);

            PdfAxialShading extendedShading = new PdfAxialShading();

            extendedShading.StartColor  = new PdfRgbColor(255, 0, 0);
            extendedShading.EndColor    = new PdfRgbColor(0, 0, 255);
            extendedShading.StartPoint  = new PdfPoint(225, 280);
            extendedShading.EndPoint    = new PdfPoint(375, 280);
            extendedShading.ExtendStart = true;
            extendedShading.ExtendEnd   = true;

            // Clip the shading to desired area.
            PdfPath esArea = new PdfPath();

            esArea.AddRectangle(25, 280, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(esArea);
            page.Graphics.DrawShading(extendedShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, esArea);

            page.Graphics.DrawString("Limited shading", sectionFont, brush, 25, 330);

            PdfAxialShading limitedShading = new PdfAxialShading();

            limitedShading.StartColor  = new PdfRgbColor(255, 0, 0);
            limitedShading.EndColor    = new PdfRgbColor(0, 0, 255);
            limitedShading.StartPoint  = new PdfPoint(225, 350);
            limitedShading.EndPoint    = new PdfPoint(375, 350);
            limitedShading.ExtendStart = false;
            limitedShading.ExtendEnd   = false;

            // Clip the shading to desired area.
            PdfPath lsArea = new PdfPath();

            lsArea.AddRectangle(25, 350, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(lsArea);
            page.Graphics.DrawShading(limitedShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, lsArea);

            page.Graphics.DrawString("Multi-stop shading", sectionFont, brush, 25, 400);
            // Multi-stop shadings use a stitching function to combine the functions that define each gradient part.
            // Function for red to blue shading.
            PdfExponentialFunction redToBlueFunc = new PdfExponentialFunction();

            // Linear function
            redToBlueFunc.Exponent = 1;
            redToBlueFunc.Domain   = new double[] { 0, 1 };
            // Red color for start
            redToBlueFunc.C0 = new double[] { 1, 0, 0 };
            // Blue color for start
            redToBlueFunc.C1 = new double[] { 0, 0, 1 };
            // Function for blue to green shading.
            PdfExponentialFunction blueToGreenFunc = new PdfExponentialFunction();

            // Linear function
            blueToGreenFunc.Exponent = 1;
            blueToGreenFunc.Domain   = new double[] { 0, 1 };
            // Blue color for start
            blueToGreenFunc.C0 = new double[] { 0, 0, 1 };
            // Green color for start
            blueToGreenFunc.C1 = new double[] { 0, 1, 0 };

            //Stitching function for the shading.
            PdfStitchingFunction shadingFunction = new PdfStitchingFunction();

            shadingFunction.Functions.Add(redToBlueFunc);
            shadingFunction.Functions.Add(blueToGreenFunc);
            shadingFunction.Domain = new double[] { 0, 1 };
            shadingFunction.Encode = new double[] { 0, 1, 0, 1 };

            // Entire shading goes from 0 to 1 (100%).
            // We set the first shading (red->blue) to cover 30% (0 - 0.3) and
            // the second shading to cover 70% (0.3 - 1).
            shadingFunction.Bounds = new double[] { 0.3 };
            // The multistop shading
            PdfAxialShading multiStopShading = new PdfAxialShading();

            multiStopShading.StartPoint = new PdfPoint(25, 420);
            multiStopShading.EndPoint   = new PdfPoint(575, 420);
            // The colorspace must match the colors specified in C0 & C1
            multiStopShading.ColorSpace = new PdfRgbColorSpace();
            multiStopShading.Function   = shadingFunction;

            // Clip the shading to desired area.
            PdfPath mssArea = new PdfPath();

            mssArea.AddRectangle(25, 420, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(mssArea);
            page.Graphics.DrawShading(multiStopShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, lsArea);

            page.Graphics.DrawString("Radial shading", sectionFont, brush, 25, 470);

            PdfRadialShading rs1 = new PdfRadialShading();

            rs1.StartColor        = new PdfRgbColor(0, 255, 0);
            rs1.EndColor          = new PdfRgbColor(255, 0, 255);
            rs1.StartCircleCenter = new PdfPoint(50, 500);
            rs1.StartCircleRadius = 10;
            rs1.EndCircleCenter   = new PdfPoint(500, 570);
            rs1.EndCircleRadius   = 100;

            page.Graphics.DrawShading(rs1);

            PdfRadialShading rs2 = new PdfRadialShading();

            rs2.StartColor        = new PdfRgbColor(0, 255, 0);
            rs2.EndColor          = new PdfRgbColor(255, 0, 255);
            rs2.StartCircleCenter = new PdfPoint(80, 600);
            rs2.StartCircleRadius = 10;
            rs2.EndCircleCenter   = new PdfPoint(110, 690);
            rs2.EndCircleRadius   = 100;

            page.Graphics.DrawShading(rs2);

            page.Graphics.CompressAndClose();
        }
예제 #10
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();
        }
예제 #11
0
        private static void DrawArcsAndPies(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen randomPen = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush randomBrush = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Arcs", titleFont, brush, 20, 50);
            page.Graphics.DrawString("Pies", titleFont, brush, 310, 50);

            page.Graphics.DrawLine(blackPen, 20, 210, 300, 210);
            page.Graphics.DrawLine(blackPen, 160, 70, 160, 350);
            page.Graphics.DrawLine(blackPen, 310, 210, 590, 210);
            page.Graphics.DrawLine(blackPen, 450, 70, 450, 350);

            blackPen.DashPattern = new double[] { 2, 2 };
            page.Graphics.DrawLine(blackPen, 20, 70, 300, 350);
            page.Graphics.DrawLine(blackPen, 20, 350, 300, 70);
            page.Graphics.DrawLine(blackPen, 310, 70, 590, 350);
            page.Graphics.DrawLine(blackPen, 310, 350, 590, 70);

            page.Graphics.DrawArc(redPen, 30, 80, 260, 260, 0, 135);
            page.Graphics.DrawPie(redPen, 320, 80, 260, 260, 45, 270);

            page.Graphics.DrawString("Random arcs and pies clipped to view", sectionFont, brush, 20, 385);
            PdfPath rectPath = new PdfPath();
            rectPath.AddRectangle(20, 400, 570, 300);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(rectPath);

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

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                int mode = rnd.Next(4);
                double left = rnd.NextDouble() * page.Width;
                double top = 380 + rnd.NextDouble() * 350;
                double width = rnd.NextDouble() * page.Width;
                double height = rnd.NextDouble() * 250;
                double startAngle = rnd.Next(360);
                double sweepAngle = rnd.Next(360);
                switch (mode)
                {
                    case 0:
                        // Stroke arc outline
                        page.Graphics.DrawArc(randomPen, left, top, width, height, startAngle, sweepAngle);
                        break;
                    case 1:
                        // Stroke pie outline
                        page.Graphics.DrawPie(randomPen, left, top, width, height, startAngle, sweepAngle);
                        break;
                    case 2:
                        // Fill pie interior
                        page.Graphics.DrawPie(randomBrush, left, top, width, height, startAngle, sweepAngle);
                        break;
                    case 3:
                        // Stroke and fill pie
                        page.Graphics.DrawPie(randomPen, randomBrush, left, top, width, height, startAngle, sweepAngle);
                        break;
                }
            }

            page.Graphics.RestoreGraphicsState();

            blackPen.DashPattern = null;
            page.Graphics.DrawPath(blackPen, rectPath);

            page.Graphics.CompressAndClose();
        }
예제 #12
0
        private static void DrawArcsAndPies(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush    = new PdfBrush();
            PdfPen   blackPen = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen   redPen   = new PdfPen(PdfRgbColor.Red, 1);

            PdfRgbColor randomPenColor   = new PdfRgbColor();
            PdfPen      randomPen        = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush    randomBrush      = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Arcs", titleFont, brush, 20, 50);
            page.Graphics.DrawString("Pies", titleFont, brush, 310, 50);

            page.Graphics.DrawLine(blackPen, 20, 210, 300, 210);
            page.Graphics.DrawLine(blackPen, 160, 70, 160, 350);
            page.Graphics.DrawLine(blackPen, 310, 210, 590, 210);
            page.Graphics.DrawLine(blackPen, 450, 70, 450, 350);

            blackPen.DashPattern = new double[] { 2, 2 };
            page.Graphics.DrawLine(blackPen, 20, 70, 300, 350);
            page.Graphics.DrawLine(blackPen, 20, 350, 300, 70);
            page.Graphics.DrawLine(blackPen, 310, 70, 590, 350);
            page.Graphics.DrawLine(blackPen, 310, 350, 590, 70);

            page.Graphics.DrawArc(redPen, 30, 80, 260, 260, 0, 135);
            page.Graphics.DrawPie(redPen, 320, 80, 260, 260, 45, 270);

            page.Graphics.DrawString("Random arcs and pies clipped to view", sectionFont, brush, 20, 385);
            PdfPath rectPath = new PdfPath();

            rectPath.AddRectangle(20, 400, 570, 300);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(rectPath);

            Random rnd = new Random();

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

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                int    mode       = rnd.Next(4);
                double left       = rnd.NextDouble() * page.Width;
                double top        = 380 + rnd.NextDouble() * 350;
                double width      = rnd.NextDouble() * page.Width;
                double height     = rnd.NextDouble() * 250;
                double startAngle = rnd.Next(360);
                double sweepAngle = rnd.Next(360);
                switch (mode)
                {
                case 0:
                    // Stroke arc outline
                    page.Graphics.DrawArc(randomPen, left, top, width, height, startAngle, sweepAngle);
                    break;

                case 1:
                    // Stroke pie outline
                    page.Graphics.DrawPie(randomPen, left, top, width, height, startAngle, sweepAngle);
                    break;

                case 2:
                    // Fill pie interior
                    page.Graphics.DrawPie(randomBrush, left, top, width, height, startAngle, sweepAngle);
                    break;

                case 3:
                    // Stroke and fill pie
                    page.Graphics.DrawPie(randomPen, randomBrush, left, top, width, height, startAngle, sweepAngle);
                    break;
                }
            }

            page.Graphics.RestoreGraphicsState();

            blackPen.DashPattern = null;
            page.Graphics.DrawPath(blackPen, rectPath);

            page.Graphics.CompressAndClose();
        }
예제 #13
0
        private static void DrawEllipses(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush    = new PdfBrush();
            PdfPen   blackPen = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen   redPen   = new PdfPen(PdfRgbColor.Red, 1);

            PdfRgbColor randomPenColor   = new PdfRgbColor();
            PdfPen      randomPen        = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush    randomBrush      = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Ellipses", titleFont, brush, 20, 50);

            page.Graphics.DrawLine(blackPen, 20, 150, 300, 150);
            page.Graphics.DrawLine(blackPen, 80, 70, 80, 350);
            page.Graphics.DrawEllipse(redPen, 80, 150, 180, 100);

            page.Graphics.DrawLine(blackPen, 320, 150, 600, 150);
            page.Graphics.DrawLine(blackPen, 380, 70, 380, 350);
            page.Graphics.DrawEllipse(redPen, 380, 150, 180, 100, 30);

            page.Graphics.DrawString("Random ellipses clipped to view", sectionFont, brush, 20, 385);
            PdfPath ellipsePath = new PdfPath();

            ellipsePath.AddEllipse(20, 400, 570, 300);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(ellipsePath);

            Random rnd = new Random();

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

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                int    mode        = rnd.Next(3);
                double left        = rnd.NextDouble() * page.Width;
                double top         = 380 + rnd.NextDouble() * 350;
                double width       = rnd.NextDouble() * page.Width;
                double height      = rnd.NextDouble() * 250;
                double orientation = rnd.Next(360);
                switch (mode)
                {
                case 0:
                    // Stroke ellipse outline
                    page.Graphics.DrawEllipse(randomPen, left, top, width, height, orientation);
                    break;

                case 1:
                    // Fill ellipse interior
                    page.Graphics.DrawEllipse(randomBrush, left, top, width, height, orientation);
                    break;

                case 2:
                    // Stroke and fill ellipse
                    page.Graphics.DrawEllipse(randomPen, randomBrush, left, top, width, height, orientation);
                    break;
                }
            }

            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawPath(blackPen, ellipsePath);

            page.Graphics.CompressAndClose();
        }
예제 #14
0
        private static void DrawBezierCurves(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1);
            PdfBrush blueBrush = new PdfBrush(PdfRgbColor.DarkBlue);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen randomPen = new PdfPen(randomPenColor, 1);

            page.Graphics.DrawString("Bezier curves", titleFont, brush, 20, 50);

            page.Graphics.DrawLine(blackPen, 20, 210, 600, 210);
            page.Graphics.DrawLine(blackPen, 306, 70, 306, 350);
            page.Graphics.DrawRectangle(blueBrush, 39, 339, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 279, 79, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 499, 299, 2, 2);
            page.Graphics.DrawRectangle(blueBrush, 589, 69, 2, 2);
            page.Graphics.DrawBezier(redPen, 40, 340, 280, 80, 500, 300, 590, 70);

            page.Graphics.DrawString("Random bezier curves clipped to view", sectionFont, brush, 20, 385);
            PdfPath rectPath = new PdfPath();
            rectPath.AddRectangle(20, 400, 570, 300);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(rectPath);

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

                double x1 = rnd.NextDouble() * page.Width;
                double y1 = 380 + rnd.NextDouble() * 350;
                double x2 = rnd.NextDouble() * page.Width;
                double y2 = 380 + rnd.NextDouble() * 350;
                double x3 = rnd.NextDouble() * page.Width;
                double y3 = 380 + rnd.NextDouble() * 350;
                double x4 = rnd.NextDouble() * page.Width;
                double y4 = 380 + rnd.NextDouble() * 350;

                page.Graphics.DrawBezier(randomPen, x1, y1, x2, y2, x3, y3, x4, y4);
            }

            page.Graphics.RestoreGraphicsState();

            blackPen.DashPattern = null;
            page.Graphics.DrawPath(blackPen, rectPath);

            page.Graphics.CompressAndClose();
        }
예제 #15
0
        private static void DrawFormXObjects(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen randomPen = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush randomBrush = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Form XObjects", titleFont, brush, 20, 50);
            page.Graphics.DrawString("Scaling", sectionFont, brush, 20, 70);

            // Create the XObject content - random rectangles
            PdfFormXObject xobject = new PdfFormXObject(300, 300);
            Random rnd = new Random();
            for (int i = 0; i < 100; i++)
            {
                randomPenColor.R = (byte)rnd.Next(256);
                randomPenColor.G = (byte)rnd.Next(256);
                randomPenColor.B = (byte)rnd.Next(256);

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                double left = rnd.NextDouble() * xobject.Width;
                double top = rnd.NextDouble() * xobject.Height;
                double width = rnd.NextDouble() * xobject.Width;
                double height = rnd.NextDouble() * xobject.Height;
                double orientation = rnd.Next(360);
                xobject.Graphics.DrawRectangle(randomPen, randomBrush, left, top, width, height, orientation);
            }

            xobject.Graphics.DrawRectangle(blackPen, 0, 0, xobject.Width, xobject.Height);
            xobject.Graphics.CompressAndClose();

            // Draw the form XObject 3 times on the page at different sizes.
            page.Graphics.DrawFormXObject(xobject, 3, 90, 100, 100);
            page.Graphics.DrawFormXObject(xobject, 106, 90, 200, 200);
            page.Graphics.DrawFormXObject(xobject, 309, 90, 300, 300);

            page.Graphics.DrawString("Flipping", sectionFont, brush, 20, 420);
            page.Graphics.DrawFormXObject(xobject, 20, 440, 150, 150);
            page.Graphics.DrawFormXObject(xobject, 200, 440, 150, 150, 0, PdfFlipDirection.VerticalFlip);
            page.Graphics.DrawFormXObject(xobject, 20, 620, 150, 150, 0, PdfFlipDirection.HorizontalFlip);
            page.Graphics.DrawFormXObject(xobject, 200, 620, 150, 150, 0, PdfFlipDirection.VerticalFlip | PdfFlipDirection.HorizontalFlip);

            page.Graphics.CompressAndClose();
        }
예제 #16
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();
        }
예제 #17
0
        private static void DrawRoundRectangles(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1);
            PdfPen redPen = new PdfPen(PdfRgbColor.Red, 1);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen randomPen = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush randomBrush = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Round rectangles", titleFont, brush, 20, 50);

            page.Graphics.DrawLine(blackPen, 20, 150, 300, 150);
            page.Graphics.DrawLine(blackPen, 80, 70, 80, 350);
            page.Graphics.DrawRoundRectangle(redPen, 80, 150, 180, 100, 20, 20);

            page.Graphics.DrawLine(blackPen, 320, 150, 600, 150);
            page.Graphics.DrawLine(blackPen, 380, 70, 380, 350);
            page.Graphics.DrawRoundRectangle(redPen, 380, 150, 180, 100, 20, 20, 30);

            page.Graphics.DrawString("Random round rectangles clipped to view", sectionFont, brush, 20, 385);
            PdfPath roundRectPath = new PdfPath();
            roundRectPath.AddRoundRectangle(20, 400, 570, 300, 20, 20);

            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(roundRectPath);

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

                randomBrushColor.R = (byte)rnd.Next(256);
                randomBrushColor.G = (byte)rnd.Next(256);
                randomBrushColor.B = (byte)rnd.Next(256);

                int mode = rnd.Next(3);
                double left = rnd.NextDouble() * page.Width;
                double top = 380 + rnd.NextDouble() * 350;
                double width = rnd.NextDouble() * page.Width;
                double height = rnd.NextDouble() * 250;
                double orientation = rnd.Next(360);
                switch (mode)
                {
                    case 0:
                        // Stroke rectangle outline
                        page.Graphics.DrawRoundRectangle(randomPen, left, top, width, height, width * 0.1, height * 0.1, orientation);
                        break;
                    case 1:
                        // Fill rectangle interior
                        page.Graphics.DrawRoundRectangle(randomBrush, left, top, width, height, width * 0.1, height * 0.1, orientation);
                        break;
                    case 2:
                        // Stroke and fill rectangle
                        page.Graphics.DrawRoundRectangle(randomPen, randomBrush, left, top, width, height, width * 0.1, height * 0.1, orientation);
                        break;
                }
            }

            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawPath(blackPen, roundRectPath);

            page.Graphics.CompressAndClose();
        }
예제 #18
0
        private static void DrawShadings(PdfPage page, PdfFont titleFont, PdfFont sectionFont)
        {
            PdfBrush brush = new PdfBrush();
            PdfPen blackPen = new PdfPen(PdfRgbColor.Black, 1);

            PdfRgbColor randomPenColor = new PdfRgbColor();
            PdfPen randomPen = new PdfPen(randomPenColor, 1);
            PdfRgbColor randomBrushColor = new PdfRgbColor();
            PdfBrush randomBrush = new PdfBrush(randomBrushColor);

            page.Graphics.DrawString("Shadings", titleFont, brush, 20, 50);

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

            PdfAxialShading horizontalShading = new PdfAxialShading();
            horizontalShading.StartColor = new PdfRgbColor(255, 0, 0);
            horizontalShading.EndColor = new PdfRgbColor(0, 0, 255);
            horizontalShading.StartPoint = new PdfPoint(25, 90);
            horizontalShading.EndPoint = new PdfPoint(175, 90);

            // Clip the shading to desired area.
            PdfPath hsArea = new PdfPath();
            hsArea.AddRectangle(25, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(hsArea);
            page.Graphics.DrawShading(horizontalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Vertical", sectionFont, brush, 225, 70);

            PdfAxialShading verticalShading = new PdfAxialShading();
            verticalShading.StartColor = new PdfRgbColor(255, 0, 0);
            verticalShading.EndColor = new PdfRgbColor(0, 0, 255);
            verticalShading.StartPoint = new PdfPoint(225, 90);
            verticalShading.EndPoint = new PdfPoint(225, 240);

            // Clip the shading to desired area.
            PdfPath vsArea = new PdfPath();
            vsArea.AddRectangle(225, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(vsArea);
            page.Graphics.DrawShading(verticalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Diagonal", sectionFont, brush, 425, 70);

            PdfAxialShading diagonalShading = new PdfAxialShading();
            diagonalShading.StartColor = new PdfRgbColor(255, 0, 0);
            diagonalShading.EndColor = new PdfRgbColor(0, 0, 255);
            diagonalShading.StartPoint = new PdfPoint(425, 90);
            diagonalShading.EndPoint = new PdfPoint(575, 240);

            // Clip the shading to desired area.
            PdfPath dsArea = new PdfPath();
            dsArea.AddRectangle(425, 90, 150, 150);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(dsArea);
            page.Graphics.DrawShading(diagonalShading);
            page.Graphics.RestoreGraphicsState();

            page.Graphics.DrawString("Extended shading", sectionFont, brush, 25, 260);

            PdfAxialShading extendedShading = new PdfAxialShading();
            extendedShading.StartColor = new PdfRgbColor(255, 0, 0);
            extendedShading.EndColor = new PdfRgbColor(0, 0, 255);
            extendedShading.StartPoint = new PdfPoint(225, 280);
            extendedShading.EndPoint = new PdfPoint(375, 280);
            extendedShading.ExtendStart = true;
            extendedShading.ExtendEnd = true;

            // Clip the shading to desired area.
            PdfPath esArea = new PdfPath();
            esArea.AddRectangle(25, 280, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(esArea);
            page.Graphics.DrawShading(extendedShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, esArea);

            page.Graphics.DrawString("Limited shading", sectionFont, brush, 25, 330);

            PdfAxialShading limitedShading = new PdfAxialShading();
            limitedShading.StartColor = new PdfRgbColor(255, 0, 0);
            limitedShading.EndColor = new PdfRgbColor(0, 0, 255);
            limitedShading.StartPoint = new PdfPoint(225, 350);
            limitedShading.EndPoint = new PdfPoint(375, 350);
            limitedShading.ExtendStart = false;
            limitedShading.ExtendEnd = false;

            // Clip the shading to desired area.
            PdfPath lsArea = new PdfPath();
            lsArea.AddRectangle(25, 350, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(lsArea);
            page.Graphics.DrawShading(limitedShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, lsArea);

            page.Graphics.DrawString("Multi-stop shading", sectionFont, brush, 25, 400);
            // Multi-stop shadings use a stitching function to combine the functions that define each gradient part.
            // Function for red to blue shading.
            PdfExponentialFunction redToBlueFunc = new PdfExponentialFunction();
            // Linear function
            redToBlueFunc.Exponent = 1;
            redToBlueFunc.Domain = new double[] { 0, 1 };
            // Red color for start
            redToBlueFunc.C0 = new double[] { 1, 0, 0 };
            // Blue color for start
            redToBlueFunc.C1 = new double[] { 0, 0, 1 };
            // Function for blue to green shading.
            PdfExponentialFunction blueToGreenFunc = new PdfExponentialFunction();
            // Linear function
            blueToGreenFunc.Exponent = 1;
            blueToGreenFunc.Domain = new double[] { 0, 1 };
            // Blue color for start
            blueToGreenFunc.C0 = new double[] { 0, 0, 1 };
            // Green color for start
            blueToGreenFunc.C1 = new double[] { 0, 1, 0 };

            //Stitching function for the shading.
            PdfStitchingFunction shadingFunction = new PdfStitchingFunction();
            shadingFunction.Functions.Add(redToBlueFunc);
            shadingFunction.Functions.Add(blueToGreenFunc);
            shadingFunction.Domain = new double[] { 0, 1 };
            shadingFunction.Encode = new double[] { 0, 1, 0, 1 };

            // Entire shading goes from 0 to 1 (100%).
            // We set the first shading (red->blue) to cover 30% (0 - 0.3) and
            // the second shading to cover 70% (0.3 - 1).
            shadingFunction.Bounds = new double[] { 0.3 };
            // The multistop shading
            PdfAxialShading multiStopShading = new PdfAxialShading();
            multiStopShading.StartPoint = new PdfPoint(25, 420);
            multiStopShading.EndPoint = new PdfPoint(575, 420);
            // The colorspace must match the colors specified in C0 & C1
            multiStopShading.ColorSpace = new PdfRgbColorSpace();
            multiStopShading.Function = shadingFunction;

            // Clip the shading to desired area.
            PdfPath mssArea = new PdfPath();
            mssArea.AddRectangle(25, 420, 550, 30);
            page.Graphics.SaveGraphicsState();
            page.Graphics.SetClip(mssArea);
            page.Graphics.DrawShading(multiStopShading);
            page.Graphics.RestoreGraphicsState();
            page.Graphics.DrawPath(blackPen, lsArea);

            page.Graphics.DrawString("Radial shading", sectionFont, brush, 25, 470);

            PdfRadialShading rs1 = new PdfRadialShading();
            rs1.StartColor = new PdfRgbColor(0, 255, 0);
            rs1.EndColor = new PdfRgbColor(255, 0, 255);
            rs1.StartCircleCenter = new PdfPoint(50, 500);
            rs1.StartCircleRadius = 10;
            rs1.EndCircleCenter = new PdfPoint(500, 570);
            rs1.EndCircleRadius = 100;

            page.Graphics.DrawShading(rs1);

            PdfRadialShading rs2 = new PdfRadialShading();
            rs2.StartColor = new PdfRgbColor(0, 255, 0);
            rs2.EndColor = new PdfRgbColor(255, 0, 255);
            rs2.StartCircleCenter = new PdfPoint(80, 600);
            rs2.StartCircleRadius = 10;
            rs2.EndCircleCenter = new PdfPoint(110, 690);
            rs2.EndCircleRadius = 100;

            page.Graphics.DrawShading(rs2);

            page.Graphics.CompressAndClose();
        }