How-to guides/Merge & Split

Merge PDFs and generate a table of contents in Rust

Combine multiple PDF files into one and insert a generated table of contents page with clickable bookmark links.

rust
use pdfluent::{Document, merge::{MergeInput, MergeOptions}};

fn main() -> pdfluent::Result<()> {
    let inputs = vec![
        MergeInput::from_file("chapter1.pdf").title("Chapter 1: Introduction"),
        MergeInput::from_file("chapter2.pdf").title("Chapter 2: Setup"),
        MergeInput::from_file("chapter3.pdf").title("Chapter 3: Usage"),
    ];

    let opts = MergeOptions::new().generate_toc(true);
    let merged = Document::merge_with_options(inputs, opts)?;

    merged.save("merged_with_toc.pdf")?;
    Ok(())
}
Install:cargo add pdfluentDownload SDK →

Step by step

1

Prepare MergeInput entries

Each MergeInput specifies a source file and an optional title used for the TOC entry and bookmark.

rust
use pdfluent::merge::MergeInput;

let inputs = vec![
    MergeInput::from_file("section1.pdf").title("Section 1"),
    MergeInput::from_file("section2.pdf").title("Section 2"),
    MergeInput::from_file("appendix.pdf").title("Appendix"),
];
2

Configure merge options

Enable TOC generation and optionally configure the TOC page style, font, and bookmark depth.

rust
use pdfluent::merge::MergeOptions;

let opts = MergeOptions::new()
    .generate_toc(true)
    .toc_title("Table of Contents")
    .toc_position(pdfluent::merge::TocPosition::FirstPage)
    .generate_bookmarks(true);
3

Merge and get the document

Document::merge_with_options returns a new Document. The TOC page is inserted at the position specified in the options.

rust
let merged = Document::merge_with_options(inputs, opts)?;
4

Inspect the generated outlines

The merge operation adds bookmark entries that match the TOC. Verify they are present.

rust
for bookmark in merged.outlines() {
    println!(
        "'{}' -> page {}",
        bookmark.title(),
        bookmark.destination_page() + 1,
    );
}
5

Save the merged document

Write the final merged file.

rust
merged.save("merged_with_toc.pdf")?;

Notes and tips

  • The TOC page is generated using the page dimensions of the first page in the merge list. Override with MergeOptions::toc_page_size(PageSize::A4).
  • Existing bookmarks from each source document are preserved and nested under the top-level chapter bookmark.
  • If a source document is encrypted, decrypt it before passing to MergeInput or pass the password with MergeInput::with_password(pw).
  • Named destinations from each source document are re-scoped with a per-document prefix to avoid collisions.

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