Sometimes HTML/CSS isn't enough—you need to draw directly on PDF pages. Add lines, rectangles, and borders programmatically at exact coordinates for forms, diagrams, or annotations.
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("document.pdf");
// Draw a red line
pdf.DrawLine(new PointF(50, 100), new PointF(500, 100), Color.Red, 2);
// Draw a blue rectangle
pdf.DrawRectangle(new RectangleF(50, 150, 200, 100), Color.Blue, 1);
pdf.SaveAs("annotated.pdf");
IronPDF provides direct drawing methods for lines and rectangles on any PDF page.
Why Draw Shapes Programmatically?
Common use cases:
- Form fields: Draw boxes for signature areas
- Diagrams: Create flowcharts or organizational charts
- Highlighting: Box important sections
- Borders: Add frames around content
- Separators: Horizontal rules between sections
- Annotations: Mark up existing documents
How Do I Draw a Line?
Use DrawLine with start and end points:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("report.pdf");
// Simple horizontal line
pdf.DrawLine(
new PointF(50, 200), // Start point (x, y)
new PointF(550, 200), // End point (x, y)
Color.Black, // Line color
1 // Line width in points
);
pdf.SaveAs("report-with-line.pdf");
Coordinates are in points (72 points = 1 inch), measured from the top-left corner.
How Do I Draw Different Line Styles?
Vary thickness and color:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("document.pdf");
// Thin black line
pdf.DrawLine(new PointF(50, 100), new PointF(550, 100), Color.Black, 0.5f);
// Medium gray line
pdf.DrawLine(new PointF(50, 150), new PointF(550, 150), Color.Gray, 2);
// Thick red line
pdf.DrawLine(new PointF(50, 200), new PointF(550, 200), Color.Red, 4);
// Colored accent line
pdf.DrawLine(new PointF(50, 250), new PointF(550, 250), Color.FromArgb(0, 120, 215), 3);
pdf.SaveAs("styled-lines.pdf");
How Do I Draw Diagonal Lines?
Lines work at any angle:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("chart.pdf");
// Diagonal line (top-left to bottom-right)
pdf.DrawLine(new PointF(100, 100), new PointF(400, 400), Color.Blue, 2);
// Opposite diagonal
pdf.DrawLine(new PointF(400, 100), new PointF(100, 400), Color.Blue, 2);
// Creates an X pattern
pdf.SaveAs("diagonal-lines.pdf");
How Do I Draw a Rectangle?
Use DrawRectangle for boxes:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("form.pdf");
// Draw rectangle outline
pdf.DrawRectangle(
new RectangleF(50, 100, 200, 80), // x, y, width, height
Color.Black, // Border color
1 // Border width
);
pdf.SaveAs("form-with-box.pdf");
The rectangle is positioned by its top-left corner.
How Do I Create a Signature Box?
Standard signature area:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("contract.pdf");
int lastPage = pdf.PageCount - 1;
// Signature box with label line
float boxX = 50;
float boxY = 650;
float boxWidth = 250;
float boxHeight = 60;
// Draw the box
pdf.DrawRectangle(new RectangleF(boxX, boxY, boxWidth, boxHeight), Color.Black, 1, lastPage);
// Draw the signature line
float lineY = boxY + boxHeight - 15;
pdf.DrawLine(new PointF(boxX + 10, lineY), new PointF(boxX + boxWidth - 10, lineY), Color.Black, 0.5f, lastPage);
pdf.SaveAs("contract-signature.pdf");
How Do I Draw on Specific Pages?
Target individual pages by index:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("multipage.pdf");
// Draw on first page (index 0)
pdf.DrawRectangle(new RectangleF(50, 50, 100, 100), Color.Red, 2, pageIndex: 0);
// Draw on second page (index 1)
pdf.DrawRectangle(new RectangleF(50, 50, 100, 100), Color.Blue, 2, pageIndex: 1);
// Draw on last page
int lastPage = pdf.PageCount - 1;
pdf.DrawRectangle(new RectangleF(50, 50, 100, 100), Color.Green, 2, pageIndex: lastPage);
pdf.SaveAs("marked-pages.pdf");
How Do I Create a Border Around Content?
Frame entire pages or sections:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("certificate.pdf");
// Page border with margin
float margin = 30;
float pageWidth = 595; // A4 width in points
float pageHeight = 842; // A4 height in points
var borderRect = new RectangleF(
margin,
margin,
pageWidth - (2 * margin),
pageHeight - (2 * margin)
);
pdf.DrawRectangle(borderRect, Color.DarkBlue, 3);
pdf.SaveAs("bordered-certificate.pdf");
How Do I Draw Multiple Shapes?
Create complex layouts:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("org-chart.pdf");
// Draw organization boxes
var ceoBox = new RectangleF(225, 50, 150, 40);
var managerBox1 = new RectangleF(75, 150, 150, 40);
var managerBox2 = new RectangleF(375, 150, 150, 40);
pdf.DrawRectangle(ceoBox, Color.Navy, 2);
pdf.DrawRectangle(managerBox1, Color.DarkGreen, 2);
pdf.DrawRectangle(managerBox2, Color.DarkGreen, 2);
// Draw connecting lines
pdf.DrawLine(new PointF(300, 90), new PointF(300, 120), Color.Black, 1);
pdf.DrawLine(new PointF(150, 120), new PointF(450, 120), Color.Black, 1);
pdf.DrawLine(new PointF(150, 120), new PointF(150, 150), Color.Black, 1);
pdf.DrawLine(new PointF(450, 120), new PointF(450, 150), Color.Black, 1);
pdf.SaveAs("org-chart-drawn.pdf");
How Do I Use Custom Colors?
Any RGB color works:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("branding.pdf");
// Company brand colors
var brandBlue = Color.FromArgb(0, 82, 147);
var brandOrange = Color.FromArgb(255, 102, 0);
var brandGray = Color.FromArgb(128, 128, 128);
pdf.DrawRectangle(new RectangleF(50, 100, 500, 200), brandBlue, 3);
pdf.DrawLine(new PointF(50, 350), new PointF(550, 350), brandOrange, 2);
// Semi-transparent (if supported)
var semiTransparent = Color.FromArgb(128, 0, 0, 0); // 50% opacity black
pdf.DrawRectangle(new RectangleF(100, 400, 200, 100), semiTransparent, 1);
pdf.SaveAs("branded-document.pdf");
How Do I Draw a Grid?
Create graph paper or tables:
using IronPdf;
using IronPdf.Drawing;
using System.Drawing;
// Install via NuGet: Install-Package IronPdf
var pdf = new PdfDocument("grid-paper.pdf");
float startX = 50;
float startY = 50;
float gridWidth = 500;
float gridHeight = 700;
float cellSize = 20;
// Draw vertical lines
for (float x = startX; x <= startX + gridWidth; x += cellSize)
{
pdf.DrawLine(new PointF(x, startY), new PointF(x, startY + gridHeight), Color.LightGray, 0.5f);
}
// Draw horizontal lines
for (float y = startY; y <= startY + gridHeight; y += cellSize)
{
pdf.DrawLine(new PointF(startX, y), new PointF(startX + gridWidth, y), Color.LightGray, 0.5f);
}
// Draw border
pdf.DrawRectangle(new RectangleF(startX, startY, gridWidth, gridHeight), Color.Black, 1);
pdf.SaveAs("grid.pdf");
Quick Reference
| Method | Parameters |
|---|---|
DrawLine() |
start point, end point, color, width, page index |
DrawRectangle() |
rectangle, color, width, page index |
| Coordinate System | Value |
|---|---|
| Origin | Top-left corner |
| Unit | Points (72 per inch) |
| A4 size | 595 x 842 points |
| Letter size | 612 x 792 points |
| Common Sizes | Points | Millimeters |
|---|---|---|
| 1 inch | 72 | 25.4 |
| 1 cm | ~28.35 | 10 |
| Line width thin | 0.5 | ~0.18 |
| Line width normal | 1 | ~0.35 |
| Line width thick | 3 | ~1.06 |
Direct drawing gives you pixel-precise control over PDF content when HTML and CSS aren't enough.
For more drawing options, see the IronPDF draw line and rectangle documentation.
Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.
Top comments (0)