Zum Inhalt springen
NexPDF

Annotationen

PDF-Annotationen auf dem Gerät hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren oder exportieren, mit NexPDF für React Native.

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) und rectToPdf.

Markierungen und Formen hinzufügen

addAnnotation(page, kind, color, opacity, contents, llx, lly, urx, ury) fügt eine Annotation eines PDF-Subtyps hinzu, etwa Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText oder Text (eine Haftnotiz). Textmarkierungen werden an die Zeilen unter dem Rechteck angepasst.

const doc = await NexPdf.open(base64Pdf);
await doc.addHighlight(0, [72, 700, 300, 716]);
await doc.addAnnotation(0, 'FreeText', '#E5484D', 1, 'Checked', 72, 640, 260, 660);
await doc.addAnnotation(0, 'Text', '#2563EB', 1, 'Please confirm this date', 320, 700, 340, 720);
const annotated = await doc.saveBytes();   // base64

Linien, Pfeile und Freihandzeichnungen

// A red arrow from (100, 500) to (250, 560), 2 pt wide.
await doc.addLine(0, 'Arrow', '#DC2626', 1, 2, 100, 500, 250, 560);

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

Eine handschriftliche Signatur ist eine Freihand-Annotation (Ink) mit mehreren Strichen, die durch | getrennt sind:

await doc.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, base64), here 240 x 120.
await doc.addImageStamp(0, 400, 60, 520, 120, rgbaBase64, 240, 120, 1);

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

kind ist distance, perimeter oder area. measurements() listet die gespeicherten auf.

Auflisten, bearbeiten, verschieben und entfernen

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

const [a] = await doc.annotations(0);
await doc.setAnnotationContents(a.num, a.gen, 'Updated note');
await doc.moveAnnotation(a.num, a.gen, 10, -20);     // dx, dy in points
await doc.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:

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

XFDF-Import und -Export

const xfdf = await doc.exportXfdf();
const other = await NexPdf.open(cleanBase64);
await other.importXfdf(xfdf);

Dateianhänge

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

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

Auf dieser Seite