Zum Inhalt springen
NexPDF

Annotationen

Lassen Sie Benutzer PDFs im nativen Reader annotieren oder fügen Sie mit NexPDF für Flutter Annotationen aus Dart über die NexPDF-C-API hinzu, listen, entfernen und verflachen Sie sie.

Deutsch

Lizenzmodul

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

Im Reader

Der native Reader hat einen Annotationsmodus mit Hervorhebung, Freihand, Formen, Linien, Notizen, Freitext, Stempeln und Signaturen. Begrenzen Sie die Werkzeuge mit annotationsConfig.availableTools:

await NexPdfReader.open(
  document: pdfBytes,
  config: NexPdfUIConfig.fromJson({
    'modeConfig': {'initialMode': 'annotations'},
    'annotationsConfig': {'availableTools': ['highlight', 'ink', 'note'], 'defaultColor': '#FFD400'},
  }),
);

Aus Code

Antworten und Prüfstatus haben Dart-Methoden; siehe Kommentarthreads. Die übrigen Annotationsfunktionen haben keine Dart-Methode. Rufen Sie sie über dart:ffi auf doc.handle auf. Koordinaten sind PDF-Punkte mit dem Ursprung unten links auf der Seite.

import 'dart:convert';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'nexpdf_ffi.dart';

// int32_t nexpdf_add_annotation(NexPdfDocument *doc, size_t page_index, const char *kind,
//     const char *color_hex, double opacity, const char *contents,
//     double llx, double lly, double urx, double ury);
final addAnnotation = nexpdfLib.lookupFunction<
    Int32 Function(Pointer<Void>, Size, Pointer<Utf8>, Pointer<Utf8>, Double, Pointer<Utf8>, Double, Double, Double, Double),
    int Function(Pointer<Void>, int, Pointer<Utf8>, Pointer<Utf8>, double, Pointer<Utf8>, double, double, double, double)>(
    'nexpdf_add_annotation');

// Highlight every search hit.
using((a) {
  for (final hit in doc.search('termination')) {
    nexpdfCall(() => addAnnotation(doc.handle, hit.page, 'Highlight'.toNativeUtf8(allocator: a),
        '#FFD400'.toNativeUtf8(allocator: a), 1.0, ''.toNativeUtf8(allocator: a),
        hit.llx, hit.lly, hit.urx, hit.ury));
  }
});
final annotated = doc.save();

kind ist ein PDF-Subtyp wie Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText oder Text (eine Haftnotiz). Textmarkierungen werden an die Zeilen unter dem Rechteck angepasst.

Auflisten und entfernen

// char *nexpdf_annotations_json(const NexPdfDocument *doc, size_t page_index);
final annotationsJson = nexpdfLib.lookupFunction<
    Pointer<Utf8> Function(Pointer<Void>, Size),
    Pointer<Utf8> Function(Pointer<Void>, int)>('nexpdf_annotations_json');

// int32_t nexpdf_remove_annotation(NexPdfDocument *doc, size_t page_index, uint32_t num, uint16_t generation);
final removeAnnotation = nexpdfLib.lookupFunction<
    Int32 Function(Pointer<Void>, Size, Uint32, Uint16),
    int Function(Pointer<Void>, int, int, int)>('nexpdf_remove_annotation');

final annots = jsonDecode(takeString(annotationsJson(doc.handle, 0)) ?? '[]') as List;
for (final a in annots.cast<Map<String, Object?>>()) {
  print('${a['num']} ${a['gen']} ${a['kind']} ${a['contents']}');
}
final first = annots.first as Map<String, Object?>;
nexpdfCall(() => removeAnnotation(doc.handle, 0, first['num'] as int, first['gen'] as int));

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

Weitere Annotationsfunktionen

C-FunktionBewirkt
nexpdf_add_ink(doc, page, color_hex, stroke_width, opacity, coords)Freihandzeichnung; coords ist "x,y;x,y;..."
nexpdf_add_line(doc, page, kind, color_hex, opacity, stroke_width, x1, y1, x2, y2)Eine Linie oder ein Pfeil (kind Arrow)
nexpdf_add_ink_signature(doc, page, color_hex, stroke_width, strokes)Eine gezeichnete Signatur; Striche getrennt durch |
nexpdf_move_annotation(doc, num, generation, dx, dy)Verschiebt eine Annotation um Punkte
nexpdf_set_annotation_contents(doc, num, generation, contents)Ändert ihren Text
nexpdf_flatten_annotations(doc, page_index)Brennt Annotationen in die Seite ein; -1 für jede Seite
nexpdf_export_xfdf(doc), nexpdf_import_xfdf(doc, xfdf)XFDF-Export und -Import

Auf dieser Seite