How to apply a radial gradient fill to a rectangle in a PDF document
The Syncfusion Essential® PDF is a comprehensive, high-performance .NET PDF library that enables you to create, read, and edit PDF documents. This article demonstrates how to draw a rectangle with a radial gradient fill in a PDF document.
Steps to draw radial gradient fill to a rectangle in a PDF programmatically
- 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;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
- Use the following code sample in Program.cs to apply a radial gradient fill to a rectangle in a PDF document.
C#
// Create a new PDF document.
PdfDocument document = new PdfDocument();
// Add a page to the document.
PdfPage page = document.Pages.Add();
// Create PDF graphics object for the page.
PdfGraphics graphics = page.Graphics;
// Define rectangle dimensions.
float rectWidth = 150f;
float rectHeight = 150f;
// Calculate position to center the rectangle horizontally at the top.
float pageWidth = page.GetClientSize().Width;
float startX = (pageWidth - rectWidth) / 2;
float startY = 60f; // Positioned near the top
// Define the rectangle path.
PdfPath path = new PdfPath();
path.AddRectangle(new RectangleF(startX, startY, rectWidth, rectHeight));
// Define a smooth gradient color scheme.
List<PdfColor> gradientColors = new List<PdfColor>
{
Color.CornflowerBlue,
Color.MediumPurple,
Color.DeepPink
};
List<float> gradientPositions = new List<float> { 0.0f, 0.5f, 1.0f };
PdfColorBlend colorBlend = new PdfColorBlend
{
Colors = gradientColors.ToArray(),
Positions = gradientPositions.ToArray()
};
// Calculate the radius for the radial gradient.
float radius = (float)Math.Sqrt((rectWidth * rectWidth) + (rectHeight * rectHeight)) / 2;
// Create a radial gradient brush centered in the rectangle.
PointF center = new PointF(startX + rectWidth / 2, startY + rectHeight / 2);
PdfRadialGradientBrush radialBrush = new PdfRadialGradientBrush(center, 0, center, radius, gradientColors[0], gradientColors[gradientColors.Count - 1])
{
InterpolationColors = colorBlend
};
// Draw the rectangle with the radial gradient fill.
graphics.DrawPath(radialBrush, path);
// Save the document to file.
using (FileStream fileStream = new FileStream("Output.pdf", FileMode.Create, FileAccess.ReadWrite))
{
document.Save(fileStream);
}
// Close the document.
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 work with brushes in a PDF document.
Conclusion
I hope you enjoyed learning on how to apply a radial gradient fill to a rectangle 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!