How-to guides/Content Editing

Find and replace text in a PDF in Rust

Replace placeholder text, update document dates, or redact strings across all pages of a PDF.

rust
use pdfluent::{PdfDocument, TextReplacement};

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

    doc.replace_text_all(&[
        TextReplacement::exact("{{CUSTOMER_NAME}}", "Acme Corp"),
        TextReplacement::exact("{{INVOICE_DATE}}", "2024-04-01"),
        TextReplacement::exact("{{TOTAL}}", "EUR 4,200.00"),
    ])?;

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

Step by step

1

Add PDFluent to Cargo.toml

Text replacement is in the base crate.

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

Open the source document

The source is typically a template PDF with placeholder strings. Load it as normal.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("template.pdf")?;
3

Replace exact placeholder strings

TextReplacement::exact() matches the literal string on any page. Matching is case-sensitive by default.

rust
use pdfluent::TextReplacement;

doc.replace_text_all(&[
    TextReplacement::exact("{{CUSTOMER_NAME}}", "Acme Corp"),
    TextReplacement::exact("{{INVOICE_DATE}}", "2024-04-01"),
    TextReplacement::exact("{{TOTAL}}", "EUR 4,200.00"),
])?;
4

Replace text using a regex pattern

TextReplacement::regex() accepts any regex pattern. Capture groups are supported in the replacement string.

rust
use pdfluent::TextReplacement;

// Replace phone numbers with a redacted placeholder
doc.replace_text_all(&[
    TextReplacement::regex(
        r"\+?\d[\d\s\-]{8,14}\d",
        "[PHONE REDACTED]",
    )?,
    // Update year in date strings
    TextReplacement::regex(
        r"2023-(\d{2}-\d{2})",
        "2024-$1",
    )?,
])?;
5

Replace text on a single page

Use page_mut(n).replace_text() to limit replacement to one page.

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

let count = page.replace_text(
    TextReplacement::exact("DRAFT", "FINAL"),
)?;

println!("Replaced {} occurrence(s) on page 1", count);
doc.save("invoice-final.pdf")?;

Notes and tips

  • Text in PDFs is stored as glyph sequences in content streams, not as plain strings. PDFluent reconstructs words by analyzing glyph positions before matching. Hyphenated text at line breaks may not match a single-word pattern.
  • Replacement text must use the same font and encoding as the original text. PDFluent matches the font of the first character in the matched range and re-encodes the replacement accordingly.
  • Replacing text that spans multiple words with a shorter string may leave visual gaps. Use TextReplacementOptions::adjust_spacing(true) to redistribute the space.
  • For full content redaction (permanent removal), combine replace_text with page.flatten() to prevent recovery from the content stream.

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