Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Edit the bookmarks

You’ll add a bookmark to the top of an outline, move another under a chapter, name places links jump to, and rename one with its uses. You need this for a bookmark panel, a script that fixes a converter’s outlines, or a pipeline that adds a summary to every report.

A bookmark goes to a page and a view of it. Viewers report where they are in page space: points from the page’s top-left corner, as the rest of Lamina PDF’s API uses them. destinationAt turns that place into the destination the file stores, in the page’s user space. The example adds a bold “Summary” bookmark at the top, moves “Website” under “Chapters”, names the last page summary and renames chapter-2 to methods:

packages/sdk/examples/edit-bookmarks.ts
// Edit a document's bookmarks and named destinations: add a bookmark, move one, and name a place links can jump to.
// A new "Summary" bookmark goes first, in bold, showing the top of the last page. The place comes
// from page space, as a viewer reports it, and becomes the destination the bookmark stores. The
// same place gets the name `summary`, the destination `chapter-2` is renamed with every bookmark
// that uses it, and the "Website" bookmark moves to the end of the "Chapters" bookmark, when the
// file has them.
import { Document } from "@laminapdf/sdk";
/** The document with its bookmarks edited, saved incrementally. Needs `init({ extract: true })`. */
export function editBookmarks(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
// The top of the last page, in page space (points from the page's top-left).
const last = Math.max(doc.pageCount - 1, 0);
const summary = doc.destinationAt(last, { kind: "at", x: 0, y: 0 });
doc.apply([
{
op: "addOutlineItem",
args: {
parent: null,
at: "first",
title: "Summary",
target: { destination: summary },
style: { bold: true },
},
},
{ op: "setNamedDestination", args: { name: "summary", destination: summary } },
]);
// Rename a destination, if the file has it: bookmarks and links that use it follow.
if (doc.namedDestinations().some((d) => d.name === "chapter-2")) {
doc.apply([{ op: "renameNamedDestination", args: { from: "chapter-2", to: "methods" } }]);
}
const outline = doc.outline();
const find = (title: string) => outline.find((node) => node.title === title)?.id;
const website = find("Website");
const chapters = find("Chapters");
if (website !== undefined && chapters !== undefined) {
doc.apply([{ op: "moveOutlineItem", args: { item: website, parent: chapters, at: "last" } }]);
}
return doc.save({ mode: "incremental" });
}
Page 1 of edit_bookmarks.pdf
Open edit_bookmarks.pdf (4 pages, 7 KB)

The result is the original bytes, unchanged, followed by one revision. Its outline starts with “Summary” in bold, the “Website” bookmark sits at the end of “Chapters”, and the name summary goes to the top of the last page. The bookmark that went to chapter-2 now goes to methods, the same place under its new name.

  • outline() lists every bookmark with its id, which the bookmark edits take, its title, where it goes, its colour and style, and whether its children show.
  • addOutlineItem (AddOutlineItem in Rust) adds a bookmark under a parent (null for the top level) at a position: "first", "last", { before: id } or { after: id }. Its target is a page ({ destination }), a named destination ({ named }) or a web address ({ uri }). The edit’s result lists the new bookmark’s ID in createdOutlineItems.
  • updateOutlineItem, moveOutlineItem and deleteOutlineItem change, move and delete a bookmark by its ID. A moved bookmark takes its children with it and keeps its ID. A deleted one takes its children with it, or leaves them in its place with children: "promote".
  • setNamedDestination gives a place a name, or points an existing name elsewhere. renameNamedDestination renames it, together with every bookmark, link and opening view of the file that uses it. Pass links: "keep" to leave those alone.
  • Lamina PDF keeps each bookmark’s place in the tree, and the counts of open bookmarks, consistent after every edit. When a file’s outline was broken, such as a loop or a wrong last item, the first edit rewrites it whole and says so in outlineRepaired.
  • A colour or style: pass style: { color: "#c00000", bold: true, italic: false }. Leave color out for the viewer’s default black.
  • A web address: pass target: { uri: "https://example.com" }. Only absolute http, https and mailto addresses are allowed; anything else, javascript: first, fails with link-uri-refused.
  • The current view: pass what your viewer shows to destinationAt, such as { kind: "at", x, y, zoom } for a point at the top-left, or { kind: "fit-width", y } to fit the width.
  • Deleting a name: deleteNamedDestination leaves the bookmarks and links that use it alone, since removing them could lose your content. The edit’s destinationUses lists them, so you can retarget or delete them next.
  • A signed file: a bookmark edit changes the document’s structure, so a certified file refuses the save. On a file with approval signatures only, the save appends a revision and the signatures stay valid.

Next: save a PDF, for what a signed or repaired file allows.