Skip to Content
NexPDF

Annotations

Add, list, move, remove and flatten PDF annotations in the browser, and import or export XFDF, with NexPDF for the web.

English

License module

Creating and changing annotations needs the annotations module (Indie tier and above). Listing annotations works without a key.

Coordinates are PDF points with the origin at the bottom-left of the page. Rectangles are [llx, lly, urx, ury]. To turn a rectangle the user draws on screen into PDF points, use pageView(page).rectToPdf.

Add markup and shapes

addAnnotation({ page, kind, color, opacity, contents, rect }) adds an annotation of a PDF subtype such as Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText or Text (a sticky note). Text markup is fitted to the lines under the rectangle. page defaults to 0, color to #E5484D and opacity to 1.

doc.addAnnotation({ page: 0, kind: 'Highlight', color: '#FFD400', rect: [72, 700, 300, 716] });
doc.addAnnotation({ page: 0, kind: 'FreeText', contents: 'Checked', rect: [72, 640, 260, 660] });
doc.addAnnotation({ page: 0, kind: 'Text', color: '#2563EB', contents: 'Please confirm this date', rect: [320, 700, 340, 720] });
const pdf = doc.toBytes();

Lines, arrows and ink

// A red arrow from (100, 500) to (250, 560), 2 pt wide: page, kind, color, opacity, width, x1, y1, x2, y2.
doc.raw.addLine(0, 'Arrow', '#DC2626', 1, 2, 100, 500, 250, 560);

// Freehand ink: points "x,y;x,y;..." in PDF points: page, color, width, opacity, coords.
doc.raw.addInk(0, '#1D4ED8', 2, 1, '100,400;120,410;140,405;160,420');

A handwritten signature is an ink annotation with several strokes separated by |:

doc.raw.addInkSignature(0, '#111827', 1.5, '300,120;320,130;340,118|350,125;380,128');

This is a visual signature only. For a cryptographic signature see Signatures.

Image stamps and measurements

// A picture stamp from RGBA pixels (width x height x 4 bytes), here from a canvas.
const { data, width, height } = canvas.getContext('2d')!.getImageData(0, 0, canvas.width, canvas.height);
doc.addImageStamp({ page: 0, rect: [400, 60, 520, 120], rgba: new Uint8Array(data.buffer), width, height });

// A stored distance measurement; per_point converts PDF points to the unit.
const result = doc.addMeasurement(0, { kind: 'distance', points: [[100, 100], [300, 100]], unit: 'mm', per_point: 0.3528 });
console.log(result.text);

kind is distance, perimeter or area. doc.measurements() lists the stored ones.

List, edit, move and remove

for (const a of doc.annotations(0)) console.log(a.num, a.gen, a.kind, a.contents);

const [a] = doc.annotations(0);
doc.raw.setAnnotationContents(a.num, a.gen, 'Updated note');
doc.raw.moveAnnotation(a.num, a.gen, 10, -20);     // dx, dy in points
doc.raw.removeAnnotation(0, a.num, a.gen);

Annotations are identified by their object number (num) and generation (gen).

Flatten

Flattening burns annotations into the page content so they can no longer be edited:

doc.raw.flattenAnnotations(-1);   // every page; pass a page index for one page

XFDF import and export

const xfdf = doc.raw.exportXfdf();
const other = await nex.PdfDocument.fromUrl('/clean.pdf');
other.raw.importXfdf(xfdf);

File attachments

doc.raw.addFileAttachmentAnnot(page, x, y, name, data, mime, description) adds a paperclip icon whose top-left corner is at (x, y):

const xml = new Uint8Array(await xmlFile.arrayBuffer());
doc.raw.addFileAttachmentAnnot(0, 520, 760, 'invoice.xml', xml, 'application/xml', 'Source data');

On this page