Skip to content

Commit babc005

Browse files
committed
957011 Added sample code for apply Radial Gradient fill to shapes in a PDF document
1 parent 794876a commit babc005

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35707.178 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apply-Radial-Gradient-fill-to-shapes", "Apply-Radial-Gradient-fill-to-shapes\Apply-Radial-Gradient-fill-to-shapes.csproj", "{6A8258DB-87FA-4361-B464-70774FD0E6AE}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Apply_Radial_Gradient_fill_to_shapes</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

Shapes/Apply-Radial-Gradient-fill-to-shapes/.NET/Apply-Radial-Gradient-fill-to-shapes/Output/gitkeep.txt

Whitespace-only changes.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

Comments
 (0)