Stamp each page with a diagonal text watermark. Control font, size, colour, opacity, rotation, and position.
use pdfluent::{PdfDocument, TextWatermark, WatermarkPosition};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("document.pdf")?;
let watermark = TextWatermark::new("CONFIDENTIAL")
.font_size(60.0)
.opacity(0.15)
.rotation(45.0)
.position(WatermarkPosition::Center);
doc.add_text_watermark(&watermark)?;
doc.save("document_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::TextWatermark;
let watermark = TextWatermark::new("DRAFT")
.font_size(72.0)
.opacity(0.12) // 12% opacity
.rotation(45.0) // degrees counter-clockwise
.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).
use pdfluent::WatermarkPosition;
let watermark = watermark.position(WatermarkPosition::Center);add_text_watermark() stamps every page. Use add_text_watermark_on_pages() to target a subset.
// All pages
doc.add_text_watermark(&watermark)?;
// Specific pages (1-indexed)
doc.add_text_watermark_on_pages(&watermark, &[1, 2, 3])?;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.