How-to guides/Watermarks & Stamps

Add an image watermark to a PDF in Rust

Overlay a logo or stamp image on every page of a PDF. Control position, size, opacity, and rotation.

rust
use pdfluent::{PdfDocument, ImageWatermark, WatermarkPosition};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut doc = PdfDocument::open("document.pdf")?;

    let watermark = ImageWatermark::from_file("logo.png")?
        .opacity(0.10)
        .width_percent(40.0)
        .position(WatermarkPosition::Center);

    doc.add_image_watermark(&watermark)?;
    doc.save("document_branded.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Open the PDF

Open the document with a mutable binding.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("brochure.pdf")?;
2

Load the watermark image

PDFluent accepts PNG and JPEG files. PNG with an alpha channel is supported, so transparent logos blend cleanly.

rust
use pdfluent::ImageWatermark;

let watermark = ImageWatermark::from_file("company_logo.png")?;
3

Configure size and opacity

Set the width as a percentage of the page width. The height is calculated automatically to preserve the image aspect ratio.

rust
let watermark = watermark
    .opacity(0.15)          // 15% opacity
    .width_percent(30.0)    // 30% of page width
    .rotation(0.0);         // no rotation
4

Set the position

Choose from preset positions or provide exact coordinates in PDF points with WatermarkPosition::Custom(x, y).

rust
use pdfluent::WatermarkPosition;

let watermark = watermark
    .position(WatermarkPosition::BottomRight);
5

Apply and save

Stamp all pages and write the result to disk.

rust
doc.add_image_watermark(&watermark)?;
doc.save("brochure_branded.pdf")?;

Notes and tips

  • PNG watermarks with a transparent background blend correctly over page content.
  • JPEG watermarks always have a white background because JPEG does not support transparency.
  • Large high-resolution images increase the PDF file size. Resize the watermark image to a reasonable resolution (72-150 DPI at intended size) before adding it.
  • The watermark image is embedded once and referenced on each page, so file size growth is minimal even for multi-page documents.

Why PDFluent for this

Pure Rust

No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.

Memory safe

Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.

Runs anywhere

Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.

Frequently asked questions