getAnnotations method

Future<List<CPDFAnnotation>> getAnnotations()

Retrieves all annotations on the current page.

This method fetches all annotations present on the current page of the PDF document and returns a list of corresponding CPDFAnnotation instances.

The returned annotations can include different types, such as:

example:

int pageIndex = 0;
CPDFPage page = controller.document.pageAtIndex(pageIndex);
List<CPDFAnnotation> annotations = await page.getAnnotations();

Returns a list of CPDFAnnotation objects for the current page. If no annotations are found, an empty list is returned. If an error occurs during parsing, it logs the error and skips the invalid annotation data.

Note: The list is filtered to exclude any invalid or malformed annotations.

Implementation

Future<List<CPDFAnnotation>> getAnnotations() async {
  final rawList = await _channel.invokeMethod('get_annotations', pageIndex);
  if (rawList is! List) return [];
  return rawList
      .whereType<Map>()
      .map((item) {
        try {
          final map = Map<String, dynamic>.from(item);
          return CPDFAnnotationRegistry.fromJson(map);
        } catch (e, stack) {
          debugPrint('CPDFAnnotation parse error: $e\n$stack');
          return null;
        }
      })
      .whereType<CPDFAnnotation>()
      .toList();
}