Add an API for Flutter
Introduction
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:
readOnlyviewer configuration option that determines if the viewer will allow edits to the document.getPageCropBoxfunction that returns aPTRectobject containing information about the crop box of a specified page.startZoomChangedListenerevent 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.
1. Fork and clone Apryse's Flutter Repo
The source is hosted on GitHub here: https://github.com/ApryseSDK/pdftron-flutter
Fork the project and clone a copy of the repository to your disk.
Adding the readOnly viewer configuration option
2. Define the new viewer configuration option in Dart
The config.dart file is responsible for setting all of the viewer configuration options.
3. Define the new viewer configuration option in Java
First define a config constant for the property in /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java:
4. Set the property
In the same file, /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, add the code to set the property:
Adding the `getPageCropBox` function
2. Define the new function in Dart
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:
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:
In pdftron_flutter.dart, add the getPageCropBox method to the PdftronFlutter 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.
3. Receive the new function from method channel
First, define the function and argument constants in /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, (constants are grouped according to purpose):
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:
Option 2
You would like the new function implemented for the widget version.
Open the /android/src/main/java/com/pdftron/pdftronflutter/FlutterDocumentView.java file and add a new case for the new function to onMethodCall:
Option 3
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:
4. Implement the new function
Following step 3, 3 options will be listed below.
Option 1
You would like the new function implemented for the plugin version.
Open the /android/src/main/java/com/pdftron/pdftronflutter/FlutterDocumentActivity.java file and implement the new function:
Option 2
You would like the new function implemented for the widget version.
Open the /android/src/main/java/com/pdftron/pdftronflutter/views/DocumentView.java file and implement the new function:
Option 3
You would like the new function implemented for both versions.
In the same file /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, implement the new function:
The logic is to first get the current doc, get the crop box and encode all the associated information into a JSON string as the result.
The actual implementation will depend on the actual functionality.
Adding startZoomChangedListener
2. Define the new listener in Dart
The events.dart file is where all event listeners are defined and implemented. In this file do the following:
add a constant to represent the event channel.
declare a type alias for the listener.
add an id to the enum
eventSinkId.add a method that sets up a stream for our event.
3. Define event emitter
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.
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:
define the event emitter,
add a setter, and
override the getter.
4. Add event constant
In /android/src/main/java/com/pdftron/pdftronflutter/helpers/PluginUtils.java, add a constant for the 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.
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.
6. Send event to Flutter
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.
The actual implementation will depend on the actual functionality.
Finishing steps
1. Push the code and integrate the updated source
Now update your library with the new code.
The new functionality is now ready to use.
2. Access the new functionality
The app can now access the new APIs as follows:
Plugin version:
Or:
Widget version:
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.
Last updated
Was this helpful?