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() -> pdfluent::Result<()> {
    let mut doc = PdfDocument::open("contract.pdf")?;
    doc.redact("CONFIDENTIAL", RedactOptions::new())?;
    doc.save("redacted.pdf")?;
    Ok(())
}

Step by step

1

Add PDFluent to your project

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

rust
[dependencies]
pdfluent = "1.0.0-beta.8"
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;

// Remove specific strings (search + redact across all pages)
doc.redact("Alice Johnson", RedactOptions::new())?;
doc.redact("account: 4111-1111-1111-1111", RedactOptions::new())?;

// Redact a page region by coordinates [x1, y1, x2, y2] (points, from bottom-left)
doc.redact_region(0, [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;

// Scope a redaction to specific pages, case-insensitive
let opts = RedactOptions::new().case_sensitive(false).on_pages(&[0, 1]);

doc.redact("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
// redact() / redact_region() apply in place — write the sanitised file
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