annotations method

Future<List<UPdfAnnotationInfo>> annotations(
  1. int pageIndex
)

Implementation

Future<List<UPdfAnnotationInfo>> annotations(int pageIndex) async {
  final UPdfPage? page = await document.page(pageIndex);
  if (page == null) return const <UPdfAnnotationInfo>[];
  final UPdfDict source = (_changed[page.ref?.number] is UPdfDict ? _changed[page.ref?.number]! as UPdfDict : page.dict);
  final Object? list = await document.resolve(source["Annots"]);
  if (list is! List<Object?>) return const <UPdfAnnotationInfo>[];
  final List<UPdfAnnotationInfo> out = <UPdfAnnotationInfo>[];
  for (final Object? entry in list) {
    final Object? annotation = entry is UPdfRef ? await current(entry) : await document.resolve(entry);
    if (annotation is! UPdfDict) continue;
    final Object? subtype = annotation["Subtype"];
    final Object? rectObject = await document.resolve(annotation["Rect"]);
    final List<double> numbers = <double>[];
    if (rectObject is List<Object?>) {
      for (final Object? value in rectObject) {
        if (value is num) numbers.add(value.toDouble());
      }
    }
    if (numbers.length < 4) continue;
    final Object? contents = await document.resolve(annotation["Contents"]);
    final Object? author = await document.resolve(annotation["T"]);
    final Object? modified = await document.resolve(annotation["M"]);
    final Object? colorObject = await document.resolve(annotation["C"]);
    Color? color;
    if (colorObject is List<Object?> && colorObject.length >= 3) {
      final List<double> rgb = colorObject.whereType<num>().map((num value) => value.toDouble()).toList();
      if (rgb.length >= 3) color = Color.fromARGB(255, (rgb[0] * 255).round().clamp(0, 255), (rgb[1] * 255).round().clamp(0, 255), (rgb[2] * 255).round().clamp(0, 255));
    }
    out.add(
      UPdfAnnotationInfo(
        objectNumber: entry is UPdfRef ? entry.number : -1,
        pageIndex: pageIndex,
        subtype: subtype is UPdfName ? subtype.value : "",
        rect: Rect.fromLTRB(min(numbers[0], numbers[2]), min(numbers[1], numbers[3]), max(numbers[0], numbers[2]), max(numbers[1], numbers[3])),
        contents: contents is UPdfString ? contents.text : "",
        author: author is UPdfString ? author.text : "",
        modified: modified is UPdfString ? modified.date : null,
        color: color,
      ),
    );
  }
  return out;
}