Some test text!

Add an API

Add an API for React Native


The PDFTron Android React Native API 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.

1. Fork and clone PDFTron's React Native Repo

The source is hosted on GitHub here:

Fork the project and clone a copy of the repository to your disk.

Adding the pageChangeOnTap prop

2. Add prop to DocumentView.js interface file

The DocumentView.js interface file lists all of the React Native props on the DocumentView component.

Add the prop declaration:

static propTypes = {
  pageChangeOnTap: PropTypes.bool,

3. Define a ReactProp matches the JS declaration

Open file /android/src/main/java/com/pdftron/reactnative/viewmanagers/

Add the method key that matches the JS declaration:

@ReactProp(name = "pageChangeOnTap")
public void setPageChangeOnTap(DocumentView documentView, boolean pageChangeOnTap) {

4. Implement the method to set the property

Open file /android/src/main/java/com/pdftron/reactnative/views/

Add the implementation:

public void setPageChangeOnTap(boolean pageChangeOnTap) {
    Context context = getContext();
    if (context != null) {
        PdfViewCtrlSettingsManager.setAllowPageChangeOnTap(context, pageChangeOnTap);

The actual implementation will depend on the actual functionality.

Adding the getField function

2. Add function to DocumentView.js interface file

The DocumentView.js interface file lists all of the React Native functions on the DocumentView component.

Add the function declaration:

getField = (fieldName) => {
    const tag = findNodeHandle(this._viewerRef);
    if(tag != null) {
      return DocumentViewManager.getField(tag, fieldName);
    return Promise.resolve();

3. Receive the new method from JavaScript

Add this method to /android/src/main/java/com/pdftron/reactnative/modules/

public void getField(final int tag, final String fieldName, final Promise promise) {
    getReactApplicationContext().runOnUiQueueThread(new Runnable() {
        public void run() {
            try {
                WritableMap field = mDocumentViewInstance.getField(tag, fieldName);
            } catch (Exception ex) {

4. Define the caller method

Add the following to /android/src/main/java/com/pdftron/reactnative/viewmanagers/

public WritableMap getField(int tag, String fieldName) throws PDFNetException {
    DocumentView documentView = mDocumentViews.get(tag);
    if (documentView != null) {
        return documentView.getField(fieldName);
    } else {
        throw new PDFNetException("", 0L, getName(), "getField", "Unable to find DocumentView.");

5. Implement the helper method

Add the following to /android/src/main/java/com/pdftron/reactnative/views/

public WritableMap getField(String fieldName) throws PDFNetException {
    PDFViewCtrl pdfViewCtrl = getPdfViewCtrl();
    PDFDoc pdfDoc = pdfViewCtrl.getDoc();

    WritableMap fieldMap = null;
    boolean shouldUnlock = false;

    try {
        shouldUnlock = true;

        Field field = pdfDoc.getField(fieldName);

        if (field != null && field.isValid()) {
            fieldMap = Arguments.createMap();
            int fieldType = field.getType();
            String typeString;
            switch (fieldType) {
                case Field.e_button:
                    typeString = FIELD_TYPE_BUTTON;
                case Field.e_check:
                    typeString = FIELD_TYPE_CHECKBOX;
                    fieldMap.putBoolean(KEY_FIELD_VALUE, field.getValueAsBool());
                case Field.e_radio:
                    typeString = FIELD_TYPE_RADIO;
                    fieldMap.putString(KEY_FIELD_VALUE, field.getValueAsString());
                case Field.e_text:
                    typeString = FIELD_TYPE_TEXT;
                    fieldMap.putString(KEY_FIELD_VALUE, field.getValueAsString());
                case Field.e_choice:
                    typeString = FIELD_TYPE_CHOICE;
                    fieldMap.putString(KEY_FIELD_VALUE, field.getValueAsString());
                case Field.e_signature:
                    typeString = FIELD_TYPE_SIGNATURE;
                    typeString = FIELD_TYPE_UNKNOWN;

            fieldMap.putString(KEY_FIELD_NAME, fieldName);
            fieldMap.putString(KEY_FIELD_TYPE, typeString);
    } finally {
        if (shouldUnlock) {
    return fieldMap;

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.

Adding the onLayoutChanged event listener

2. Define the new listener in JS

Event listeners are handled in DocumentView.js. Add the following to the file:

static propTypes = {
    onLayoutChanged: PropTypes.func,

onChange = (event) => {
    if (event.nativeEvent.onLeadingNavButtonPressed) {
    } else if (event.nativeEvent.onLayoutChanged) {
      if (this.props.onLayoutChanged) {

The onLayoutChanged event listener does not require any arguments. If your event listener requires arguments, access them using event.nativeEvent.exampleArg.

3. Add event constant for Android

In android/src/main/java/com/pdftron/reactnative/utils/, add a constant to represent the event.

public static final String ON_LAYOUT_CHANGED = "onLayoutChanged";

4. Implement event listener

In /android/src/main/java/com/pdftron/reactnative/views/, create a new event listener to send the event to JS. This event listener will call the existing method onReceiveNativeEvent when layout changes. Note that the event you want to implement may be a part of an existing event listener.

private View.OnLayoutChangeListener mLayoutChangedListener = new OnLayoutChangeListener() {
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {

        WritableMap params = Arguments.createMap();
        params.putString(ON_LAYOUT_CHANGED, ON_LAYOUT_CHANGED);


5. Set up and remove event listener

In the same file, /android/src/main/java/com/pdftron/reactnative/views/, add the event listener to our PDFViewCtrl when the document has loaded, and remove the listener when it is no longer necessary.

public void onTabDocumentLoaded(String tag) {

protected void onDetachedFromWindow() {
    if (getPdfViewCtrl() != null) {

The actual implementation will depend on the actual functionality.

Finishing steps

1. Push the code and integrate the updated source

Now npm install your forked version to your application.

The new functionality is now ready to use.

2. Access the new functionality

The app can now access the new API as follows:

onDocumentLoaded = () => {
    this._viewer.getField('someFieldName').then((field) => {
        if (field !== undefined) {
            console.log('field name:', field.fieldName);
            console.log('field value:', field.fieldValue);
            console.log('field type:', field.fieldType);

onLayoutChanged = () => {
    console.log("Layout changed");

    ref={(c) => this._viewer = c}

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 PDFTron customers.

Get the answers you need: Support