Events and Notifications

PSPDFKit for Flutter allows you to listen to various events that occur when the end user interacts with PSPDFKit.

PspdfkitWidget Events

PSPDFKit for Flutter supports the following events:

  • onDocumentLoaded — Called when a document is loaded with the loaded PdfDocument object. This event is triggered after the document is loaded and the first page is rendered.

  • onDocumentLoadFailed — Called when a document fails to load. The event delivers an error message with the reason for the failure.

  • onPageChanged — Called when a page is changed with the new page index. This event is triggered when a document is first loaded or when a user navigates to a new page.

To listen to these events, add an event listener to the PspdfkitWidget:

PspdfkitWidget(
  document: document,
  onDocumentLoaded: (pdfDocument) {
    print('Document loaded');
  },
  onDocumentLoadFailed: (error) {
    print('Document load failed: $error');
  },
  onPageChanged: (pageIndex) {
    print('Page changed to $pageIndex');
  },
);

Web-Only Events

PSPDFKit for Flutter Web supports annotation events. An event listener can be added to the PspdfkitWidget via the PspdfkitWidgetController:

pspdfkitWidgetController.addEventListener('<event-type>', (event) {
            // Do something with the event.
    });

A complete list of supported events can be found in the PSPDFKit for Web documentation.

Present Mode Events

The following table lists the events supported by PSPDFKit for Flutter in present mode.

Event Description
flutterPdfActivityOnPause Android only. Called when the PDF activity is paused.
pdfViewControllerWillDismiss iOS only. Called right before the dismissal of the PDF view controller.
pdfViewControllerDidDismiss iOS only. Called right after the dismissal of the PDF view controller.

Implementing a Callback Method

The example below shows how to implement a callback to get notified when the PDF activity is paused or when the PDF view controller is about to be dismissed.

// First implement the callback methods:

void flutterPdfActivityOnPauseHandler() {
  print('flutterPdfActivityOnPauseHandler');
}

void pdfViewControllerWillDismissHandler() {
  print('pdfViewControllerWillDismissHandler');
}

void pdfViewControllerDidDismissHandler() {
  print('pdfViewControllerDidDismissHandler');
}

// Next, register these callbacks with `Pspdfkit` once. This can be done in the `build` method of your widget.
Pspdfkit.flutterPdfActivityOnPause = () => flutterPdfActivityOnPauseHandler();
Pspdfkit.pdfViewControllerWillDismiss = () => pdfViewControllerWillDismissHandler();
Pspdfkit.pdfViewControllerDidDismiss = () => pdfViewControllerDidDismissHandler();

Now whenever any of these events happen, the corresponding method will trigger and print the relevant text.