Ir al contenido
NexPDF

Anotaciones

Permite que los usuarios anoten PDF en el lector nativo, o añade, enumera, elimina y aplana anotaciones desde Dart mediante la API de C de NexPDF, con NexPDF para Flutter.

Español

Módulo de licencia

Crear y modificar anotaciones necesita el módulo annotations (nivel Indie y superiores). Enumerar las anotaciones con nexpdf_annotations_json funciona sin clave.

En el lector

El lector nativo tiene un modo de anotaciones con resaltado, tinta, formas, líneas, notas, texto libre, sellos y firmas. Limita las herramientas con annotationsConfig.availableTools:

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

Desde código

Las respuestas y los estados de revisión tienen métodos de Dart; consulta Hilos de comentarios. Las demás funciones de anotación no tienen método de Dart. Llámalas mediante dart:ffi sobre doc.handle. Las coordenadas son puntos PDF con el origen en la esquina inferior izquierda de la página.

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 es un subtipo de PDF como Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText o Text (una nota adhesiva). El marcado de texto se ajusta a las líneas que hay bajo el rectángulo.

Enumerar y eliminar

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

Las anotaciones se identifican por su número de objeto (num) y su generación (gen).

Otras funciones de anotación

Función CQué hace
nexpdf_add_ink(doc, page, color_hex, stroke_width, opacity, coords)Tinta a mano alzada; coords es "x,y;x,y;..."
nexpdf_add_line(doc, page, kind, color_hex, opacity, stroke_width, x1, y1, x2, y2)Una línea o una flecha (kind Arrow)
nexpdf_add_ink_signature(doc, page, color_hex, stroke_width, strokes)Una firma dibujada; los trazos se separan con |
nexpdf_move_annotation(doc, num, generation, dx, dy)Mueve una anotación en puntos
nexpdf_set_annotation_contents(doc, num, generation, contents)Cambia su texto
nexpdf_flatten_annotations(doc, page_index)Integra las anotaciones en la página; -1 para todas las páginas
nexpdf_export_xfdf(doc), nexpdf_import_xfdf(doc, xfdf)Exportación e importación de XFDF

En esta página