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-beta.18"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")?;Overlay a logo or stamp image on every page of a PDF. Control position, size, opacity, and rotation.
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(())
}Open the document with a mutable binding.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("brochure.pdf")?;PDFluent accepts PNG and JPEG files. PNG with an alpha channel is supported, so transparent logos blend cleanly.
use pdfluent::ImageWatermark;
let watermark = ImageWatermark::from_file("company_logo.png")?;Set the width as a percentage of the page width. The height is calculated automatically to preserve the image aspect ratio.
let watermark = watermark
.opacity(0.15) // 15% opacity
.width_percent(30.0) // 30% of page width
.rotation(0.0); // no rotationChoose from preset positions or provide exact coordinates in PDF points with WatermarkPosition::Custom(x, y).
use pdfluent::WatermarkPosition;
let watermark = watermark
.position(WatermarkPosition::BottomRight);Stamp all pages and write the result to disk.
doc.add_image_watermark(&watermark)?;
doc.save("brochure_branded.pdf")?;Draw text or an image stamp at a fixed position on every page, with configurable opacity, size, and rotation.
use pdfluent::{PdfDocument, stamp::{TextStamp, StampPosition}};
fn main() -> pdfluent::Result<()> {
let mut doc = PdfDocument::open("input.pdf")?;
let stamp = TextStamp::new("CONFIDENTIAL")
.position(StampPosition::Center)
.font_size(48.0)
.opacity(0.25)
.rotation(45.0)
.color(pdfluent::color::Color::rgb(0.8, 0.0, 0.0));
doc.apply_stamp(&stamp)?;
doc.save("stamped.pdf")?;
Ok(())
}Stamps are applied to page content streams. Open the file for mutation.
let mut doc = PdfDocument::open("input.pdf")?;TextStamp::new takes the stamp text. Chain builder methods for position, size, color, rotation, and opacity.
use pdfluent::stamp::{TextStamp, StampPosition};
let stamp = TextStamp::new("DRAFT")
.position(StampPosition::Center)
.font_size(72.0)
.opacity(0.15)
.rotation(45.0);doc.apply_stamp() iterates all pages and appends the stamp as a graphics operator block in each content stream.
doc.apply_stamp(&stamp)?;Use apply_stamp_to_page to target individual pages.
// Stamp only pages 1 and 3 (zero-indexed: 0, 2)
doc.apply_stamp_to_page(&stamp, 0)?;
doc.apply_stamp_to_page(&stamp, 2)?;Load a PNG or JPEG and use ImageStamp to overlay it on each page.
use pdfluent::stamp::{ImageStamp, StampPosition};
let image_data = std::fs::read("stamp_logo.png")?;
let img_stamp = ImageStamp::from_png(&image_data)
.position(StampPosition::BottomRight)
.width_pt(120.0)
.opacity(0.6);
doc.apply_stamp(&img_stamp)?;
doc.save("stamped.pdf")?;Stamp page numbers onto every page of a PDF. Control position, font, format, and starting number. Skip the cover page or use Roman numerals for front matter.
use pdfluent::{PdfDocument, PageNumberOptions, PageNumberPosition};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
doc.add_page_numbers(
PageNumberOptions::default()
.position(PageNumberPosition::BottomCenter)
.format("{page} / {total}")
.font_size(9.0)
.start_page(1) // skip cover page (index 0)
.start_number(1),
)?;
doc.save("report_numbered.pdf")?;
Ok(())
}Load the document you want to number.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("thesis.pdf")?;
println!("Pages: {}", doc.page_count());PageNumberOptions controls position, font, format string, and which pages receive numbers.
use pdfluent::{PageNumberOptions, PageNumberPosition};
let opts = PageNumberOptions::default()
.position(PageNumberPosition::BottomCenter)
.format("{page}") // or "Page {page} of {total}"
.font("Helvetica")
.font_size(10.0)
.margin(30.0) // distance from page edge in points
.start_page(0) // zero-based index of first numbered page
.start_number(1); // the number printed on start_pageApply Roman numeral numbering to the first few pages, then switch to Arabic for the main body.
use pdfluent::{PageNumberOptions, PageNumberPosition, NumberStyle};
// Front matter: pages 0-3 in Roman numerals (i, ii, iii, iv)
let front_opts = PageNumberOptions::default()
.position(PageNumberPosition::BottomCenter)
.style(NumberStyle::RomanLower)
.start_page(0)
.end_page(3)
.start_number(1)
.font_size(9.0);
// Main body: pages 4 onward in Arabic (1, 2, 3, ...)
let body_opts = PageNumberOptions::default()
.position(PageNumberPosition::BottomCenter)
.style(NumberStyle::Arabic)
.start_page(4)
.start_number(1)
.font_size(9.0);
doc.add_page_numbers(front_opts)?;
doc.add_page_numbers(body_opts)?;Write the result to disk.
doc.save("thesis_numbered.pdf")?;
println!("Page numbers added.");