Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Add a text box

You’ll type on a report’s first page: a callout pointing at the chart, and a yellow note beside the total with one word in bold. You need this for a text tool in an editor, or a script that adds a review note to every file it checks.

Viewers report where a pointer went in page space: points from the page’s top-left corner. textBoxAt and calloutAt turn a box and points there into the edit that adds a text box, in the page’s user space:

packages/sdk/examples/add-text-box.ts
// Type on a page: a callout pointing at the chart, and a filled note beside the total with one
// word in bold.
// The boxes and points are in page space (points from the page's top-left), as a viewer reports
// where a pointer went. The boxes are in Helvetica, which every reader has, so the WebAssembly
// extract module can draw them too.
import { type AnnotColor, type AnnotCommon, Document, type RichText } from "@laminapdf/sdk";
// Where the boxes go on the example's input, the quarterly report, in points from the page's
// top-left: for your own file, take them from your viewer's pointer.
/** The callout's box, in the space right of the chart: `[x0, y0, x1, y1]`. */
const CALLOUT = [330, 168, 500, 190] as const;
/** The callout's line: from the chart's right edge, through a knee, to the box. */
const LEADER = [
[257, 200],
[300, 179],
[330, 179],
] as const;
/** The note beside the total. */
const NOTE = [300, 452, 520, 486] as const;
/** A pale yellow, like a sticky note. */
const YELLOW: AnnotColor = [1, 0.96, 0.7];
/**
* The document with a callout and a note typed on its first page, saved incrementally. `at` is
* when the boxes were typed, as a PDF date.
*/
export function addTextBox(bytes: Uint8Array, at: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const byMe = (): AnnotCommon => ({ author: "Ada Lovelace", created: at, modified: at });
const callout = doc.calloutAt(0, CALLOUT, LEADER, "Label the chart's axes");
callout.common = byMe();
const text: RichText = [
[{ text: "Check: " }, { text: "€ 4,300", bold: true }, { text: " should match the table's total." }],
];
const note = doc.textBoxAt(0, NOTE, text);
if (note.spec.type === "freeText") note.spec.fill = YELLOW;
note.common = byMe();
doc.apply([
{ op: "addAnnot", args: callout },
{ op: "addAnnot", args: note },
]);
return doc.save({ mode: "incremental" });
}
Page 1 of add_text_box.pdf
Open add_text_box.pdf (2 pages, 27 KB)

The result is the original bytes, unchanged, followed by one revision. A callout reading “Label the chart’s axes” points at the chart’s right edge, and a yellow note beside the total puts “€ 4,300” in bold.

  • Each method gives an edit for a box in 12-point Helvetica, left-aligned, with a 1-point border. Change its spec before you apply it: the font, size, align, the fill behind the text and the border.
  • The text is a string, a paragraph per line, or rich text: an array of paragraphs, each an array of spans that can be bold or italic. Lamina PDF breaks lines at the box’s width.
  • Lamina PDF draws every box itself and stores the drawing in the file, so every reader that draws stored appearances, as pdf.js, PDFium and Acrobat do, shows the same box. The text is upright as the page is shown, even on a turned page.
  • A box keeps its text in the keys other apps read too: the plain text as its description, its font, size and colour, and the bold and italic words as rich text. An app that draws boxes itself from those keys shows plain text where it ignores rich text.
  • The author and dates come from your code, never from a clock inside Lamina PDF, so the same edit gives the same file wherever it runs.
  • Another font: set spec.font to "times" or "courier", which every reader has. For any other font, add its bytes with addBlob and set spec.font to the ID it returns. Lamina PDF embeds the characters the box uses.
  • Characters the font lacks: set spec.fallbacks to the IDs of fonts that have them, for example a Noto font for Chinese or Arabic. A character no font has fails the edit with annotation-glyph-missing.
  • Typewriter text: set spec.intent to { type: "typewriter" } for text with no border or fill, as when filling in a form that has no fields. Its lines end only where yours do, never at the box’s edge, as in Acrobat’s typewriter tool.
  • Change a box later: updateAnnot with a freeText patch replaces its text, font, size or any other field, and Lamina PDF draws it again. A box’s contents are its text, so change them with the patch’s text. A box another app made keeps its look until you change it. Then Lamina PDF keeps its paragraphs, bold and italic, and warns with rich-text-simplified when the box held more.
  • In the browser: boxes in Helvetica, Times and Courier work in the extract and text modules. A font given by its bytes, and fallback fonts, need the text module: open the document after init({ text: true }). Elsewhere the edit fails with appearance-needs-text.
  • A PDF/A file: Helvetica, Times and Courier aren’t embedded, which PDF/A forbids, so doc.plan() warns with pdfa-font-not-embedded. Give the box a font by its bytes instead.

Next: draw on a page, with ink, shapes and arrows made the same way.