Recursive AI

Add text at coordinates in an existing PDF in C#

OnePDF is a free, open-source PDF library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Terminal
dotnet add package RecursiveAI.OnePdf

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OnePdf;
using var doc = PdfDocument.Open("input.pdf",
    new PdfOpenOptions { AllowEditing = true });
using var ink = doc.Pages[0].BeginContent();
var font = doc.AddStandardFont("Helvetica");
ink.SetFont(font, 12);
ink.DrawText("Reviewed", 72, 676);
ink.Commit();
doc.Save("reviewed.pdf");
Console.WriteLine("Saved reviewed.pdf");

On this page, x and y are in points (1/72 in) from the bottom-left corner, with y growing upward.

Writes reviewed.pdf with “Reviewed” on page 1.

Text
Saved reviewed.pdf

Unzip the example, then dotnet run.

Use input.pdf from the ZIP. It has three unrotated pages. Each says Review this figure in 12-point Helvetica at (72, 700). The new line starts 24 points below it, at (72, 676).

The full program

The program commits the new text to the first page, saves a copy, then reopens it and extracts the page text.

C#
using OnePdf;
using var doc = PdfDocument.Open("input.pdf",
    new PdfOpenOptions { AllowEditing = true });
using var ink = doc.Pages[0].BeginContent();
var font = doc.AddStandardFont("Helvetica");
ink.SetFont(font, 12);
ink.DrawText("Reviewed", 72, 676);
ink.Commit();
doc.Save("reviewed.pdf");
 
using var saved = PdfDocument.Open("reviewed.pdf");
Console.WriteLine(saved.ExtractText()[0].Text.Trim());

dotnet run prints:

Text
Review this figure
 
Reviewed

If you dispose the builder without calling Commit(), the new text is discarded.

Variations

To make the added text blue, set the fill color before calling DrawText:

C#
ink.SetFillRgb(0, 0, 0.65);
ink.DrawText("Reviewed", 72, 676);

The short answer

Open the PDF with AllowEditing = true. On the page you want to edit, call BeginContent(), select a font and call DrawText(text, x, y). The coordinates identify the start of the baseline, not the upper-left corner of the letters. Commit the builder and save to a new file.

How do I add text to an existing PDF without replacing the old text?

BeginContent() defaults to an overlay. It writes a separate stream after the existing content, so the original text remains in the file. If the two lines overlap, the new text paints on top; it doesn’t push the old text down or reflow the page. This is page content, not a text field a reader can fill in. In the example, extraction from the saved file returns the original line and the new one. If you want to place content behind the original page instead, pass PdfContentPlacement.Underlay to BeginContent(); an opaque background on that page can hide the new line.

How do PDF coordinates work when placing text?

On the sample, x grows to the right, y grows upward, and the lower-left corner is (0, 0). One unit is 1/72 inch. The second line uses the same x as the first but a smaller y so it lands below it. If a position comes from a top-left screen coordinate, don’t pass it straight to DrawText: the screen may use pixels and a downward y-axis. Check the PDF’s media box, visible crop, rotation and user unit before doing that conversion. The content creation guide explains that drawing uses the page’s default user space; the builder does not adjust the coordinates for the crop box or page rotation.

How do I embed a font for the added text?

The sample uses standard Helvetica and doesn’t embed a font. AddStandardFont uses WinAnsi for Helvetica, so characters outside that encoding are written as ? and reported in doc.Diagnostics. To embed a font, give doc.AddFont the bytes of a licensed font file that covers the characters you need, and pass its return value to SetFont. Embedding doesn’t add script shaping: DrawText itself doesn’t perform kerning, ligatures or bidirectional reordering.

All onepdf recipes