How-to guides/Content Editing

Add page numbers to a PDF in Rust

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.

Not in the current release. This capability is not part of the published PDFluent SDK; the example below does not compile against the current release. See the changelog for what ships today.
rust
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(())
}

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 number.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("thesis.pdf")?;
println!("Pages: {}", doc.page_count());
3

Configure page number options

PageNumberOptions controls position, font, format string, and which pages receive numbers.

rust
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_page
4

Use Roman numerals for front matter

Apply Roman numeral numbering to the first few pages, then switch to Arabic for the main body.

rust
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)?;
5

Save the numbered document

Write the result to disk.

rust
doc.save("thesis_numbered.pdf")?;
println!("Page numbers added.");

Notes and tips

  • Page numbers are stamped directly onto the content stream. They are permanent and visible to all viewers.
  • start_page is a zero-based index. To skip a cover page, set start_page(1) so page index 0 is left unnumbered.
  • The format string supports {page} for the current page number and {total} for the total page count.
  • margin controls the distance from the nearest page edge. Increase it if numbers appear clipped.

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