Aller au contenu
NexPDF

Annotations

Ajoutez, listez, déplacez, supprimez et aplatissez des annotations PDF, et importez ou exportez du XFDF, avec NexPDF pour Node.js.

Français

Module de licence

La création et la modification d'annotations nécessitent le module annotations (offre Indie et supérieures). Le listage des annotations fonctionne sans clé.

Les coordonnées sont en points PDF, avec l'origine en bas à gauche de la page. Les rectangles sont [llx, lly, urx, ury].

Ajouter du marquage et des formes

addAnnotation({ page, kind, color, opacity, contents, rect }) ajoute une annotation d'un sous-type PDF tel que Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText ou Text (une note autocollante). Le marquage de texte est ajusté aux lignes situées sous le rectangle. page vaut 0 par défaut, color #E5484D et opacity 1.

import { PdfDocument } from '@nexdevit/nexpdf-node';

const doc = await PdfDocument.open('contract.pdf');
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] });
await doc.save('annotated.pdf');

Lignes, flèches et encre

// 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');

Une signature manuscrite est une annotation d'encre avec plusieurs traits séparés par | :

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

Il s'agit uniquement d'une signature visuelle. Pour une signature cryptographique, voir Signatures.

Tampons d'image et mesures

// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
doc.addImageStamp({ page: 0, rect: [400, 60, 520, 120], rgba: rgbaBytes, width: 240, height: 120 });

// 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 vaut distance, perimeter ou area. doc.measurements() liste celles qui sont stockées.

Lister, modifier, déplacer et supprimer

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);

Les annotations sont identifiées par leur numéro d'objet (num) et leur génération (gen).

Aplatir

L'aplatissement intègre les annotations au contenu de la page : elles ne peuvent plus être modifiées :

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

Import et export XFDF

const xfdf = doc.raw.exportXfdf();
const other = await PdfDocument.open('clean.pdf');
other.raw.importXfdf(xfdf);
await other.save('with-annotations.pdf');

Pièces jointes

doc.raw.addFileAttachmentAnnot(page, x, y, name, data, mime, description) ajoute une icône de trombone dont le coin supérieur gauche se trouve en (x, y) :

import { readFile } from 'node:fs/promises';

doc.raw.addFileAttachmentAnnot(0, 520, 760, 'invoice.xml', await readFile('invoice.xml'), 'application/xml', 'Source data');

Sur cette page