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
+ // Create PDF graphics object for the page.
12
+ PdfGraphics graphics = page . Graphics ;
13
+
14
+ // Define rectangle dimensions.
15
+ float rectWidth = 150f ;
16
+ float rectHeight = 150f ;
17
+
18
+ // Calculate position to center the rectangle horizontally at the top.
19
+ float pageWidth = page . GetClientSize ( ) . Width ;
20
+ float startX = ( pageWidth - rectWidth ) / 2 ;
21
+ float startY = 60f ; // Positioned near the top
22
+
23
+ // Define the rectangle path.
24
+ PdfPath path = new PdfPath ( ) ;
25
+ path . AddRectangle ( new RectangleF ( startX , startY , rectWidth , rectHeight ) ) ;
26
+
27
+ // Define a smooth gradient color scheme.
28
+ List < PdfColor > gradientColors = new List < PdfColor >
29
+ {
30
+ Color . CornflowerBlue ,
31
+ Color . MediumPurple ,
32
+ Color . DeepPink
33
+ } ;
34
+
35
+ List < float > gradientPositions = new List < float > { 0.0f , 0.5f , 1.0f } ;
36
+
37
+ PdfColorBlend colorBlend = new PdfColorBlend
38
+ {
39
+ Colors = gradientColors . ToArray ( ) ,
40
+ Positions = gradientPositions . ToArray ( )
41
+ } ;
42
+
43
+ // Calculate the radius for the radial gradient.
44
+ float radius = ( float ) Math . Sqrt ( ( rectWidth * rectWidth ) + ( rectHeight * rectHeight ) ) / 2 ;
45
+
46
+ // Create a radial gradient brush centered in the rectangle.
47
+ PointF center = new PointF ( startX + rectWidth / 2 , startY + rectHeight / 2 ) ;
48
+ PdfRadialGradientBrush radialBrush = new PdfRadialGradientBrush ( center , 0 , center , radius , gradientColors [ 0 ] , gradientColors [ gradientColors . Count - 1 ] )
49
+ {
50
+ InterpolationColors = colorBlend
51
+ } ;
52
+
53
+ // Draw the rectangle with the radial gradient fill.
54
+ graphics . DrawPath ( radialBrush , path ) ;
55
+
56
+ // Save the document to file.
57
+ using ( FileStream fileStream = new FileStream ( Path . GetFullPath ( @"Output/Output.pdf" ) , FileMode . Create , FileAccess . ReadWrite ) )
58
+ {
59
+ document . Save ( fileStream ) ;
60
+ }
61
+
62
+ // Close the document.
63
+ document . Close ( true ) ;
0 commit comments