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() -> 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(())
}Add the pdfluent crate to Cargo.toml. No native dependencies are required.
[dependencies]
pdfluent = "0.9"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;
// 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))?;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, 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)?;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.
doc.apply_redactions()?;
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.