Skip to content

957011 Added sample code for apply Radial Gradient fill to shapes in a PDF document #168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35707.178 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Multi-color-radial-gradient-fill-in-PDF", "Multi-color-radial-gradient-fill-in-PDF\Multi-color-radial-gradient-fill-in-PDF.csproj", "{6A8258DB-87FA-4361-B464-70774FD0E6AE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6A8258DB-87FA-4361-B464-70774FD0E6AE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Multi-color-radial-gradient-fill-in-PDF</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.Pdf.Net.Core" Version="*" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;

// Create a new PDF document.
PdfDocument document = new PdfDocument();

// Add a page to the document.
PdfPage page = document.Pages.Add();

// Get graphics context of the added page.
PdfGraphics graphics = page.Graphics;

// Define the rectangle dimensions.
float rectWidth = 416;
float rectHeight = 145;

// Calculate the top-center position.
float pageWidth = page.GetClientSize().Width;
float rectX = (pageWidth - rectWidth) / 2; // Centered horizontally
float rectY = 50; // Distance from the top

// Define the rectangle path.
PdfPath path = new PdfPath();
path.AddRectangle(new RectangleF(rectX, rectY, rectWidth, rectHeight));

// Define gradient colors using RGB (byte values from 0 to 255).
List<PdfColor> finalGradientColors = new List<PdfColor>
{
new PdfColor(0, 63, 255), // Blue
new PdfColor(0, 200, 83), // Green
new PdfColor(255, 193, 7), // Amber
new PdfColor(255, 0, 139) // Magenta
};

// Define positions for the gradient colors.
List<float> finalGradientPositions = new List<float> { 0, 0.2f, 0.8f, 1 };

// Create a color blend object for the gradient.
PdfColorBlend gradientColorBlend = new PdfColorBlend
{
Colors = finalGradientColors.ToArray(),
Positions = finalGradientPositions.ToArray()
};

// Calculate the center point and radius for the radial gradient.
PointF center = new PointF(rectX + rectWidth / 2, rectY + rectHeight / 2);
float radius = (float)Math.Sqrt((rectWidth * rectWidth) + (rectHeight * rectHeight)) / 2;

// Create and configure the radial gradient brush.
PdfRadialGradientBrush rectangleGradientBrush = new PdfRadialGradientBrush(
center, 0, center, radius,
finalGradientColors[0],
finalGradientColors[finalGradientColors.Count - 1]
)
{
InterpolationColors = gradientColorBlend
};

// Draw the gradient-filled rectangle.
graphics.DrawPath(rectangleGradientBrush, path);

//Create file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
document.Save(outputFileStream);
}

// Close the document and release resources.
document.Close(true);
Loading