How-to guides/Merge & Split

Extract specific pages from a PDF in Rust

Pick individual pages or non-contiguous sets and write them to a new PDF. Works with page numbers, page labels, or a custom predicate.

rust
use pdfluent::PdfDocument;

fn main() -> pdfluent::Result<()> {
    let doc = PdfDocument::open("report.pdf")?;
    let subset = doc.extract_pages(0..3)?;
    subset.save("first-three.pdf")?;
    Ok(())
}

Step by step

1

Open the source PDF

Open the document you want to extract pages from. Page count is available immediately after opening.

rust
use pdfluent::PdfDocument;

let doc = PdfDocument::open("source.pdf")?;
println!("{} pages total", doc.page_count());
2

Extract by page numbers

Pass a slice of 1-based page numbers. Pages appear in the output in the order given, so you can reorder them freely.

rust
// Extract a contiguous page range (0-based, end-exclusive): pages 2-4
let extracted = doc.extract_pages(1..4)?;
extracted.save("pages_2_to_4.pdf")?;
3

Extract by page label

If the PDF uses custom page labels such as "i", "ii", "A-1", pass label strings instead of integers.

rust
// Split into single-page documents
for (i, page) in doc.split_pages()?.into_iter().enumerate() {
    page.save(format!("page_{}.pdf", i + 1))?;
}
4

Extract with a filter predicate

Use extract_pages_where() to filter programmatically. The closure receives a PageInfo struct with page number, label, width, height, and rotation.

rust
// Extract a range and read the bytes (e.g. to send over HTTP)
let bytes = doc.extract_pages(0..3)?.to_bytes()?;
println!("Extracted PDF is {} bytes", bytes.len());
5

Save or return as bytes

Call save() to write to disk, or to_bytes() to get the PDF data as a Vec<u8> for streaming or further processing.

rust
// Extract the final two pages by range and save
let n = doc.page_count();
doc.extract_pages(n.saturating_sub(2)..n)?.save("last_pages.pdf")?;

Notes and tips

  • Pages are extracted in the order of the input slice. Duplicates are allowed and produce repeated pages.
  • Annotations and form fields on extracted pages are included.
  • If a page number is out of range, extract_pages returns an Err immediately before writing any output.

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