Aller au contenu
NexPDF

Annotations

Laissez les utilisateurs annoter des PDF dans le lecteur natif, ou ajoutez, listez, supprimez et aplatissez des annotations depuis Dart via l'API C de NexPDF, avec NexPDF pour Flutter.

Français

Module de licence

La création et la modification des annotations nécessitent le module annotations (niveau Indie et supérieur). Lister les annotations avec nexpdf_annotations_json fonctionne sans clé.

Dans le lecteur

Le lecteur natif a un mode annotations avec surlignage, encre, formes, lignes, notes, texte libre, tampons et signatures. Limitez les outils avec annotationsConfig.availableTools :

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

Depuis le code

Les réponses et les états de révision ont des méthodes Dart ; voir Fils de commentaires. Les autres fonctions d'annotation n'ont pas de méthode Dart. Appelez-les via dart:ffi sur doc.handle. Les coordonnées sont en points PDF, avec l'origine en bas à gauche de la page.

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 est un sous-type PDF comme 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.

Lister et supprimer

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

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

Autres fonctions d'annotation

Fonction CEffet
nexpdf_add_ink(doc, page, color_hex, stroke_width, opacity, coords)Encre à main levée ; coords vaut "x,y;x,y;..."
nexpdf_add_line(doc, page, kind, color_hex, opacity, stroke_width, x1, y1, x2, y2)Une ligne ou une flèche (kind Arrow)
nexpdf_add_ink_signature(doc, page, color_hex, stroke_width, strokes)Une signature dessinée ; traits séparés par |
nexpdf_move_annotation(doc, num, generation, dx, dy)Déplace une annotation de quelques points
nexpdf_set_annotation_contents(doc, num, generation, contents)Modifie son texte
nexpdf_flatten_annotations(doc, page_index)Incruste les annotations dans la page ; -1 pour toutes les pages
nexpdf_export_xfdf(doc), nexpdf_import_xfdf(doc, xfdf)Export et import XFDF

Sur cette page