Add text at a specific position on any PDF page. Useful for stamps, approval marks, page numbers, and annotations.
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(())
}Text overlay is part of the base crate.
# Cargo.toml
[dependencies]
pdfluent = "0.9"page_mut(n) gives you a mutable reference to page n. Pages are zero-indexed.
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());Use TextOptions to set position, size, and color. The default font is Helvetica (one of the 14 PDF base fonts, no embedding required).
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()
},
)?;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.
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()
},
)?;Set rotation_degrees on TextOptions to rotate the text. 45 degrees is a common watermark angle.
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")?;No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.