ZingPDF logo

Guide

How to add text to a PDF page in C#

Load or create the PDF, get the page, then call Page.AddTextAsync(...) with a bounding box and font settings.

The main inputs are the page, the rectangle the text should occupy, and the font settings you want to use. If the text may overflow, pass layout options explicitly.

The shortest working example

If you want to place text onto a page with explicit font options, use the overload that accepts FontOptions.

using ZingPDF;
using ZingPDF.Graphics;
using ZingPDF.Syntax.CommonDataStructures;
using ZingPDF.Text;

using var pdf = Pdf.Create();
var page = await pdf.GetPageAsync(1);

await page.AddTextAsync(
    "Invoice paid",
    Rectangle.FromCoordinates(new Coordinate(72, 720), new Coordinate(260, 760)),
    new FontOptions
    {
        ResourceName = "Helv",
        Size = 18,
        Colour = RGBColour.Black
    });

await pdf.SaveAsync(File.Create("text-added.pdf"));

The rectangle controls where the text is laid out

The bounding box is not just a rough position marker. It is the space ZingPDF uses for layout.

If you want the text in a specific part of the page, define the rectangle for that area instead of treating it like a single point.

Pass layout options when the text may not fit

If the text could be wider than the box, decide that up front instead of relying on whatever happens at the requested size.

using ZingPDF.Text;

await page.AddTextAsync(
    "This sentence is longer than the original box width.",
    Rectangle.FromCoordinates(new Coordinate(72, 680), new Coordinate(240, 720)),
    new FontOptions
    {
        ResourceName = "Helv",
        Size = 24,
        Colour = RGBColour.Black
    },
    new TextLayoutOptions
    {
        Overflow = TextOverflowMode.ShrinkToFit
    });

Use ShrinkToFit when the text must stay inside the box, or choose a different overflow mode if clipping is the better fit for the document.

Switch to a registered font when you need one

The registered-font overload is a better fit when you want a standard font helper or an embedded TrueType font.

Register the font at the document level, then pass the returned PdfFont into AddTextAsync(...).

Need a real font instead of a raw resource name?

Registering fonts makes the write path clearer when you are generating more than a small amount of text.

Read How to use standard PDF fonts in C# or How to embed a TrueType font in a PDF in C#.

Open docs View pricing