Add watermark to PDF using Rust
Contents
[
Hide
]
The add_watermark method allows developers to programmatically apply a text watermark to an existing PDF document. The watermark can be fully customized, including:
- Watermark text
- Font family
- Font size
- Text color (HEX format)
- X and Y position coordinates
- Rotation angle
- Foreground or background placement
- Opacity (transparency level)
In this example, the application opens an existing PDF file, applies a semi-transparent rotated watermark, and saves the modified document under a new file name.
This functionality is particularly useful for marking documents as Draft, Confidential, Sample, or for adding branding elements before distribution.
- Open the existing PDF document.
- Call the add_watermark method and configure the watermark properties.
- Save the updated document.
use asposepdf::Document;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Open a PDF-document with filename
let pdf = Document::open("sample.pdf")?;
// Add watermark to PDF-document
pdf.add_watermark(
"WATERMARK",
"Arial",
16.0,
"#010101",
100,
100,
45,
true,
0.5,
)?;
// Save the previously opened PDF-document with new filename
pdf.save_as("sample_add_watermark.pdf")?;
Ok(())
}