This guide shows developers how to use PDFluent to apply common page elements to PDF documents. It is for Rust programmers working with PDFs.
Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "1.0.0"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_watermark() stamps every page. There is no page subset: WatermarkOptions carries position, rotation, opacity, layer, font size and colour, and not a page list.
// Apply to all pages
doc.add_watermark("DRAFT", opts)?;Save the watermarked document to disk.
doc.save("contract_draft.pdf")?;Draw text or an image stamp at a fixed position on every page, with configurable opacity, size, and rotation.
use pdfluent::{PdfDocument, PageDecoration, WatermarkOptions};
use pdfluent::watermark::{Layer, Position};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("input.pdf")?;
doc.add_decoration(PageDecoration::watermark(
"CONFIDENTIAL",
WatermarkOptions::centered()
.at(Position::TopRight(36.0, 36.0))
.layer(Layer::Foreground)
.font_size(18.0)
.color(0.8, 0.1, 0.1)
.opacity(1.0),
))?;
doc.save("stamped.pdf")?;
Ok(())
}Stamping rewrites page content, so bind the document as mutable.
let mut doc = PdfDocument::open("input.pdf")?;WatermarkOptions carries the whole appearance. Layer::Foreground draws the stamp over the page content; Layer::Background puts it underneath. Colour components are floats from 0.0 to 1.0.
use pdfluent::WatermarkOptions;
use pdfluent::watermark::{Layer, Position};
let look = WatermarkOptions::centered()
.at(Position::TopRight(36.0, 36.0)) // points in from the corner
.layer(Layer::Foreground)
.font_size(18.0)
.color(0.8, 0.1, 0.1)
.opacity(1.0);add_decoration takes the text and the appearance together. add_watermark(text, options) is the same operation under a shorter name.
use pdfluent::PageDecoration;
doc.add_decoration(PageDecoration::watermark("CONFIDENTIAL", look))?;A diagonal draft mark is the same call with a rotation and a lower opacity, centred rather than cornered.
doc.add_watermark(
"DRAFT",
WatermarkOptions::centered()
.rotated(45.0)
.opacity(0.3)
.font_size(64.0)
.layer(Layer::Background),
)?;An image stamp is an image insert, not a decoration. Give it the target page, the position in PDF points from the bottom-left, and the size to draw at.
use pdfluent::{ImageInsert, InsertImageFormat};
let logo = std::fs::read("stamp_logo.png")?;
let report = doc.insert_image(
ImageInsert::new(logo, InsertImageFormat::Png, 1, 400.0, 700.0, 120.0, 40.0)
.with_opacity(0.6),
)?;
println!("placed {} at {}x{}px", report.resource_name, report.pixel_width, report.pixel_height);Write the stamped document to a new file.
doc.save("stamped.pdf")?;