How-to guides/Redaction

Permanently redact text and images from a PDF in Rust

Remove sensitive content from PDF files with black-box redaction. PDFluent removes the underlying text and image data, not just paints over it.

rust
use pdfluent::{PdfDocument, RedactOptions};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut doc = PdfDocument::open("contract.pdf")?;

    doc.redact_text("John Smith", RedactOptions::default())?;
    doc.redact_text("555-867-5309", RedactOptions::default())?;

    doc.save("contract_redacted.pdf")?;
    println!("Redaction complete");
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml. No native dependencies are required.

rust
[dependencies]
pdfluent = "0.9"
2

Open the PDF document

Load the PDF you want to redact. Use open() for files on disk or from_bytes() for in-memory data.

rust
use pdfluent::PdfDocument;

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

Mark text regions for redaction

Call redact_text() with the exact string to remove. PDFluent searches all pages and marks every occurrence. The text is not yet removed at this step.

rust
use pdfluent::RedactOptions;

// Mark specific strings
doc.redact_text("Alice Johnson", RedactOptions::default())?;
doc.redact_text("account: 4111-1111-1111-1111", RedactOptions::default())?;

// Mark a page region by coordinates (points, from bottom-left)
doc.redact_region(0, pdfluent::Rect::new(50.0, 700.0, 300.0, 720.0))?;
4

Configure the redaction fill

The default fill is solid black. You can change the fill colour or add a label such as "REDACTED" over the box.

rust
use pdfluent::{RedactOptions, Color};

let opts = RedactOptions::default()
    .fill_color(Color::black())
    .overlay_text("REDACTED")
    .overlay_font_size(8.0);

doc.redact_text("SSN: 123-45-6789", opts)?;
5

Apply redactions and save

Call apply_redactions() to permanently remove the marked content from the data stream. Then save the file. After this step the content cannot be recovered.

rust
doc.apply_redactions()?;
doc.save("report_redacted.pdf")?;

println!("All redactions applied and saved.");

Notes and tips

  • apply_redactions() removes text from the content stream. Simply drawing a black box does not remove the underlying text.
  • Redaction applies to all pages unless you use redact_text_on_page() to target a specific page index.
  • Scanned PDFs contain images, not text. Use redact_region() with pixel coordinates for those pages.
  • Always verify the output in a PDF viewer before distributing redacted documents.

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