Skip to content

Commit 45d6535

Browse files
committed
apply a radial gradient fill to a rectangle in a PDF Added sample code for apply a radial gradient fill to a rectangle in a PDF document
1 parent f80debc commit 45d6535

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-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}") = "Radial-gradient-rectangle-in-PDF", "Radial-gradient-rectangle-in-PDF\Radial-gradient-rectangle-in-PDF.csproj", "{D570FDA5-9A74-4EF2-879A-BC4DEE988669}"
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+
{D570FDA5-9A74-4EF2-879A-BC4DEE988669}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{D570FDA5-9A74-4EF2-879A-BC4DEE988669}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{D570FDA5-9A74-4EF2-879A-BC4DEE988669}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{D570FDA5-9A74-4EF2-879A-BC4DEE988669}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Brushes/Radial-gradient-rectangle-in-PDF/.NET/Radial-gradient-rectangle-in-PDF/Output/gitkeep.txt

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

0 commit comments

Comments
 (0)