Zum Inhalt springen
NexPDF

Annotationen

PDF-Annotationen im Browser mit NexPDF für das Web hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren oder exportieren.

Deutsch

Lizenzmodul

Das Erstellen und Ändern von Annotationen braucht das Modul annotations (Tarif Indie und höher). Das Auflisten von Annotationen funktioniert ohne Schlüssel.

Koordinaten sind PDF-Punkte mit dem Ursprung unten links auf der Seite. Rechtecke sind [llx, lly, urx, ury]. Um ein Rechteck, das der Benutzer auf dem Bildschirm zeichnet, in PDF-Punkte umzurechnen, verwenden Sie pageView(page).rectToPdf.

Markierungen und Formen hinzufügen

addAnnotation({ page, kind, color, opacity, contents, rect }) fügt eine Annotation eines PDF-Subtyps wie Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText oder Text (eine Haftnotiz) hinzu. Textmarkierungen werden an die Zeilen unter dem Rechteck angepasst. page ist standardmäßig 0, color #E5484D und opacity 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();

Linien, Pfeile und Freihand

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

Eine handschriftliche Signatur ist eine Freihand-Annotation mit mehreren, durch | getrennten Strichen:

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

Das ist nur eine visuelle Signatur. Für eine kryptografische Signatur siehe Signaturen.

Bildstempel und Messungen

// 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 ist distance, perimeter oder area. doc.measurements() listet die gespeicherten auf.

Auflisten, bearbeiten, verschieben und entfernen

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

Annotationen werden durch ihre Objektnummer (num) und Generation (gen) identifiziert.

Verflachen

Das Verflachen brennt Annotationen in den Seiteninhalt ein, sodass sie nicht mehr bearbeitet werden können:

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

XFDF-Import und -Export

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

Dateianhänge

doc.raw.addFileAttachmentAnnot(page, x, y, name, data, mime, description) fügt ein Büroklammer-Symbol hinzu, dessen obere linke Ecke bei (x, y) liegt:

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

Auf dieser Seite