Retrieve or Set the Annotation Bounding Box in Flutter

Each annotation has a size and a position, known as the annotation’s bounding box. To access the bounding box of a given annotation, you need to call getAnnotations(pageIndex, type) (available only in iOS) to get the annotation. Then, you can access its bounding box (bbox). The value of the bounding box is a rect array instance, which holds the annotation’s size and position in PDF coordinates.

The example below shows how to add a button that displays an alert with the value of the bounding box of the first annotation on the first page of the document:

import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'package:pspdfkit_flutter/src/pspdfkit_view.dart';

class PspdfkitAnnotationsExampleWidget extends StatefulWidget {
  final String documentPath;
  final dynamic configuration;

  PspdfkitAnnotationsExampleWidget(
      {Key? key, required this.documentPath, this.configuration})
      : super(key: key);

  @override
  PspdfkitAnnotationsExampleWidgetState createState() =>
      PspdfkitAnnotationsExampleWidgetState();
}

class PspdfkitAnnotationsExampleWidgetState
    extends State<PspdfkitAnnotationsExampleWidget> {
  late PspdfkitView view;

  @override
  Widget build(BuildContext context) {
    if (defaultTargetPlatform == TargetPlatform.iOS) {
      return CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(),
          child: SafeArea(
              bottom: false,
              child: Column(children: <Widget>[
                Expanded(
                    child: UiKitView(
                        viewType: 'com.pspdfkit.widget',
                        onPlatformViewCreated: onPlatformViewCreated,
                        creationParamsCodec: const StandardMessageCodec())),
                Container(
                    height: 80,
                    child: Row(children: <Widget>[
                      Container(width: 20),
                      CupertinoButton(
                          onPressed: () async {
                            dynamic annotations = await view.getAnnotations(0, '');
                            await showCupertinoDialog<CupertinoAlertDialog>(
                                context: context,
                                builder: (BuildContext context) =>
                                    CupertinoAlertDialog(
                                      title: Text('Bounding box'),
                                      content: Text('${annotations[0]['bbox']}'),
                                      actions: [
                                        TextButton(
                                            onPressed: () {
                                              Navigator.of(context).pop();
                                            },
                                            child: Text('OK'))
                                      ],
                                    ));
                          },
                          child: Text('Get Bounding Box')),
                      Container(width: 20)
                    ]))])));
    } else {
      // `PspdfkitView` is only supported in iOS at the moment.
      // Support for other platforms is coming soon.
      return Text('$defaultTargetPlatform is not yet supported by pspdfkit.');
    }
  }

  Future<void> onPlatformViewCreated(int id) async {
    view = PspdfkitView.init(id, widget.documentPath, widget.configuration);
  }
}

Here’s how it looks:

Bounding box alert