Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Add links

You’ll add two links to a report’s first page: one on a box over its chart, one on a line of text. You need this for a link tool in an editor, or a script that links every product name in a catalogue to its web page.

Viewers report a drawn box or a text selection in page space: points from the page’s top-left corner. linkOnArea and linkOnText turn those boxes into the edit that adds the link, in the page’s user space. The example links the chart and the line “Widgets sold out in May.” to two web pages, then changes the chart link’s address:

packages/sdk/examples/add-links.ts
// Add links to a page: one on a box drawn over a chart, one on a line of text, then point the first somewhere else.
// The boxes are in page space (points from the page's top-left), as a viewer reports a drawn box
// and a text selection. Each link gets a description, which screen readers read. In a tagged
// document the link on text joins the structure tree around the words it covers.
import { Document } from "@laminapdf/sdk";
/** The document with two links added to its first page, saved incrementally. Needs `init({ extract: true })`. */
export function addLinks(bytes: Uint8Array): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
// A box drawn over the chart.
const onChart = doc.linkOnArea(0, [56, 140, 300, 240], { uri: "https://example.com/sales" });
onChart.contents = "Sales figures";
// The line "Widgets sold out in May.", as a selection gives it.
const onText = doc.linkOnText(0, [[56, 358.241, 196.335, 373.223]], { uri: "https://example.com/widgets" });
onText.contents = "Widget stock";
const added = doc.apply([
{ op: "addLink", args: onChart },
{ op: "addLink", args: onText },
]);
// The new links' IDs come back with the edit: change the first one's address.
const chartLink = added.createdAnnotations[0];
if (chartLink !== undefined) {
doc.apply([{ op: "updateLink", args: { link: chartLink, target: { uri: "https://example.com/sales/2026" } } }]);
}
return doc.save({ mode: "incremental" });
}
Page 1 of add_links.pdf
Open add_links.pdf (2 pages, 25 KB)

The result is the original bytes, unchanged, followed by two revisions. Neither link draws a border, so the page looks the same, but a click on the chart opens the 2026 sales page and a click on the line opens the widgets page.

  • linkOnArea takes one box, and linkOnText takes one box per line of the selected text. A link on text follows only its lines, so the gaps between them stay plain text.
  • The report is a tagged PDF, the kind screen readers follow. In a tagged file, linkOnText also finds the words under the boxes, and the new link joins the document’s structure around them.
  • Each link takes a target: a page ({ destination }), a named destination ({ named }) or a web address ({ uri }), as a bookmark does.
  • contents is the link’s description, which screen readers read out. The accessible PDF standard, PDF/UA, asks every link for one.
  • The edit’s result lists each new link’s ID in createdAnnotations. updateLink takes that ID to change a link’s boxes, target or description, and deleteLink removes it. pageLinks lists the IDs of the links a page already has.
  • A page of the file: pass { destination: doc.destinationAt(page, view) } as the target, with the view your reader shows.
  • An email address: pass { uri: "mailto:sales@example.com" }. Only absolute http, https and mailto addresses are allowed. Any other address, such as a javascript: one, fails with link-uri-refused.
  • Several lines: pass every line’s box to linkOnText, each [x0, y0, x1, y1], in reading order.
  • A file that claims PDF/UA but isn’t tagged: the edit fails with link-not-tagged, since an untagged link would break the claim.
  • A signed file: you can add links to a certified file that allows comments, or to one with only approval signatures. Lamina PDF appends them as a new revision, and the signatures stay valid. A certification that forbids comments makes the save fail.

Next: edit the bookmarks, which go to places in the same way.