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(); }