How to Draw Lines and Rectangles on PDFs in C#
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 seamlessly 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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
IronPdf.PdfDocument pdf = IronPdf.PdfDocument.FromFile("input.pdf"); pdf.DrawLine(10, 10, 200, 10, "#FF0000", 2); pdf.SaveAs("output.pdf");Deploy to test on your live environment
Minimal Workflow (5 steps)
- Download IronPDF C# Library from NuGet
- Import the target PDF document or render it from HTML
- Use the
DrawLineMethod for Adding Lines to PDFs - Use the
DrawRectangleMethod for Adding Rectangles - Export the edited PDF document
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
:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-line.csusing 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")For more advanced PDF manipulation features, check out the API Reference which provides comprehensive documentation for all available methods and properties.
What Does the Line Drawing Output Look Like?
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"));
}// 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"));
}IRON VB CONVERTER ERROR developers@ironsoftware.comThis 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.
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.
:path=/static-assets/pdf/content-code-examples/how-to/draw-line-and-rectangle-draw-rectangle.csusing 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")What Does the Rectangle Drawing Output Look Like?
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// 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 fillIRON VB CONVERTER ERROR developers@ironsoftware.comBest 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
}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
}IRON VB CONVERTER ERROR developers@ironsoftware.comGetting 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
How do I draw a line on a PDF using C#?
To draw a line on a PDF in C#, use IronPDF's DrawLine method on a PdfDocument object. Simply specify the start and end coordinates, color (as a hex code), and line width. For example: pdf.DrawLine(10, 10, 200, 10, "#FF0000", 2) creates a red horizontal line.
What parameters does the DrawLine method accept?
IronPDF's DrawLine method accepts several parameters: Page Index (which page to draw on), Start Point coordinates (X, Y), End Point coordinates (X, Y), Width (line thickness in points), and Color (using hex codes or predefined colors).
Can I add rectangles to existing PDF documents?
Yes, you can add rectangles to existing PDFs using IronPDF's DrawRectangle method. This method allows you to specify the rectangle's position, dimensions, border color, fill color, and border thickness to create custom shapes on your PDF pages.
What types of geometric shapes can I create on PDFs?
With IronPDF, you can create lines and rectangles on PDF documents. These shapes can be used to create forms, diagrams, annotations, borders, dividers, and to highlight important sections within your PDFs.
Do I need to create a new PDF to add shapes, or can I modify existing ones?
IronPDF allows you to modify existing PDFs by adding shapes. You can load an existing PDF using PdfDocument.FromFile() and then use the drawing methods to add lines and rectangles without creating a new document from scratch.
How can I control the color of shapes in my PDF?
IronPDF supports color customization using hex color codes (like "#FF0000" for red) or predefined colors. The Color class from the IronDrawing API provides additional options for applying colors to your lines and rectangles.






