How-to guides/Content Editing

Add page headers and footers to a PDF in Rust

Stamp text or image headers and footers onto every page of a PDF. Control font, size, colour, alignment, and per-page variables like page numbers and dates.

rust
use pdfluent::{PdfDocument, HeaderFooter, TextElement, Alignment};

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

    let header = HeaderFooter::header()
        .left(TextElement::new("ACME Corp Confidential").font_size(8.0))
        .right(TextElement::new("{date}").font_size(8.0));

    let footer = HeaderFooter::footer()
        .center(TextElement::new("Page {page} of {total_pages}").font_size(8.0));

    doc.apply_header_footer(header)?;
    doc.apply_header_footer(footer)?;

    doc.save("report_with_header_footer.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml.

rust
[dependencies]
pdfluent = "0.9"
2

Open the PDF

Load the document you want to stamp with headers and footers.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("quarterly_results.pdf")?;
3

Define the header

HeaderFooter::header() creates a top-of-page band. Set left, center, and right text zones. Use {page}, {total_pages}, {date}, and {filename} tokens.

rust
use pdfluent::{HeaderFooter, TextElement, Color};

let header = HeaderFooter::header()
    .margin_top(20.0)          // distance from top edge in points
    .left(
        TextElement::new("Quarterly Results 2025")
            .font("Helvetica-Bold")
            .font_size(9.0)
            .color(Color::rgb(0.2, 0.2, 0.2))
    )
    .right(
        TextElement::new("{date}")
            .font("Helvetica")
            .font_size(9.0)
    );
4

Define the footer

HeaderFooter::footer() creates a bottom-of-page band. The same three zones and tokens apply.

rust
let footer = HeaderFooter::footer()
    .margin_bottom(20.0)
    .left(
        TextElement::new("CONFIDENTIAL")
            .font("Helvetica")
            .font_size(7.0)
            .color(Color::rgb(0.6, 0.0, 0.0))
    )
    .center(
        TextElement::new("Page {page} of {total_pages}")
            .font("Helvetica")
            .font_size(8.0)
    )
    .right(
        TextElement::new("Acme Corp")
            .font("Helvetica")
            .font_size(7.0)
    );
5

Apply to the document and save

apply_header_footer() stamps all pages by default. Pass a page range to limit it to specific pages.

rust
doc.apply_header_footer(header)?;
doc.apply_header_footer(footer)?;

// Or apply only to pages 2 onwards (skip cover page)
// doc.apply_header_footer_range(footer, 1..)?;

doc.save("quarterly_results_final.pdf")?;

Notes and tips

  • Headers and footers are stamped directly onto each page content stream. They become permanent content, not annotations.
  • Available tokens: {page} (current page number), {total_pages}, {date} (today in ISO format), {filename}.
  • margin_top and margin_bottom are measured in points from the edge of the MediaBox.
  • If pages have different sizes, the header/footer adapts to each page width automatically.

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