Pick individual pages or non-contiguous sets and write them to a new PDF. Works with page numbers, page labels, or a custom predicate.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let doc = PdfDocument::open("report.pdf")?;
// Extract pages 1, 3, 5 and 7
doc.extract_pages(&[1, 3, 5, 7])?
.save("selected.pdf")?;
Ok(())
}Open the document you want to extract pages from. Page count is available immediately after opening.
use pdfluent::PdfDocument;
let doc = PdfDocument::open("source.pdf")?;
println!("{} pages total", doc.page_count());Pass a slice of 1-based page numbers. Pages appear in the output in the order given, so you can reorder them freely.
// Extract pages 2, 5, and 8 in that order
let extracted = doc.extract_pages(&[2, 5, 8])?;
extracted.save("three_pages.pdf")?;If the PDF uses custom page labels such as "i", "ii", "A-1", pass label strings instead of integers.
let extracted = doc.extract_pages_by_label(&["i", "ii", "1", "2"])?;
extracted.save("front_matter_and_intro.pdf")?;Use extract_pages_where() to filter programmatically. The closure receives a PageInfo struct with page number, label, width, height, and rotation.
use pdfluent::PageInfo;
// Keep only landscape pages
let extracted = doc.extract_pages_where(|p: &PageInfo| {
p.width > p.height
})?;
extracted.save("landscape_pages.pdf")?;Call save() to write to disk, or to_bytes() to get the PDF data as a Vec<u8> for streaming or further processing.
let bytes = doc.extract_pages(&[1, 2, 3])?.to_bytes()?;
// Send over HTTP, write to S3, etc.
println!("Extracted PDF is {} bytes", bytes.len());No JVM, no runtime, no DLL dependencies. Ships as a single native binary or WASM module.
Rust's ownership model prevents buffer overflows and use-after-free. No segfaults in PDF parsing.
Same code runs server-side, in Docker, on AWS Lambda, on Cloudflare Workers, or in the browser via WASM.