Insert a blank page, a page from another PDF, or multiple pages at any position in an existing document.
use pdfluent::{PdfDocument, PageSize};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut doc = PdfDocument::open("report.pdf")?;
// Insert a blank A4 page after page 2 (at index 2)
doc.insert_blank_page(2, PageSize::A4)?;
doc.save("report_with_divider.pdf")?;
println!("Page inserted. New count: {}", 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("annual_report.pdf")?;
println!("Current page count: {}", doc.page_count());insert_blank_page(index, size) inserts a new empty page before the page at that index. Use index = page_count() to append at the end.
use pdfluent::PageSize;
// Insert blank A4 page before the third page (index 2)
doc.insert_blank_page(2, PageSize::A4)?;
// Append a blank Letter page at the end
doc.insert_blank_page(doc.page_count(), PageSize::Letter)?;Open a second document and copy pages from it into the target at a specific position.
let source = PdfDocument::open("cover_page.pdf")?;
// Insert the first page of source before page 0 (prepend)
doc.insert_page_from(0, &source, 0)?;
// Insert pages 1-3 from source after the current last page
for i in 1..=3 {
let pos = doc.page_count();
doc.insert_page_from(pos, &source, i)?;
}Write the result to disk.
doc.save("report_expanded.pdf")?;
println!("New 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.