IRONSOFTWAREHOME

How to Draw Lines and Rectangles on PDFs in C#

Curtis Chau
Curtis Chau
Updated: August 3, 2026

To draw lines and rectangles on PDFs in C#, use IronPDF's DrawLine and DrawRectangle methods on a PdfDocument object, specifying coordinates, colors, and dimensions to add professional geometric shapes programmatically.

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. These drawing capabilities are essential for creating forms, diagrams, annotations, and highlighting important sections in PDF documents. IronPDF's drawing features integrate with its other PDF editing capabilities, allowing developers to enhance existing PDFs or create entirely new documents with custom graphics.

Quickstart: Draw Lines and Rectangles with IronPDF

Add lines and rectangles to your PDF documents using IronPDF. This guide demonstrates how to use the DrawLine method for lines and the DrawRectangle method for rectangles. With just a few lines of code, you can create dynamic graphical elements in your PDFs, adding professional-quality visuals to your applications.

  1. 1Install IronPDF with NuGet Package Manager

    PM > Install-Package IronPdf

  2. 2Copy and run this code snippet.

    IronPdf.PdfDocument pdf = IronPdf.PdfDocument.FromFile("input.pdf");
    var start = new IronSoftware.Drawing.PointF(10, 10);
    var end = new IronSoftware.Drawing.PointF(200, 10);
    pdf.DrawLine(0, start, end, 2, new IronSoftware.Drawing.Color("#FF0000"));
    pdf.SaveAs("output.pdf");
    C#
  3. 3Deploy to test on your live environment

    Start using IronPDF in your project today with a free trial
    arrow pointer

How Do I Draw Lines on PDFs in C#?

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. This feature allows you to create underlines, dividers, borders, or custom diagrams directly within your PDF documents.

The DrawLine method accepts several parameters that give you precise control over the appearance of your lines:

  • Page Index: Specifies which page to draw on (zero-based indexing)
  • Start Point: The beginning coordinates (X, Y)
  • End Point: The ending coordinates (X, Y)
  • Width: The thickness in points
  • Color: The line color using hex codes or predefined colors
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");

For more advanced PDF manipulation features, check out the API Reference which provides comprehensive documentation for all available methods and properties.

Output

The line renders between the specified start and end points, drawn in the color set by the hex code.

Advanced Line Drawing Techniques

When working with lines in PDFs, you may want to create more complex patterns or designs. Here's an example of drawing multiple lines to create a grid pattern:

// Create a grid pattern with horizontal and vertical lines
for (int i = 0; i < 5; i++)
{
    // Draw horizontal lines
    var horizontalStart = new IronSoftware.Drawing.PointF(100, 100 + (i * 100));
    var horizontalEnd = new IronSoftware.Drawing.PointF(500, 100 + (i * 100));
    pdf.DrawLine(0, horizontalStart, horizontalEnd, 2, new IronSoftware.Drawing.Color("#0000FF"));
    
    // Draw vertical lines
    var verticalStart = new IronSoftware.Drawing.PointF(100 + (i * 100), 100);
    var verticalEnd = new IronSoftware.Drawing.PointF(100 + (i * 100), 500);
    pdf.DrawLine(0, verticalStart, verticalEnd, 2, new IronSoftware.Drawing.Color("#0000FF"));
}

Output

The loop draws a five by five grid of blue lines across the page.

This technique is particularly useful when creating forms or structured layouts in your PDFs. For more information on creating forms, visit our guide on creating PDF forms. To read the stroke style (width, cap, join, and dash pattern) of lines already present in a PDF, see how to distinguish CAD features by stroke style.

How Do I Draw Rectangles on PDFs in C#?

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. Configure the coordinates, width, and height for the rectangle with the RectangleF class offered by IronDrawing API Documentation.

Rectangles are versatile shapes that can be used for various purposes in PDF documents:

  • Creating borders around important content
  • Highlighting sections of text or images
  • Building form fields and checkboxes
  • Designing headers and footers
  • Creating visual separators between sections

The DrawRectangle method provides options for both outline and fill colors, allowing you to create either outlined rectangles, filled rectangles, or a combination of both. This flexibility makes it ideal for custom watermarking and other visual enhancements.

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");

Output

The rectangle renders at the given position with its outline and fill colors applied.

Creating Complex Layouts with Rectangles

You can combine rectangles with other drawing features to create sophisticated layouts. Here's an example that creates a business card template:

// Create a business card template
var cardBorder = new IronSoftware.Drawing.RectangleF(50, 50, 350, 200);
var logoArea = new IronSoftware.Drawing.RectangleF(60, 60, 80, 80);
var textArea = new IronSoftware.Drawing.RectangleF(150, 60, 240, 180);

// Draw the main card border
pdf.DrawRectangle(0, cardBorder, new IronSoftware.Drawing.Color("#000000"), 
                 new IronSoftware.Drawing.Color("#FFFFFF"), 3);

// Draw logo area with light gray background
pdf.DrawRectangle(0, logoArea, new IronSoftware.Drawing.Color("#666666"), 
                 new IronSoftware.Drawing.Color("#F0F0F0"), 1);

// Draw text area border
pdf.DrawRectangle(0, textArea, new IronSoftware.Drawing.Color("#CCCCCC"), 
                 null, 1); // null for no fill

Output

The three rectangles form a business-card layout where an outer border frames a filled logo box in one corner while an unfilled text-area block sits beside it.

My favorite library of this kind is IronPDF. It allows for fast and efficient manipulation of PDF files. It also has many valuable features, like exporting to PDF/A format and digitally signing PDF documents.

Milan Jovanovic

Microsoft MVP

View case study

IronOCR means we can save $40,000 annually from manual processing, while enhancing productivity and freeing up resources for high-impact tasks. I would highly recommend it.

Brent Matzelle

Chief Technology Officer, OPYN

View case study

The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.

David Jones

Lead Software Engineer, Agorus Build

View case study

Best Practices and Tips

When working with lines and rectangles in PDFs, consider these best practices:

Coordinate System Understanding

The PDF coordinate system starts from the bottom-left corner of the page, with X increasing to the right and Y increasing upward. This differs from many screen-based coordinate systems. Understanding this is crucial for accurate positioning. For more details on page layout, see our guide on custom margins.

Performance Considerations

When drawing multiple shapes, batch operations whenever possible. Instead of saving the PDF after each shape, draw all shapes first and then save once. This approach is especially important when working with large PDF files.

Color Selection

Use consistent color schemes throughout your document. Consider accessibility by ensuring sufficient contrast between line/fill colors and the background. The IronDrawing library supports various color formats including hex codes, RGB values, and named colors.

Integration with Other Features

Drawing operations work well with other IronPDF features. You can:

  • Draw on existing PDFs loaded from files
  • Add shapes to PDFs generated from HTML
  • Combine drawing with text and image stamping
  • Use drawing with page orientation settings

For examples of these integrations, see our guides on creating new PDFs, stamp text image, and page orientation rotation.

Error Handling

Always implement proper error handling when drawing on PDFs:

try 
{
    pdf.DrawLine(pageIndex, start, end, width, color);
    pdf.DrawRectangle(pageIndex, rectangle, lineColor, fillColor, lineWidth);
    pdf.SaveAs("output.pdf");
}
catch (Exception ex)
{
    Console.WriteLine($"Error drawing on PDF: {ex.Message}");
    // Handle the error appropriately
}

Getting Started

To begin using IronPDF's drawing capabilities in your project, follow our installation overview or check out the quickstart guide for a comprehensive introduction to IronPDF.

For more advanced drawing operations, including adding text and bitmaps to your PDFs, explore our guide on drawing text and bitmaps. These features, combined with line and rectangle drawing, provide a complete toolkit for PDF customization and enhancement.

Frequently Asked Questions

What are the basic methods to draw shapes on PDFs using C#?

To draw shapes on PDFs using C#, IronPDF provides `DrawLine` and `DrawRectangle` methods. These methods allow you to add lines and rectangles by specifying coordinates, colors, and dimensions within a `PdfDocument` object.

How do I add a line to a PDF document using IronPDF?

You can add a line to a PDF document using the `DrawLine` method of the `PdfDocument` object in IronPDF. You need to specify parameters like the page index, start and end points, width, and color.

Can I customize the appearance of lines when drawing them on PDFs?

Yes, IronPDF allows you to customize lines by specifying their width and color using the `DrawLine` method. You can use HEX color codes or predefined colors to style the lines.

What are the benefits of drawing rectangles on PDFs with IronPDF?

Drawing rectangles on PDFs with IronPDF allows you to create borders, highlight sections, design form fields, and construct layouts. The `DrawRectangle` method supports options for both outlined and filled rectangles.

How does IronPDF handle color formats for drawing operations?

IronPDF supports various color formats, including HEX codes, RGB values, and named colors through the IronDrawing library, enabling detailed control over the appearance of your PDF drawings.

What is the importance of understanding the PDF coordinate system in IronPDF?

Understanding the PDF coordinate system is crucial because it starts from the bottom-left corner, unlike many screen-based systems. This understanding ensures accurate positioning of shapes when drawing on PDFs.

How can performance be optimized when drawing multiple shapes on a PDF?

For better performance, batch drawing operations by adding all shapes before saving the PDF. This minimizes the saving process, especially useful with large PDF files.

Does IronPDF allow integration of drawing features with other PDF functionalities?

Yes, IronPDF's drawing features integrate with other functionalities such as PDF editing, HTML rendering, and text or image stamping, providing a versatile toolset for PDF customization.

What error handling strategies should be used when drawing on PDFs with IronPDF?

Implementing proper error handling is important. Wrap drawing operations in try-catch blocks to handle exceptions and ensure error messages are appropriately logged or displayed.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,105,021Version:2026.9just released

Get your FREE

30-day Trial Key instantly.

bullet_checkedNo credit card or account creation required
bullet_testTest in production
without watermarks
bullet_calendar30 days fully
functional product
bullet_support24/5 technical
support during trial
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999