DEV Community

IronSoftware
IronSoftware

Posted on

Draw Text and Bitmaps on PDF in C#

Our invoices needed "PAID" stamps. Dynamically generated. Red text. Rotated 45 degrees. Overlaid on existing PDFs. Manual editing wasn't scalable.

Drawing on PDFs solved this. Here's how to add text and images to existing documents.

How Do I Draw Text on a PDF?

Use DrawText():

using IronPdf;
// Install via NuGet: Install-Package IronPdf

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

pdf.DrawText("PAID", 300, 400, 0); // X=300, Y=400, Page=0

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

Adds text at specified coordinates.

Why Draw on PDFs?

Stamps: "DRAFT", "CONFIDENTIAL", "APPROVED"
Watermarks: Company logos, security markings
Annotations: Dynamic notes, timestamps
Branding: Add logos to generated documents

I use this to stamp approval dates on contracts.

How Do I Position Text?

Coordinates are in points from bottom-left:

pdf.DrawText("Top Left", 50, 750, 0);
pdf.DrawText("Bottom Right", 450, 50, 0);
Enter fullscreen mode Exit fullscreen mode

X: Left to right (0 = left edge)
Y: Bottom to top (0 = bottom edge)
Page: Zero-indexed page number

Can I Style the Text?

Yes, specify font and size:

pdf.DrawText("Bold Text", 100, 500, 0,
    font: StandardFonts.HelveticaBold,
    fontSize: 24);
Enter fullscreen mode Exit fullscreen mode

Available fonts: Courier, Helvetica, TimesRoman, Symbol, ZapfDingbats.

How Do I Rotate Text?

Set rotation angle:

pdf.DrawText("DIAGONAL", 300, 400, 0, rotation: 45);
Enter fullscreen mode Exit fullscreen mode

Rotates clockwise in degrees.

Can I Add Watermarks?

Yes, draw semi-transparent text across the page:

for (int i = 0; i < pdf.PageCount; i++)
{
    pdf.DrawText("CONFIDENTIAL", 200, 400, i,
        font: StandardFonts.HelveticaBold,
        fontSize: 72,
        rotation: 45);
}
Enter fullscreen mode Exit fullscreen mode

Applies watermark to all pages.

How Do I Draw Images?

Use DrawBitmap():

using System.Drawing;

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

using var logo = new Bitmap("company-logo.png");

pdf.DrawBitmap(logo, 450, 700, 0);

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

Adds image at specified location.

Can I Resize Images?

Yes, specify dimensions:

pdf.DrawBitmap(logo, 450, 700, 0,
    width: 100,
    height: 50);
Enter fullscreen mode Exit fullscreen mode

Image scales to fit dimensions.

How Do I Add Transparent Images?

PNG images with transparency preserve their alpha channel:

using var watermark = new Bitmap("transparent-watermark.png");

pdf.DrawBitmap(watermark, 200, 300, 0);
Enter fullscreen mode Exit fullscreen mode

Transparency maintained in the PDF.

Can I Draw on Multiple Pages?

Yes, loop through pages:

for (int i = 0; i < pdf.PageCount; i++)
{
    pdf.DrawText($"Page {i + 1}", 50, 50, i);
}
Enter fullscreen mode Exit fullscreen mode

Adds page numbers to every page.

How Do I Load Images from URLs?

Download first, then draw:

using var httpClient = new HttpClient();
var imageBytes = await httpClient.GetByteArrayAsync("https://example.com/logo.png");

using var ms = new MemoryStream(imageBytes);
using var bitmap = new Bitmap(ms);

pdf.DrawBitmap(bitmap, 100, 600, 0);
Enter fullscreen mode Exit fullscreen mode

Can I Draw Custom Graphics?

For advanced graphics (lines, shapes), generate them as images first:

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

g.Clear(Color.White);
g.DrawRectangle(Pens.Black, 10, 10, 180, 80);
g.DrawString("Custom", new Font("Arial", 20), Brushes.Red, 50, 35);

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

System.Drawing creates the graphic, then you draw it on the PDF.

How Do I Add Timestamps?

var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

pdf.DrawText($"Signed: {timestamp}", 400, 50, 0,
    font: StandardFonts.Courier,
    fontSize: 10);
Enter fullscreen mode Exit fullscreen mode

Dynamic text based on current time.

Can I Draw from Base64 Images?

Yes, decode first:

var base64 = "iVBORw0KGgoAAAANSUhE...";
var imageBytes = Convert.FromBase64String(base64);

using var ms = new MemoryStream(imageBytes);
using var bitmap = new Bitmap(ms);

pdf.DrawBitmap(bitmap, 300, 600, 0);
Enter fullscreen mode Exit fullscreen mode

How Do I Handle Large Images?

Resize before drawing to reduce PDF size:

using var original = new Bitmap("large-photo.jpg");

int maxWidth = 400;
int maxHeight = 300;

using var resized = new Bitmap(original, new Size(maxWidth, maxHeight));

pdf.DrawBitmap(resized, 100, 400, 0);
Enter fullscreen mode Exit fullscreen mode

What Coordinate System Is Used?

PDF uses points (1/72 inch) from bottom-left origin:

  • Standard letter page: 612 x 792 points
  • A4 page: 595 x 842 points
  • Y increases upward (unlike screen coordinates)

Can I Draw Barcodes or QR Codes?

Generate as images, then draw:

// Using a barcode library like IronBarcode
var barcode = BarcodeWriter.CreateBarcode("12345", BarcodeEncoding.Code128);
var barcodeImage = barcode.ToBitmap();

pdf.DrawBitmap(barcodeImage, 50, 100, 0);
Enter fullscreen mode Exit fullscreen mode

How Do I Draw on Password-Protected PDFs?

Open with password first:

var pdf = PdfDocument.FromFile("protected.pdf", "password123");

pdf.DrawText("APPROVED", 300, 500, 0);

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

What's the Performance Impact?

Drawing text: ~10-30ms per operation
Drawing images: ~50-150ms depending on size

For batch stamping, process PDFs in parallel:

Parallel.ForEach(files, file =>
{
    var pdf = PdfDocument.FromFile(file);
    pdf.DrawText("DRAFT", 300, 400, 0);
    pdf.SaveAs($"stamped/{Path.GetFileName(file)}");
});
Enter fullscreen mode Exit fullscreen mode

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)