Remove one page, a range of pages, or a custom set of page indices from a PDF. Bookmarks and named destinations are updated automatically.
use pdfluent::PdfDocument;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
// Remove page 5 (zero-based index 4)
doc.delete_page(4)?;
doc.save("report_trimmed.pdf")?;
println!("Remaining pages: {}", doc.page_count());
Ok(())
}Add the pdfluent crate to Cargo.toml.
[dependencies]
pdfluent = "0.9"Load the document you want to modify.
use pdfluent::PdfDocument;
let mut doc = PdfDocument::open("draft.pdf")?;
println!("Before: {} pages", doc.page_count());Call delete_page() with the zero-based index of the page to remove. After deletion, indices of subsequent pages shift down by one.
// Remove the last page
let last = doc.page_count() - 1;
doc.delete_page(last)?;
// Remove the first page
doc.delete_page(0)?;delete_page_range(start..end) removes all pages from start (inclusive) to end (exclusive). This is faster than looping with delete_page().
// Remove pages 3 through 6 (indices 2..6, exclusive end)
doc.delete_page_range(2..6)?;
println!("After range delete: {} pages", doc.page_count());Pass a Vec or slice of indices to delete_pages(). Provide indices in any order; PDFluent sorts and removes them correctly.
// Remove pages at indices 1, 4, and 7
doc.delete_pages(&[1, 4, 7])?;
doc.save("draft_cleaned.pdf")?;
println!("Final page count: {}", doc.page_count());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.