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.
In ios/Classes/PdftronFlutterPlugin.m, we iterate through the config and check for the presence of keys. Add the config constant that you defined in the previous step, and set the property:
Now the document associated with the PTPDFViewCtrl is read-only.
## 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:
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.
8 NSLog(@"Error: The document view controller has no document.");
9 flutterResult([FlutterError errorWithCode:@"get_page_crop_box" message:@"Failed to get page crop box" details:@"Error: The document view controller has no document."]);
43 NSLog(@"Error: There was an error while trying to get the page crop box. %@", error.localizedDescription);
44 }
45}
The logic is to first get the current document view controller, lock the file and check whether the document is still being downloaded. If not, 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:
The two methods below, onListenWithArguments:eventSink: and onCancelWithArguments:eventSink:, are used to start and cancel the event listener respectively.
In the same file, add a case for our event id to each method:
In the next step, we will call this method whenever the event occurs.
6. Receive event from PDFViewCtrl
The PTPDFViewCtrlDelegate protocol contains the method pdfViewCtrl:pdfScrollViewDidZoom:. This method allows adopting delegates to respond to the PTPDFViewCtrl class when the zoom event occurs.
Implement this method in ios/Classes/PTFlutterDocumentController.m:
10 print("flutter zoom changed. Current zoom is: $zoom");
11});
12
13zoomChangedCancel(); // Cancels the listener
3. All done!
If you're only developing for iOS, then you're all done!
If you're also deploying on Android, you'll need to complete the necessary steps for Android.
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.