Frameworks
React Native
Flutter
Version 10
Version 9
Version 8
Version 7
Version 6
Version 5
## Introduction
The React Native 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 React Native.
This guide provides an example of how to add the following to the React Native interface:
pageChangeOnTap
prop that determines whether page will turn left or right when tapping corresponding edges.getField
function which retrieves information about a field using its name.onLayoutChanged
event listener that is raised when the layout of viewer has changed.You can follow the same pattern to add new functions and props that your React Native app may need. The new 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: Native UI Components to have a better understanding of the system.
The source is hosted on GitHub here: https://github.com/ApryseSDK/pdftron-react-native
Fork the project and clone a copy of the repository to your disk.
pageChangeOnTap
propThe DocumentView.tsx
interface file lists all of the React Native props on the DocumentView component.
Add the prop declaration:
For props that accept Config
constants or arrays of Config
constants, use the oneOf
and arrayOf
helper methods. These helpers return flexible types that allow custom checks for TypeScript users, while maintaining standard run-time checks for all users.
Open file ios/RNTPTDocumentViewManager.m
.
Add the method key that matches the TS declaration:
Open the ios/RNTPTDocumentView.h
interface file and add a new property to the RNTPTDocumentView
class:
Open the ios/RNTPTDocumentView.m
implementation file, and add the property setter:
In this case, we call the applyViewerSettings
method and access the changesPageOnTap
property to complete our implementation:
The actual implementation will depend on the actual functionality.
getField
functionThe DocumentView.tsx
interface file lists all of the React Native functions on the DocumentView component.
Add the function declaration:
All functions return Promise<void | T>
where T
is the expected return type upon success. If the function returns void
upon success, simply put Promise<void>
.
While you are adding types, consider representing objects with reusable object types. You can use existing ones, or create a new type alias or interface in AnnotOptions.ts
(see Object Types).
The JS name of the new method is getField
and the native method is getFieldForDocumentViewTag:fieldname:
.
In ios/RNTPTDocumentViewModule.m
, use the RCT_REMAP_METHOD
macro to expose the native method to JS and simultaneously specify the JS name of the method.
Add the native method to ios/RNTPTDocumentViewManager.h
:
Implement the native method in ios/RNTPTDocumentViewManager.m
:
Add the following to ios/RNTPTDocumentView.h
:
Implement the method in ios/RNTPTDocumentView.m
:
The logic is to get the PDF view controller and if the field names are valid, use the controller to get the fields from the current document.
onLayoutChanged
event listenerEvent listeners are handled in DocumentView.tsx
. Add the following to the file:
The onLayoutChanged
event listener does not require any arguments. If your event listener requires arguments, access them using event.nativeEvent.exampleArg
.
For event listeners and other props that accept functions, use the func
helper method. This helper returns flexible types that allow custom checks for TypeScript users, while maintaining standard run-time checks for all users.
While you are adding types, consider representing objects with reusable object types. You can use existing ones, or create a new type alias or interface in AnnotOptions.ts
(see Object Types).
Add the following protocol method to ios/RNTPTDocumentView.h
:
Implement the protocol method in ios/RNTPTDocumentViewManager.m
, under the Events pragma mark. Notice that we call RNTPTDocumentView
's RCTBubblingEventBlock onChange
event handler block, which will call the callback prop in JS.
layoutChanged
protocol method in iOSIn step 3, we implemented the protocol method layoutChanged
to send events to JS, but we haven't used it yet.
Add the following protocol method to ios/DocumentViewController/RNTPTDocumentViewController.h
:
Implement this protocol method in ios/RNTPTDocumentView.m
, under the RNTPTDocumentViewControllerDelegate pragma mark. This is where we use layoutChanged
.
The PTPDFViewCtrlDelegate
protocol contains the method pdfViewCtrlOnLayoutChanged
. This method allows adopting delegates to respond to the PTPDFViewCtrl
class when page layout has changed.
Implement pdfViewCtrlOnLayoutChanged
in the following files:
ios/DocumentViewController/RNTPTDocumentViewController.m
ios/DocumentViewController/RNTPTDocumentController.m
ios/DocumentViewController/RNTPTCollaborationDocumentViewController.m
ios/DocumentViewController/RNTPTCollaborationDocumentController.m
The actual implementation will depend on the actual functionality.
Now npm install
your forked version to your application.
The new functionality is now ready to use.
The app can now access the new API as follows:
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.
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales