Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Copy pages between PDFs

You’ll copy a page from one PDF into another, then make a new PDF from three pages of a longer one. Use the first to add a cover page, an appendix or a signed form from another file, and the second to send someone only the chapter they need.

Add the other file as a source first. Lamina PDF names the source by a hash of its bytes and lists its page IDs, and you pick the pages to copy from that list. The program copies the other file’s third page to the end:

packages/sdk/examples/insert-pages.ts
// Copy a page from another PDF into this one.
//
// The copy keeps its annotations and form fields. A field whose name the document already uses
// takes the next free `<name>#2`, and a link to a page that didn't come along is removed; the
// edit lists both.
import { Document } from "@laminapdf/sdk";
/** `bytes` with the third page of `other` added at the end. Needs `init()`. */
export function insertPages(bytes: Uint8Array, other: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const source = doc.addSource(other);
const third = source.pages[2];
if (third === undefined) {
throw new Error("the other file has no third page");
}
const edited = doc.insertPagesFrom(source.id, [third], "end");
for (const field of edited.fields ?? []) {
if (field.outcome === "renamed") {
console.warn(`a copied field was renamed ${field.name}`);
}
}
console.warn(`${edited.linksRemoved?.length ?? 0} links removed`);
return doc.save();
}
Page 1 of insert_pages.pdf
Open insert_pages.pdf (3 pages, 4 KB)

The result has three pages, and the third is the copied one. Its text field was called name, which the form already used, so the copy’s field is name#2 and keeps a value of its own.

extractPages (extract_pages in Rust) takes page IDs and returns a new document holding only those pages:

packages/sdk/examples/extract-pages.ts
// Make a new PDF from some pages of another.
//
// The new file holds only what its pages use, and its pages keep the labels they had (`iii`,
// `1`, `2` rather than 1, 2, 3).
import { Document } from "@laminapdf/sdk";
/** A new document of the third to the fifth page of `bytes`. Needs `init()`. */
export function extractPages(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const pages = doc.pages.slice(2, 5).map((page) => page.id);
using part = doc.extractPages(pages);
return part.save();
}
Page 1 of extract_pages.pdf
Open extract_pages.pdf (3 pages, 912 bytes)

The new file’s pages keep the labels they showed in the book, iii, 1 and 2, and it holds only the fonts and images those pages use.

  • A copy is a new page with a new ID, listed in the edit’s pageOrder. The copy shares nothing with the source file, so changing one never changes the other.
  • The copy’s annotations come along. A link to a page that came too still works; a link to a page left behind is removed, and the edit’s linksRemoved lists the page that held it.
  • Form fields keep their names. A name the document already uses gets the next free number, <name>#2 and on, and the edit’s fields lists each renamed field.
  • A signature’s field isn’t copied, since a signature signs one place in one file. The edit’s fields lists it as notCopied.
  • Page labels continue the numbering where the copies land, and every other page keeps its label.
  • Replace a page: replacePages(targets, source, pages) puts each source page in a target’s place. Bookmarks, links and named destinations that pointed at the old page point at the new one.
  • Keep fields linked: pass { fields: "link" } as the last argument, and a copied field joins the document’s field of the same name, so both show one value. Linking needs both fields to have the same type and value; otherwise the copy is renamed.
  • Leave annotations behind: pass { annotations: false } to copy only the page content and its form fields.
  • Split into several files: exportRanges([[a, b], [c]]) makes one document per list of pages, and split cuts a file by chapter or page count.
  • Tagged documents: a copied page keeps its tags when the document is tagged: the elements that hold its content join the document’s structure tree, so a screen reader still reads it in order. A role, class or element ID the document already uses another way is renamed <name>#2, and the edit’s structureRenamed lists it. Copied into an untagged document, the page comes without its tags, and the edit’s tagsDropped lists it. On a file that claims PDF/UA-1, copying from a file without that claim warns with imported-pdfua-unchecked.
  • Undo: one undo() removes every copied page and what came with it.

Next: duplicate pages, to copy a page within the same document.