The Flutter API for Apryse Mobile SDK includes all of the most used functions and methods for viewing, annotating and saving PDF documents. However, it is possible your app may need access to APIs that are available as part of the native API, but are not directly available to Flutter.
There are 2 different ways to use Apryse Flutter API on iOS or Android:
Present a document via a plugin.
Show an Apryse document view via a Widget.
This guide will demonstrate how to add a new functionality to both approaches.
The examples provided will show you how to add the following to the Flutter interface:
readOnly viewer configuration option that determines if the viewer will allow edits to the document.
getPageCropBox function that returns a PTRect object containing information about the crop box of a specified page.
startZoomChangedListener event listener that is raised when the zoom ratio is changed in the current document.
You can follow the same pattern to add new functions and viewer configuration options that your Flutter app may need. These additions could be simple ones, which expose one piece of functionality, or custom ones, that expose a series of native commands under the hood.
Prior to following this guide, we highly recommend you to go through the official guide here: Writing Platform-specific code to have a better understanding of the system.
The constants.dart file contains constants grouped by their purpose. The names of functions, parameters, buttons, toolbars, and other relevant constants are stored here. Add a constant to the Functions class:
Dart
1class Functions {
2 static const getPageCropBox = "getPageCropBox";
3}
The document_view.dart file handles function calls for the widget, while pdftron_flutter.dart handles function calls for the plugin. If you want to implement for both the widget and plugin then add functions to both files, otherwise add your function to whichever you prefer.
Note that since we use JSON to transfer the information between Flutter and the native code, one extra step of decoding is required here.
In document_view.dart, add the getPageCropBox method to the DocumentViewController class:
The options.dart file contains constants and classes for convenience. In this exercise, we add a new class called PTRect to this file for easier access of information.
First, define the function and argument constants in /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, (constants are grouped according to purpose):
Java
1public static final String KEY_PAGE_NUMBER = "pageNumber";
2...
3public static final String FUNCTION_GET_PAGE_CROP_BOX = "getPageCropBox";
Then, you have 3 options:
Option 1
You would like the new function implemented for the plugin version.
Open the /android/src/main/java/com/pdftron/pdftronflutter/PdftronFlutterPlugin.java file and add a new case for the new function to onMethodCall:
Java
1@Override
2public void onMethodCall(MethodCall call, Result result) {
You would like the new function implemented for both versions. Note that this option is only for convenience and easier maintenance; you could always just use Options 1 and/or 2.
Open the /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java file and add a new case for the new function to onMethodCall:
The ViewerComponent interface contains various methods that can be overridden by subclasses. In /android/src/main/java/com/pdftron/pdftronflutter/helpers/ViewerComponent.java, add a getter for an event emitter:
The FlutterDocumentActivity class extends ViewerComponent and is used for the plugin version of our APIs. Do the following steps in /android/src/main/java/com/pdftron/pdftronflutter/FlutterDocumentActivity.java:
define the event emitter,
add a setter,
override the getter, and
destroy the event emitter when it is no longer necessary.
Java
1private static AtomicReference<EventSink> sZoomChangedEventEmitter = new AtomicReference<>();
The DocumentView class extends ViewerComponent and is used for the widget version of our APIs. Do the following steps in /android/src/main/java/com/pdftron/pdftronflutter/views/DocumentView.java:
In /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, add a constant for the event.
Java
1public static final String EVENT_ZOOM_CHANGED = "zoom_changed_event";
5. Set up event channel
For the plugin version, set up the event channel and stream handler in /android/src/main/java/com/pdftron/pdftronflutter/PdftronFlutterPlugin.java. In the stream handler, set our event emitter when an event stream is created, and remove it when an event stream is torn down.
20 registrar.platformViewRegistry().registerViewFactory("pdftron_flutter/documentview", new DocumentViewFactory(registrar.messenger(), registrar.activeContext()));
21}
For the widget version, set up the event channel and stream handler in /android/src/main/java/com/pdftron/pdftronflutter/FlutterDocumentView.java. In the stream handler, set our event emitter when an event stream is created, and remove it when an event stream is torn down.
In /android/src/main/java/com/pdftron/pdftronflutter/helpers/ViewerImpl.java, set up an event listener that sends the event to Flutter. Note that the necessary listener may already exist.
Java
1private PDFViewCtrl.OnCanvasSizeChangeListener mOnCanvasSizeChangedListener = new PDFViewCtrl.OnCanvasSizeChangeListener() {
10 print("flutter zoom changed. Current zoom is: $zoom");
11});
12
13zoomChangedCancel(); // Cancels the listener
3. All done!
If you're only developing for Android, then you're all done!
If you're also deploying on iOS, you'll need to complete the necessary steps for iOS.
If you're developing for both iOS and Android, please consider submitting a PR, as upstreaming the change will simplify your developing and make the APIs available for other Apryse customers.