How to fill a shape with a multi-color radial gradient brush in PDF using C#
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. This guide demonstrates how to fill shapes with a multi-color radial gradient brush in a PDF document.
Steps to fill a shape with a multi-color radial gradient brush in PDF document
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference in your console application from Nuget.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to fill a shape with a multi-color radial gradient brush in PDF document.
C#
// 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);
// Save the PDF document to a file using a file stream.
using (FileStream output = new FileStream("Output.pdf", FileMode.Create, FileAccess.Write))
{
document.Save(output);
}
// Close the document and release resources.
document.Close(true);
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to add brushes in a PDF document.
Conclusion
I hope you enjoyed learning on how to fill shapes with a multi-color radial gradient brush in a PDF document.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!