Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Merge and split PDFs

You’ll merge two PDFs into one, then split a book into one file per chapter. Use the first to send a report with its appendices as one file, and the second to send each reader only the chapter they need.

Document.merge (Document::merge in Rust) takes each file’s bytes and a name for it, and returns a new document with every file’s pages in order:

packages/sdk/examples/merge-documents.ts
// Merge PDFs into one, with a bookmark for each file.
//
// Each file's own bookmarks go under a bookmark named after it, its form fields keep working
// (renamed when two files use one name), and its pages keep their labels.
import { Document } from "@laminapdf/sdk";
/** One document of `files` (each a name and its bytes), in order, with a bookmark per file. Needs `init()`. */
export function mergeDocuments(files: readonly (readonly [string, Uint8Array])[]): Uint8Array<ArrayBuffer> {
using merged = Document.merge(files.map(([title, bytes]) => ({ bytes, title })));
return merged.save();
}
Page 1 of merge_documents.pdf
Open merge_documents.pdf (6 pages, 2 KB)

The merged file has six pages: the five of references.pdf, then the one of hello.pdf. Its bookmarks start with one per file, named as you named the file, and references.pdf’s own bookmarks sit under its bookmark.

split cuts a document where each top-level bookmark’s page begins and returns one new document per chapter:

packages/sdk/examples/split-document.ts
// Split a PDF into one file per chapter.
//
// The file is cut where each top-level bookmark's page begins, and each part keeps the
// bookmarks of its chapter.
import { Document } from "@laminapdf/sdk";
/** One file per chapter of `bytes`: a part from each top-level bookmark's page to the next. Needs `init()`. */
export function splitDocument(bytes: Uint8Array): Uint8Array<ArrayBuffer>[] {
using doc = Document.open(bytes);
const chapters = doc.split({ bookmarks: 1 });
try {
return chapters.map((chapter) => chapter.save());
} finally {
for (const chapter of chapters) chapter.free();
}
}

The book has three chapters of four pages, so the program makes three files. Each keeps its chapter’s bookmark and the two section bookmarks under it.

  • Each file’s pages come in as copied pages do: with their annotations, form fields and page labels, and with the named destinations their links use. Copy pages between PDFs explains the rules.
  • When two files use one form field name, the later file’s field becomes <name>#2. A named destination that two files use is renamed the same way, and the bookmarks and links that used it follow the new name.
  • Layers keep the visibility each file gave them.
  • A merged file is a new document, so saving it writes a whole file with no password. An encrypted file needs its password beside its bytes.
  • A part of a split holds only what its pages use, as a document from extractPages does.

Merged pages keep their tags when the result can stay tagged throughout: when you merge into a tagged document, or make a new document from files that are all tagged. Each file’s elements go under a Part element named after it, so a screen reader reads the files in order. When one of the files is untagged, a new document comes out untagged, since a tagged file must tag all its content.

On a document that claims PDF/UA-1, merging a file without that claim warns with imported-pdfua-unchecked. A document that claims a PDF/A level warns with imported-pdfa-unchecked when a merged file doesn’t claim the same level, so check the saved file with a PDF/A validator.

  • Keep the files’ bookmarks at the top level: pass { outline: "keep" } as the options. { outline: "none" } leaves them out.
  • Merge into an open document: add each file with addSource, then call doc.mergeDocuments([a.id, b.id], "end"). The edit’s renamedDestinations lists each renamed destination, and one undo() takes the whole merge back.
  • Merge only some pages: pass { source: a.id, pages: [...] } in place of the source’s ID. Bookmarks to pages left behind are dropped, and the edit’s bookmarksDropped counts them.
  • Split by page count: split({ count: 10 }) makes parts of ten pages. split({ ranges: [[a, b], [c]] }) makes one part per list of page IDs.
  • Split at sections: split({ bookmarks: 2 }) cuts at the second level of bookmarks.

Next: organise pages, to put the merged pages in the order you want.