Stamp each page with a diagonal text watermark. Control font, size, colour, opacity, rotation, and position.
use pdfluent::{PdfDocument, WatermarkOptions};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("report.pdf")?;
doc.add_watermark("DRAFT", WatermarkOptions::centered().opacity(0.3))?;
doc.save("watermarked.pdf")?;
Ok(())
}Open the document with a mutable binding.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("contract.pdf")?;Create a TextWatermark with the text you want to stamp. Use the builder methods to set style properties.
use pdfluent::WatermarkOptions;
let opts = WatermarkOptions::centered()
.font_size(72.0)
.opacity(0.12) // 12% opacity
.rotated(45.0) // degrees
.color(0.6, 0.0, 0.0); // dark red, sRGBWatermarkPosition::Center places the text at the page centre. Other options include TopLeft, TopRight, BottomLeft, BottomRight, and Custom(x, y).
// Watermarks are centered by default; tune size, angle, and opacity
let opts = WatermarkOptions::centered()
.font_size(64.0)
.rotated(45.0)
.opacity(0.15);add_text_watermark() stamps every page. Use add_text_watermark_on_pages() to target a subset.
// Apply to all pages
doc.add_watermark("DRAFT", opts)?;Save the watermarked document to disk.
doc.save("contract_draft.pdf")?;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.