Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Add text to a page

You’ll type two lines on a report’s first page: a note in Helvetica with one bold word, and a greeting in two scripts. You need this for an editor’s text tool, a script that stamps a line on every page, or a document that has no form fields.

Unlike a text box, which is a comment, this text becomes part of the page: it prints, every reader draws it, and no one can hide it. A box in page space, points from the page’s top-left corner as a viewer reports where a pointer went, says where it goes:

packages/sdk/examples/add-text.ts
// Type on a page as page content: a note in Helvetica with one bold word, and a greeting in two
// scripts set in fonts given by their bytes, the second font drawing the Arabic the first lacks.
// The text becomes part of the page: it prints, every reader draws it, and it copies and searches
// as typed. The boxes are in page space (points from the page's top-left), as a viewer reports
// where a pointer went. In this tagged report each text is a paragraph that screen readers read.
// Fonts given by their bytes need the WebAssembly text module; Helvetica, Times and Courier need
// no bytes.
import { Document } from "@laminapdf/sdk";
// Where the texts 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 note: `[x0, y0, x1, y1]`. */
const NOTE = [56, 690, 300, 712] as const;
/** The greeting, under the note. */
const GREETING = [56, 718, 300, 746] as const;
/** The size of the greeting, in points. */
const GREETING_SIZE = 16;
/**
* The document with a note and a greeting typed on its first page, saved incrementally. `at` is
* when they were typed, as a PDF date.
*/
export function addText(bytes: Uint8Array, latin: Uint8Array, arabic: Uint8Array, at: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
doc.content.addText(0, {
id: "note",
rect: NOTE,
runs: [{ text: "Reviewed " }, { text: "twice", bold: true }],
fonts: ["helvetica"],
modified: at,
});
// The first font draws the Latin, the second what the first lacks.
doc.content.addText(0, {
id: "greeting",
rect: GREETING,
runs: [{ text: "Hello مرحبا" }],
fonts: [doc.addBlob(latin), doc.addBlob(arabic)],
size: GREETING_SIZE,
modified: at,
});
return doc.save({ mode: "incremental" });
}
Page 1 of add_text.pdf
Open add_text.pdf (2 pages, 35 KB)

The result is the original bytes, unchanged, followed by one revision. The greeting’s Latin letters come from the first font and the Arabic from the second, so a font you pass never has to cover every script.

  • Fonts: fonts is the font, then the fallbacks. "helvetica", "times" and "courier" need no bytes and work in the extract and text modules. For any other font, add its bytes with addBlob and name the ID it returns. Lamina PDF embeds the part of the font the text uses, with a map that makes the text copy and search as typed.
  • Fallbacks: each character is drawn in the first font of the chain that has it, so one list can hold a Latin font, a symbol font and the font of another script. A character no font has fails the edit with content-glyph-missing, never a blank.
  • Layout: the text is shaped and wrapped at the box’s width, right to left where the script runs that way. It starts at the top of the box and is clipped to it, so make the box tall enough.
  • Tagging: in a tagged file the text is a paragraph that screen readers read, or decoration they skip for tag: "artifact". A file that claims PDF/UA-1 also needs every font embedded (ISO 14289-1 §7.21.4.1): text in Helvetica, Times or Courier is not, so add the font’s bytes for such a file. Lamina PDF does not check this when it adds the text.
  • Finding it again: Lamina PDF stores each text’s ID and what it says with the text. After a save and a reopen, content.added lists it and content.update and content.remove change it.
  • A font that forbids embedding: a font file declares in its OS/2 table whether it may be embedded. One that forbids it fails the edit with content-font-not-embeddable; choose another font. Bytes that aren’t a TrueType or OpenType font, such as a WOFF2 file, fail with content-font-invalid.
  • Size, colour and alignment: size is in points, color a gray, RGB or CMYK array, and align is "left", "center" or "right".
  • Change the text later: content.update(index, id, { text, modified }) replaces the box, runs, fonts, size and colour, and matrix moves or turns it. content.remove(index, id) takes it off; an incremental save keeps the earlier revisions, and the text stays in them.
  • In the browser: Helvetica, Times and Courier work in the extract and text modules. A font given by its bytes needs 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 text a font by its bytes instead.

Next: place images and shapes in the same way.