How-to guides/Document Info

Get the page count of a PDF in Rust

Read the total number of pages from a PDF file. PDFluent reads the page count from the document catalog without parsing every page's content stream.

rust
use pdfluent::prelude::*;

fn main() -> Result<()> {
    let doc = PdfDocument::open("report.pdf")?;
    println!("Pages: {}", doc.page_count());
    Ok(())
}
Install:cargo add pdfluent@1.0.0-beta.5Download SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml.

rust
[dependencies]
pdfluent = "1"
2

Open the PDF and call page_count()

page_count() reads the /Count entry in the PDF page tree. It doesn't parse page content streams; for a well-formed PDF the call is effectively O(1).

rust
use pdfluent::prelude::*;

let doc = PdfDocument::open("invoice.pdf")?;
let count = doc.page_count();
println!("This PDF has {} page(s)", count);
3

Get page count from bytes

If the PDF is already in memory (from a network response or database blob), use from_bytes() instead of open().

rust
let pdf_bytes: Vec<u8> = fetch_pdf_from_database()?;
let doc = PdfDocument::from_bytes(&pdf_bytes)?;
println!("Pages: {}", doc.page_count());
4

Walk the page tree when /Count is suspect

Some malformed PDFs have an incorrect /Count entry. If you need a physically-verified count, iterate doc.pages() and count as you go — each iteration step resolves the next page from the tree.

rust
let doc = PdfDocument::open("maybe_corrupt.pdf")?;
let physical_count = doc.pages().count();
let declared_count = doc.page_count();
assert_eq!(physical_count, declared_count, "declared /Count != physical pages");
5

Batch page counts for a directory of PDFs

Loop over files and collect counts. PdfDocument opens in-memory, so drop each doc between iterations to keep allocations bounded.

rust
use pdfluent::prelude::*;
use std::fs;

for entry in fs::read_dir("./invoices")? {
    let path = entry?.path();
    if path.extension().map(|e| e == "pdf").unwrap_or(false) {
        match PdfDocument::open(&path) {
            Ok(doc) => println!("{}: {} pages", path.display(), doc.page_count()),
            Err(e) => eprintln!("{}: error - {}", path.display(), e),
        }
    }
}

Notes and tips

  • page_count() returns a usize. A valid PDF always has at least 1 page.
  • Encrypted PDFs require decryption before the page count is accessible. Open them with PdfDocument::open_with(path, OpenOptions::new().with_password("...")).
  • If you need a physically-accurate count against a potentially-malformed /Count entry, walk doc.pages().count() as shown in step 4.
  • Page indexing in the 1.0 SDK is 1-based throughout (RFC 0001 §1).

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