Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Mark up a page

You’ll mark up a report’s first page the way a reviewer does: highlight a line, leave a note beside the chart, and propose a better word. You need this for a review tool in an editor, or a script that flags every sentence a checker finds.

Viewers report a text selection as one box per line, and a click as a point, in page space: points from the page’s top-left corner. markText, noteAt and caretAt turn those into the edit that adds the mark, in the page’s user space. replaceText adds a strike-out and a caret together:

packages/sdk/examples/mark-up.ts
// Mark up a page: highlight a line of text, leave a note beside the chart, and propose a better word.
// The boxes are in page space (points from the page's top-left), as a viewer reports a text
// selection and a click. Each mark records who made it and when. In a tagged document a
// highlight's description, which screen readers read, is the text it marks.
import { Document } from "@laminapdf/sdk";
// Where the marks 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 selection and clicks.
/** The line "Widgets sold out in May.", as a selection gives it: `[x0, y0, x1, y1]`. */
const WIDGETS_LINE = [56, 358.241, 196.335, 373.223] as const;
/** The words "sold out" on that line. */
const SOLD_OUT = [114.902, 358.241, 156.713, 373.223] as const;
/** The note's spot, right of the chart. */
const BESIDE_CHART = [310, 140] as const;
/** The colour of a paper sticky note. */
const STICKY_YELLOW: [number, number, number] = [1, 0.82, 0];
/**
* The document with a highlight, a note and a proposed word on its first page, saved
* incrementally. `at` is when the marks were made, as a PDF date. Needs `init({ extract: true })`.
*/
export function markUp(bytes: Uint8Array, at: string): Uint8Array<ArrayBuffer> {
using doc = Document.open(bytes);
const byMe = { author: "Ada Lovelace", created: at, modified: at };
// In a tagged document the highlight's description is already the marked text; add who made it.
const highlight = doc.markText(0, "highlight", [WIDGETS_LINE]);
highlight.common = { ...highlight.common, ...byMe };
const note = doc.noteAt(0, BESIDE_CHART, "Add June's figures?", "comment");
note.common = { ...note.common, ...byMe, color: STICKY_YELLOW };
doc.apply([
{ op: "addAnnot", args: highlight },
{ op: "addAnnot", args: note },
]);
// "sold out" reads better as "ran out": a strike-out and a caret, applied as one undo step.
doc.replaceText(0, [SOLD_OUT], "ran out", byMe);
return doc.save({ mode: "incremental" });
}
Page 1 of mark_up.pdf
Open mark_up.pdf (2 pages, 28 KB)

The result is the original bytes, unchanged, followed by two revisions. “Widgets sold out in May.” is highlighted in yellow, a speech-bubble note sits right of the chart, and “sold out” is struck through with a caret after it that holds “ran out”.

  • markText takes one box per line of the selected text and a kind: highlight, underline, strikeOut or squiggly. Each box becomes one of the mark’s quadrilaterals, so a mark over three lines covers only those lines.
  • Lamina PDF draws every mark 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 mark. A highlight multiplies its colour with the page, which keeps the text under it legible.
  • The report is a tagged PDF, the kind screen readers follow. In a tagged file, a mark’s description (contents) is the text it marks, or the kind of mark when its boxes hold no text. The mark joins the document’s structure right after that text.
  • noteAt puts a 20-point icon with its top-left corner on the point, and a pop-up beside it that shows the comment. The note keeps its size at any zoom and stays upright when the page turns, as Acrobat’s notes do.
  • replaceText groups the strike-out with a caret, so a reader shows, moves and deletes them as one correction. The caret holds the replacement text. Both edits are one step: a single undo removes both.
  • 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 mark: pass "underline", "strikeOut" or "squiggly" to markText.
  • A colour or opacity: set common.color ([r, g, b] from 0 to 1) and common.opacity before applying the edit.
  • A note’s icon: pass "note", "comment", "key", "help", "paragraph", "newParagraph" or "insert".
  • Change a mark later: updateAnnot takes the ID from createdAnnotations and a new color, opacity or spec. Lamina PDF draws the mark again. deleteAnnot removes a mark, with its pop-up and the rest of its group.
  • A PDF/A or PDF/UA file: the marks keep the file’s claims, and doc.plan() warns before you save when an edit breaks one. A highlight breaks PDF/A-1, which forbids transparency (pdfa-transparency). A mark with an empty description breaks PDF/UA (annot-without-contents).
  • A signed file: you can mark up a certified file that allows comments, or one with only approval signatures. Lamina PDF appends the marks as a new revision, and the signatures stay valid.

Next: add links, another kind of annotation made the same way.