Rearrange, reverse, or interleave pages in a PDF document using a page index map.
use pdfluent::Document;
fn main() -> pdfluent::Result<()> {
let mut doc = Document::open("input.pdf")?;
// Move the last page to the front
let count = doc.page_count();
let mut order: Vec<usize> = (0..count).collect();
order.rotate_right(1);
doc.reorder_pages(&order)?;
doc.save("reordered.pdf")?;
Ok(())
}Load the document and check the page count so you can build the index map.
let mut doc = Document::open("input.pdf")?;
let count = doc.page_count();Create a Vec<usize> where each element is the zero-based index of the original page you want at that position. The vec must contain every index exactly once.
// Reverse all pages
let order: Vec<usize> = (0..count).rev().collect();Call reorder_pages with the index slice. PDFluent rewrites the /Pages tree; no content streams are copied or duplicated.
doc.reorder_pages(&order)?;Remove a page index from its current position and insert it at the target position.
let mut order: Vec<usize> = (0..count).collect();
// Move page at index 4 to position 1
let page = order.remove(4);
order.insert(1, page);
doc.reorder_pages(&order)?;Write the reordered document to a file.
doc.save("reordered.pdf")?;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.