Skip to Content
NexPDF

Annotations

Let users annotate PDFs in the native reader, or add, list, remove and flatten annotations from Dart through the NexPDF C API, with NexPDF for Flutter.

English

License module

Creating and changing annotations needs the annotations module (Indie tier and above). Listing annotations with nexpdf_annotations_json works without a key.

In the reader

The native reader has an annotations mode with highlight, ink, shapes, lines, notes, free text, stamps and signatures. Limit the tools with annotationsConfig.availableTools:

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

From code

Replies and review states have Dart methods; see Comment threads. The other annotation functions have no Dart method. Call them through dart:ffi on doc.handle. Coordinates are PDF points with the origin at the bottom-left of the 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 is a PDF subtype such as Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText or Text (a sticky note). Text markup is fitted to the lines under the rectangle.

List and remove

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

Annotations are identified by their object number (num) and generation (gen).

Other annotation functions

C functionDoes
nexpdf_add_ink(doc, page, color_hex, stroke_width, opacity, coords)Freehand ink; coords is "x,y;x,y;..."
nexpdf_add_line(doc, page, kind, color_hex, opacity, stroke_width, x1, y1, x2, y2)A line or arrow (kind Arrow)
nexpdf_add_ink_signature(doc, page, color_hex, stroke_width, strokes)A drawn signature; strokes separated by |
nexpdf_move_annotation(doc, num, generation, dx, dy)Moves an annotation by points
nexpdf_set_annotation_contents(doc, num, generation, contents)Changes its text
nexpdf_flatten_annotations(doc, page_index)Burns annotations into the page; -1 for every page
nexpdf_export_xfdf(doc), nexpdf_import_xfdf(doc, xfdf)XFDF export and import

On this page