DEV Community

IronSoftware
IronSoftware

Posted on

Draw Lines and Rectangles on PDF in C#

Our invoices needed visual separators. Horizontal lines between sections. Boxes around totals. Manual design in HTML wasn't pixel-perfect. We needed programmatic control.

Drawing shapes on PDFs solved this. Here's how to add lines, rectangles, and borders.

How Do I Draw Lines on PDFs?

Use DrawLine():

using IronPdf;
using IronSoftware.Drawing;
// Install via NuGet: Install-Package IronPdf

var pdf = PdfDocument.FromFile("document.pdf");

pdf.DrawLine(100, 700, 500, 700, 0, Color.Black, 2);

pdf.SaveAs("lined.pdf");
Enter fullscreen mode Exit fullscreen mode

Draws a horizontal line from (100, 700) to (500, 700) on page 0.

Why Draw Shapes on PDFs?

Visual separation: Divide sections with horizontal rules
Highlighting: Box important information
Forms: Create fillable borders and signature boxes
Tables: Add custom gridlines
Design elements: Underlines, dividers, frames

I use this to create invoice templates with structured layouts.

How Do I Draw Rectangles?

Use DrawRectangle():

pdf.DrawRectangle(100, 500, 400, 200, 0, Color.Blue, 3);
Enter fullscreen mode Exit fullscreen mode

Draws a rectangle:

  • X: 100, Y: 500 (bottom-left corner)
  • Width: 400, Height: 200
  • Page: 0
  • Color: Blue border
  • Thickness: 3 points

Can I Fill Rectangles?

IronPDF's DrawRectangle draws borders. For filled shapes, generate as an image first:

using System.Drawing;

using var canvas = new Bitmap(400, 200);
using var g = Graphics.FromImage(canvas);

g.Clear(Color.LightBlue);
g.DrawRectangle(Pens.Black, 0, 0, 399, 199);

pdf.DrawBitmap(canvas, 100, 500, 0);
Enter fullscreen mode Exit fullscreen mode

Draws a filled blue rectangle with black border.

How Do I Draw Dotted or Dashed Lines?

System.Drawing supports dash patterns. Generate as image:

using var canvas = new Bitmap(400, 50);
using var g = Graphics.FromImage(canvas);

g.Clear(Color.White);

using var pen = new Pen(Color.Black, 2);
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

g.DrawLine(pen, 0, 25, 400, 25);

pdf.DrawBitmap(canvas, 100, 500, 0);
Enter fullscreen mode Exit fullscreen mode

Draws a dashed line.

Can I Draw Diagonal Lines?

Yes, specify different start and end coordinates:

// Diagonal from top-left to bottom-right
pdf.DrawLine(100, 700, 500, 400, 0, Color.Red, 2);
Enter fullscreen mode Exit fullscreen mode

Creates a slanted line.

How Do I Create Table Borders?

Loop to draw grid lines:

int x = 100, y = 600, width = 400, height = 300;
int rows = 5, cols = 4;

// Horizontal lines
for (int i = 0; i <= rows; i++)
{
    int yPos = y + (i * height / rows);
    pdf.DrawLine(x, yPos, x + width, yPos, 0, Color.Black, 1);
}

// Vertical lines
for (int j = 0; j <= cols; j++)
{
    int xPos = x + (j * width / cols);
    pdf.DrawLine(xPos, y, xPos, y + height, 0, Color.Black, 1);
}
Enter fullscreen mode Exit fullscreen mode

Creates a 5x4 table grid.

What Line Thickness Is Best?

1 point: Subtle borders, form fields
2 points: Standard lines, dividers
3-5 points: Emphasis, headers, frames

Experiment for your design.

How Do I Draw Rounded Rectangles?

Use System.Drawing's DrawPath:

using var canvas = new Bitmap(400, 200);
using var g = Graphics.FromImage(canvas);

g.Clear(Color.White);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

using var path = new System.Drawing.Drawing2D.GraphicsPath();
int radius = 20;
var rect = new Rectangle(10, 10, 380, 180);

path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);
path.CloseFigure();

g.DrawPath(Pens.Black, path);

pdf.DrawBitmap(canvas, 100, 500, 0);
Enter fullscreen mode Exit fullscreen mode

Creates rounded corners.

Can I Draw Circles?

Draw as bitmap using DrawEllipse:

using var canvas = new Bitmap(200, 200);
using var g = Graphics.FromImage(canvas);

g.Clear(Color.Transparent);
g.DrawEllipse(Pens.Black, 10, 10, 180, 180);

pdf.DrawBitmap(canvas, 200, 500, 0);
Enter fullscreen mode Exit fullscreen mode

Perfect circle if width equals height.

How Do I Position Shapes Precisely?

Use PDF coordinate system (points from bottom-left):

// Top-left corner of page (letter size: 612x792)
pdf.DrawLine(50, 750, 100, 750, 0, Color.Black, 1);

// Bottom-right corner
pdf.DrawLine(512, 50, 562, 50, 0, Color.Black, 1);
Enter fullscreen mode Exit fullscreen mode

1 point = 1/72 inch.

Can I Add Borders to Entire Pages?

Yes, draw a rectangle matching page dimensions:

for (int i = 0; i < pdf.PageCount; i++)
{
    // Letter size: 612x792 points, 10-point margin
    pdf.DrawRectangle(10, 10, 592, 772, i, Color.Black, 2);
}
Enter fullscreen mode Exit fullscreen mode

Adds border to all pages.

How Do I Draw Signature Boxes?

int x = 100, y = 150;

pdf.DrawRectangle(x, y, 300, 60, 0, Color.Black, 1);
pdf.DrawText("Signature:", x, y + 65, 0);
pdf.DrawLine(x + 10, y + 10, x + 290, y + 10, 0, Color.Gray, 1);
Enter fullscreen mode Exit fullscreen mode

Creates a signature field with label and baseline.

What's the Performance Impact?

Single line: ~5-10ms
Complex shapes: ~20-50ms
Many shapes: Process in batch for efficiency

For complex graphics, consider generating once and reusing:

var template = PdfDocument.FromFile("template.pdf");
// Add lines/shapes once
template.SaveAs("stamped-template.pdf");

// Reuse for many documents
Enter fullscreen mode Exit fullscreen mode

Can I Draw Over Text?

Yes, lines and shapes draw on top of existing content:

// Strike through text
pdf.DrawLine(100, 500, 400, 500, 0, Color.Red, 2);
Enter fullscreen mode Exit fullscreen mode

Perfect for redactions or highlights.

How Do I Draw Multiple Shapes?

Chain method calls or loop:

// Frame an important section
pdf.DrawRectangle(90, 490, 420, 220, 0, Color.Red, 3);
pdf.DrawLine(95, 705, 505, 705, 0, Color.Red, 1);
pdf.DrawText("IMPORTANT", 100, 710, 0);
Enter fullscreen mode Exit fullscreen mode

Can I Use Transparent Colors?

IronDrawing supports alpha channels:

var semiTransparent = Color.FromArgb(128, 255, 0, 0); // 50% red

pdf.DrawRectangle(100, 500, 400, 200, 0, semiTransparent, 5);
Enter fullscreen mode Exit fullscreen mode

Semi-transparent overlay effect.

How Do I Align Shapes with Text?

Calculate text width and position shapes accordingly. Or use fixed coordinates for consistent layouts:

int labelX = 100, labelY = 500;

pdf.DrawText("Total:", labelX, labelY, 0);
pdf.DrawLine(labelX + 50, labelY - 5, labelX + 200, labelY - 5, 0, Color.Black, 1);
Enter fullscreen mode Exit fullscreen mode

Underlines the value field.


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)