How-to guides/Content Editing

Overlay text on a PDF page in Rust

Add text at a specific position on any PDF page. Useful for stamps, approval marks, page numbers, and annotations.

rust
use pdfluent::{PdfDocument, TextOptions, Color};

fn main() -> pdfluent::Result<()> {
    let mut doc = PdfDocument::open("document.pdf")?;
    let mut page = doc.page_mut(0)?;

    page.add_text(
        "APPROVED",
        TextOptions {
            x: 50.0,
            y: 750.0,
            font_size: 36.0,
            color: Color::rgb(0, 128, 0),
            ..TextOptions::default()
        },
    )?;

    doc.save("document-stamped.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to Cargo.toml

Text overlay is part of the base crate.

rust
# Cargo.toml
[dependencies]
pdfluent = "0.9"
2

Open the document and access a page

page_mut(n) gives you a mutable reference to page n. Pages are zero-indexed.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("document.pdf")?;
let mut page = doc.page_mut(0)?;

println!("Page size: {}x{} pt", page.width(), page.height());
3

Add text with default font

Use TextOptions to set position, size, and color. The default font is Helvetica (one of the 14 PDF base fonts, no embedding required).

rust
use pdfluent::{TextOptions, Color};

page.add_text(
    "APPROVED",
    TextOptions {
        x: 50.0,
        y: 750.0,
        font_size: 36.0,
        color: Color::rgb(0, 128, 0),
        ..TextOptions::default()
    },
)?;
4

Use a custom embedded font

Load a TTF or OTF font from disk and embed it in the document. Embedded fonts are required for non-Latin scripts and for precise rendering across all PDF viewers.

rust
use pdfluent::Font;

let font = Font::from_file("fonts/Inter-Regular.ttf")?;
let font_ref = doc.embed_font(font)?;

let mut page = doc.page_mut(0)?;

page.add_text(
    "Invoice Total: EUR 1,234.56",
    TextOptions {
        x: 50.0,
        y: 200.0,
        font_size: 12.0,
        font: Some(font_ref),
        color: Color::black(),
        ..TextOptions::default()
    },
)?;
5

Add a rotated watermark text

Set rotation_degrees on TextOptions to rotate the text. 45 degrees is a common watermark angle.

rust
page.add_text(
    "DRAFT",
    TextOptions {
        x: 200.0,
        y: 300.0,
        font_size: 72.0,
        color: Color::rgba(200, 0, 0, 80), // semi-transparent red
        rotation_degrees: 45.0,
        ..TextOptions::default()
    },
)?;

doc.save("document-watermarked.pdf")?;

Notes and tips

  • PDF coordinates start at bottom-left. y: 750.0 on an A4 page (842 pt tall) places the text baseline 750 pt from the bottom, which is near the top.
  • The 14 standard PDF fonts (Helvetica, Times-Roman, Courier, and variants) do not need to be embedded. All other fonts must be embedded for reliable rendering.
  • Text added with add_text() is added on top of existing content. It is selectable and searchable. Use page.add_text_as_image() to burn text as a raster if you want to prevent selection.
  • For multi-line text blocks, use page.add_text_block() which handles line wrapping within a bounding box.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions