Annotationen
PDF-Annotationen mit NexPDF für PHP hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren oder exportieren.
Lizenzmodul
Das Erstellen und Ändern von Annotationen erfordert das Modul annotations (ab dem Tarif Indie). Das Auflisten von
Annotationen mit annotationsJson funktioniert ohne Schlüssel.
Koordinaten sind PDF-Punkt mit dem Ursprung unten links auf der Seite. Rechtecke sind
llx, lly, urx, ury.
Markierungen und Formen hinzufügen
addAnnotation($pageIndex, $kind, $colorHex, $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.
use Nexdevit\NexPdf\Document;
$doc = Document::open('contract.pdf');
$doc->addAnnotation(0, 'Highlight', '#FFD400', 1.0, '', 72, 700, 300, 716);
$doc->addAnnotation(0, 'FreeText', '#E5484D', 1.0, 'Checked', 72, 640, 260, 660);
$doc->addAnnotation(0, 'Text', '#2563EB', 1.0, 'Please confirm this date', 320, 700, 340, 720);
$doc->save('annotated.pdf');Linien, Pfeile und Freihandzeichnungen
// A red arrow from (100, 500) to (250, 560), 2 pt wide.
$doc->addLine(0, 'Arrow', '#DC2626', 1.0, 2.0, 100, 500, 250, 560);
// Freehand ink: points "x,y;x,y;..." in PDF points.
$doc->addInk(0, '#1D4ED8', 2.0, 1.0, '100,400;120,410;140,405;160,420');Eine handschriftliche Unterschrift ist eine Freihand-Annotation mit mehreren Strichen, die durch | getrennt sind:
$doc->addInkSignature(0, '#111827', 1.5, '300,120;320,130;340,118|350,125;380,128');Dies ist nur eine visuelle Unterschrift. Für eine kryptografische Signatur siehe Signaturen.
Bildstempel und Messungen
// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
$doc->addImageStamp(0, 400, 60, 520, 120, $rgba, 240, 120, 1.0);
// A stored distance measurement; per_point converts PDF points to the unit.
$m = ['kind' => 'distance', 'points' => [[100, 100], [300, 100]], 'unit' => 'mm', 'per_point' => 0.3528];
$result = json_decode($doc->addMeasurement(0, json_encode($m)), true);
echo $result['text'], "\n";kind ist distance, perimeter oder area. $doc->measurementsJson() listet die gespeicherten auf.
Auflisten, bearbeiten, verschieben und entfernen
foreach (json_decode($doc->annotationsJson(0), true) as $a) {
echo $a['num'], ' ', $a['gen'], ' ', $a['kind'], ' ', $a['contents'], "\n";
}
$a = json_decode($doc->annotationsJson(0), true)[0];
$doc->setAnnotationContents($a['num'], $a['gen'], 'Updated note');
$doc->moveAnnotation($a['num'], $a['gen'], 10, -20); // dx, dy in points
$doc->removeAnnotation(0, $a['num'], $a['gen']);Annotationen werden durch ihre Objektnummer (num) und Generation (gen) identifiziert.
Verflachen
Beim Verflachen werden Annotationen in den Seiteninhalt eingebrannt, sodass sie nicht mehr bearbeitet werden können:
$doc->flattenAnnotations(-1); // every page; pass a page index for one pageXFDF-Import und -Export
$xfdf = $doc->exportXfdf();
$other = Document::open('clean.pdf');
$other->importXfdf($xfdf);
$other->save('with-annotations.pdf');Dateianhänge
addFileAttachmentAnnot($pageIndex, $x, $y, $name, $data, $mime, $description) fügt ein Büroklammer-
Symbol hinzu, dessen obere linke Ecke bei ($x, $y) liegt:
$doc->addFileAttachmentAnnot(0, 520, 760, 'invoice.xml', file_get_contents('invoice.xml'),
'application/xml', 'Source data');Barcodes
QR-Codes, Data Matrix, PDF417 und lineare Barcodes aus PDF-Seiten und Bildern lesen und mit NexPDF für PHP echte Vektor-Barcodes zeichnen.
Formulare
PDF-Formularfelder mit NexPDF für PHP auflisten, ausfüllen, erstellen, neu berechnen und verflachen sowie Formulardaten als JSON, FDF oder XFDF austauschen.