How do you insert a shape into a PDF file? In this page, you will learn how to use XsPDF .NET PDF library to add and insert shapes into a PDF file. Most commonly used shapes are supported, including Line, Rectangle, RoundedRectangle, Ellipse, Polygon, Curve, ClosedCurve, Arc, Bezier, and more. If you are looking for a tool to draw and add shapes to PDF, then it is worth of trying XsPDF .NET PDF library.
On this guide page, we only take two shapes (line and rectangle) as examples of how to add shapes to PDF file page in C#. You may directly download a free trial to test and see more C# coding examples.
Add a Line to PDF Page in C#
This example shows how to draw a line graphics and this shape to a PDF page.
// Create a new PDF document. PdfDocument document = new PdfDocument(); // Create a page. var page = document.AddPage(); // Get current page graphics var gfx = XGraphics.FromPdfPage(page); // Define 2 points var point1 = new XPoint(50, 50); var point2 = new XPoint(100, 100); // Define pen with red color, width and dash style var pen = new XPen(XColors.Red); pen.Width = 3; pen.DashStyle = XDashStyle.DashDot; // Add line graphics to page gfx.DrawLine(pen, point1, point2); document.Save("line.pdf");
Add a Rectangle to PDF Page in C#
This example shows how to draw a stroke rectangle and add it to a PDF page. Furthermore, you can draw and insert a filled rectangle into PDF page.
// Create a new PDF document. PdfDocument document = new PdfDocument(); // Create a page. var page = document.AddPage(); // Get current page graphics var gfx = XGraphics.FromPdfPage(page); // Define a rectangle var rect = new XRect(50, 50, 100, 100); // Define pen with transparent color var pen = new XPen(XColor.FromArgb(128, 255, 0, 0)); var brush = new XSolidBrush(XColors.Blue); // Add stroke rectangle to page gfx.DrawRectangle(pen, rect); // Add filled rectangle to page //gfx.DrawRectangle(brush, rect); document.Save("rectangle.pdf");