How to draw shapes in PDF using C# and VB.NET in WinForms?
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add shapes like line, curve, path, text, rectangle, pie, arc, bezier, and ellipse with different types of brushes like solid brush, gradient brush, tiling brush, and image brush in the PDF using C# and VB.NET.
Steps to draw shapes in PDF programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports System.Drawing
- Use the following code snippet to draw shapes in the PDF.
C#
//Create a document PdfDocument doc = new PdfDocument(); //Add a new page PdfPage page = doc.Pages.Add(); //Create PDF graphics for the page PdfGraphics graphics = page.Graphics; #region Polygon //Add pen PdfPen pen = new PdfPen(PdfBrushes.Brown, 10f); //Create a gradient brush PdfLinearGradientBrush brush = new PdfLinearGradientBrush(new PointF(10, 100), new PointF(100, 200), new PdfColor(Color.Red), new PdfColor(Color.Green)); //Create polygon points PointF p1 = new PointF(10, 100); PointF p2 = new PointF(10, 200); PointF p3 = new PointF(100, 100); PointF p4 = new PointF(100, 200); PointF p5 = new PointF(55, 150); PointF[] points = { p1, p2, p3, p4, p5 }; //Draw polygon graphics.DrawPolygon(pen, brush, points); #endregion #region Arc pen.Width = 11; pen.LineCap = PdfLineCap.Round; RectangleF rect = new RectangleF(300, 50, 200, 200); //Draw an arc graphics.DrawArc(pen, rect, 0, 90); pen.Color = Color.DarkBlue; rect.X -= 10; graphics.DrawArc(pen, rect, 90, 90); pen.Color = Color.Red; rect.Y -= 10; graphics.DrawArc(pen, rect, 180, 90); pen.Color = Color.DarkCyan; rect.X += 10; graphics.DrawArc(pen, rect, 270, 90); #endregion #region Rectangle pen.Color = Color.Green; pen.Width = 2; //Draw rectangle graphics.DrawRectangle(pen, null, 10, 300, 200, 100); #endregion //Save and close the document doc.Save("Shapes.pdf"); doc.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Shapes.pdf");
VB.NET
'Create a document Dim doc As PdfDocument = New PdfDocument() 'Add a new page Dim page As PdfPage = doc.Pages.Add() 'Create PDF graphics for the page Dim graphics As PdfGraphics = page.Graphics #Region "Polygon" 'Add pen Dim pen As PdfPen = New PdfPen(PdfBrushes.Brown, 10.0F) 'Create a gradient brush Dim brush As PdfLinearGradientBrush = New PdfLinearGradientBrush(New PointF(10, 100), New PointF(100, 200), New PdfColor(Color.Red), New PdfColor(Color.Green)) 'Create polygon points Dim p1 As PointF = New PointF(10, 100) Dim p2 As PointF = New PointF(10, 200) Dim p3 As PointF = New PointF(100, 100) Dim p4 As PointF = New PointF(100, 200) Dim p5 As PointF = New PointF(55, 150) Dim points As PointF() = {p1, p2, p3, p4, p5} 'Draw polygon graphics.DrawPolygon(pen, brush, points) #End Region #Region "Arc" pen.Width = 11 pen.LineCap = PdfLineCap.Round Dim rect As RectangleF = New RectangleF(300, 50, 200, 200) 'Draw an arc graphics.DrawArc(pen, rect, 0, 90) pen.Color = Color.DarkBlue rect.X -= 10 graphics.DrawArc(pen, rect, 90, 90) pen.Color = Color.Red rect.Y -= 10 graphics.DrawArc(pen, rect, 180, 90) pen.Color = Color.DarkCyan rect.X += 10 graphics.DrawArc(pen, rect, 270, 90) #End Region #Region "Rectangle" pen.Color = Color.Green pen.Width = 2 'Draw rectangle graphics.DrawRectangle(pen, Nothing, 10, 300, 200, 100) #End Region 'Save and close the document doc.Save("Shapes.pdf") doc.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer System.Diagnostics.Process.Start("Shapes.pdf")
A complete working sample can be downloaded from DrawShapesSample.zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation, where you can find the options like add a polygon with linear gradient brush and shape with pagination.
An online sample link to drawing the shapes such as Ellipse, Arcs, Polygon, Pie, and Rectangle in the PDF document with gradient and color spaces.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
I hope you enjoyed learning about how to draw shapes in a PDF using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples 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 or feedback portal. We are always happy to assist you!