How to Draw Lines and Rectangles on PDFs

by Chaknith Bin

Drawing lines and rectangles onto a PDF document refers to the process of adding geometric shapes, specifically lines and rectangles, to the content of a PDF file. This is often done programmatically using a programming language like C# or VB.NET and a library like IronPDF.

When you draw a line, you create a visible line segment with specified starting and ending points. Similarly, when you draw a rectangle, you define a four-sided shape with specified dimensions and position.


C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

Download DLL

Manually install into your project

Draw Line Example

By utilizing the DrawLine method available for the PdfDocument object, you can add lines to an existing PDF. Using the Color class offered by IronDrawing opens up the possibility to apply a line with a color from a HEX color code.

:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-line.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");

// Configure the required parameters
int pageIndex = 0;
var start = new IronSoftware.Drawing.PointF(200,150);
var end = new IronSoftware.Drawing.PointF(1000,150);
int width = 10;
var color = new IronSoftware.Drawing.Color("#000000");

// Draw line on PDF
pdf.DrawLine(pageIndex, start, end, width, color);

pdf.SaveAs("drawLine.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")

' Configure the required parameters
Private pageIndex As Integer = 0
Private start = New IronSoftware.Drawing.PointF(200,150)
Private [end] = New IronSoftware.Drawing.PointF(1000,150)
Private width As Integer = 10
Private color = New IronSoftware.Drawing.Color("#000000")

' Draw line on PDF
pdf.DrawLine(pageIndex, start, [end], width, color)

pdf.SaveAs("drawLine.pdf")
VB   C#

Output PDF

Draw Rectangle Example

To add rectangles to PDFs, use the DrawRectangle method. Once the PDF document is opened or rendered, this method is available for the PdfDocument object. Easily configure the coordinates, width, and height for the rectangle with the RectangleF class offered by IronDrawing.

:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-rectangle.cs
using IronPdf;

ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdf = renderer.RenderHtmlAsPdf("<h1>testing</h1>");

// Configure the required parameters
int pageIndex = 0;
var rectangle = new IronSoftware.Drawing.RectangleF(200, 100, 1000, 100);
var lineColor = new IronSoftware.Drawing.Color("#000000");
var fillColor = new IronSoftware.Drawing.Color("#32AB90");
int lineWidth = 5;

// Draw rectangle on PDF
pdf.DrawRectangle(pageIndex, rectangle, lineColor, fillColor, lineWidth);

pdf.SaveAs("drawRectangle.pdf");
Imports IronPdf

Private renderer As New ChromePdfRenderer()
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf("<h1>testing</h1>")

' Configure the required parameters
Private pageIndex As Integer = 0
Private rectangle = New IronSoftware.Drawing.RectangleF(200, 100, 1000, 100)
Private lineColor = New IronSoftware.Drawing.Color("#000000")
Private fillColor = New IronSoftware.Drawing.Color("#32AB90")
Private lineWidth As Integer = 5

' Draw rectangle on PDF
pdf.DrawRectangle(pageIndex, rectangle, lineColor, fillColor, lineWidth)

pdf.SaveAs("drawRectangle.pdf")
VB   C#

Output PDF

Chaknith Bin

Software Engineer

Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.