Remove sensitive content from PDF files with black-box redaction. PDFluent removes the underlying text and image data, not just paints over it.
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(())
}Add the pdfluent crate to Cargo.toml. No native dependencies are required.
[dependencies]
pdfluent = "1.0.0-beta.8"Load the PDF you want to redact. Use open() for files on disk or from_bytes() for in-memory data.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("sensitive_report.pdf")?;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.
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])?;The default fill is solid black. You can change the fill colour or add a label such as "REDACTED" over the box.
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)?;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.
// redact() / redact_region() apply in place — write the sanitised file
doc.save("report_redacted.pdf")?;
println!("All redactions applied and saved.");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.