How-to guides/Document Structure

Insert blank or existing pages into a PDF in Rust

Insert a blank page, a page from another PDF, or multiple pages at any position in an existing document.

rust
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(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Add PDFluent to your project

Add the pdfluent crate to Cargo.toml.

rust
[dependencies]
pdfluent = "0.9"
2

Open the target PDF

Load the document you want to modify.

rust
use pdfluent::PdfDocument;

let mut doc = PdfDocument::open("annual_report.pdf")?;
println!("Current page count: {}", doc.page_count());
3

Insert a blank page at a position

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.

rust
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)?;
4

Insert pages from another PDF

Open a second document and copy pages from it into the target at a specific position.

rust
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)?;
}
5

Save the modified document

Write the result to disk.

rust
doc.save("report_expanded.pdf")?;
println!("New page count: {}", doc.page_count());

Notes and tips

  • Insertion indices are zero-based. Inserting at index 0 prepends the page before the current first page.
  • insert_page_from() copies the page content, resources, and annotations from the source document.
  • Bookmarks from the source document are not automatically carried over. Add them manually if needed.
  • PageSize::Custom(width, height) accepts dimensions in points for non-standard page sizes.

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