|
| 1 | +using Syncfusion.Pdf.Graphics; |
| 2 | +using Syncfusion.Pdf; |
| 3 | +using Syncfusion.Drawing; |
| 4 | + |
| 5 | +// Create a new PDF document. |
| 6 | +PdfDocument document = new PdfDocument(); |
| 7 | + |
| 8 | +// Add a page to the document. |
| 9 | +PdfPage page = document.Pages.Add(); |
| 10 | + |
| 11 | +// Get graphics context of the added page. |
| 12 | +PdfGraphics graphics = page.Graphics; |
| 13 | + |
| 14 | +// Define the rectangle dimensions. |
| 15 | +float rectWidth = 416; |
| 16 | +float rectHeight = 145; |
| 17 | + |
| 18 | +// Calculate the top-center position. |
| 19 | +float pageWidth = page.GetClientSize().Width; |
| 20 | +float rectX = (pageWidth - rectWidth) / 2; // Centered horizontally |
| 21 | +float rectY = 50; // Distance from the top |
| 22 | + |
| 23 | +// Define the rectangle path. |
| 24 | +PdfPath path = new PdfPath(); |
| 25 | +path.AddRectangle(new RectangleF(rectX, rectY, rectWidth, rectHeight)); |
| 26 | + |
| 27 | +// Define gradient colors. |
| 28 | +List<PdfColor> finalGradientColors = new List<PdfColor> |
| 29 | + { |
| 30 | + new PdfColor(0, 0.247058809f, 1, 0), |
| 31 | + new PdfColor(0, 0.247058809f, 1, 0), |
| 32 | + new PdfColor(1, 0, 0.545454562f, 80), |
| 33 | + new PdfColor(1, 0, 0.545454562f, 80) |
| 34 | + }; |
| 35 | + |
| 36 | +// Define positions for the gradient colors. |
| 37 | +List<float> finalGradientPositions = new List<float> { 0, 0.2f, 0.8f, 1 }; |
| 38 | + |
| 39 | +// Create a color blend object for the gradient. |
| 40 | +PdfColorBlend gradientColorBlend = new PdfColorBlend |
| 41 | +{ |
| 42 | + Colors = finalGradientColors.ToArray(), |
| 43 | + Positions = finalGradientPositions.ToArray() |
| 44 | +}; |
| 45 | + |
| 46 | +// Calculate the center point and radius for the radial gradient. |
| 47 | +PointF center = new PointF(rectX + rectWidth / 2, rectY + rectHeight / 2); |
| 48 | +float radius = (float)Math.Sqrt((rectWidth * rectWidth) + (rectHeight * rectHeight)) / 2; |
| 49 | + |
| 50 | +// Create and configure the radial gradient brush. |
| 51 | +PdfRadialGradientBrush rectangleGradientBrush = new PdfRadialGradientBrush( |
| 52 | + center, 0, center, radius, |
| 53 | + finalGradientColors[0], |
| 54 | + finalGradientColors[finalGradientColors.Count - 1] |
| 55 | +) |
| 56 | +{ |
| 57 | + InterpolationColors = gradientColorBlend |
| 58 | +}; |
| 59 | + |
| 60 | +// Draw the gradient-filled rectangle. |
| 61 | +graphics.DrawPath(rectangleGradientBrush, path); |
| 62 | + |
| 63 | +// Save the PDF document to a file using a file stream. |
| 64 | +using (FileStream fs = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.Write)) |
| 65 | +{ |
| 66 | + document.Save(fs); |
| 67 | +} |
| 68 | + |
| 69 | +// Close the document and release resources. |
| 70 | +document.Close(true); |
0 commit comments