How to Draw Lines and Rectangles on PDFs

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 positions.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



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 API Documentation 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")
$vbLabelText   $csharpLabel

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 API Documentation.

: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")
$vbLabelText   $csharpLabel

Output PDF

Frequently Asked Questions

What is this library for creating and editing PDFs in C#?

IronPDF is a C# library that allows developers to generate, edit, and manipulate PDF documents programmatically using .NET languages like C# and VB.NET.

How do you draw a line on a PDF using C#?

To draw a line on a PDF using C#, use the DrawLine method of the PdfDocument object in IronPDF. You need to specify the line color, starting and ending coordinates, and line thickness.

How can you draw a rectangle on a PDF using C#?

To draw a rectangle on a PDF using C#, use the DrawRectangle method of the PdfDocument object in IronPDF. Define the rectangle's color, position, dimensions, and thickness using the RectangleF class.

What is the DrawLine method?

The DrawLine method is a function of the PdfDocument object in IronPDF that allows users to add lines to a PDF by specifying parameters like color, start and end positions, and thickness.

What is the DrawRectangle method?

The DrawRectangle method is a feature of the PdfDocument object in IronPDF that enables users to add rectangles to a PDF. Users can specify the color, position, size, and thickness of the rectangle.

Where can I find the drawing API documentation for this library?

The IronDrawing API Documentation can be found at https://ironsoftware.com/open-source/csharp/drawing/docs/. It provides detailed information on using drawing functionalities available in IronPDF.

How do you define the color of a line or rectangle in this library?

In IronPDF, you can define the color of a line or rectangle using the Color class in conjunction with the ColorTranslator.FromHtml method, where you can specify a HEX color code.

What languages can be used with this PDF library?

IronPDF primarily supports .NET languages such as C# and VB.NET, allowing developers to create and modify PDF documents programmatically.

How can you save a PDF after editing it with this library?

After editing a PDF with IronPDF, you can save it using the SaveAs method of the PdfDocument object, specifying the desired output file path.

Chaknith related to Output PDF
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.