Foxit PDF SDK for Android

How to Implement Foxit PDF SDK for Android using Xamarin

Implement Foxit PDF SDK for Android using Xamarin

Xamarin is a cross-platform development framework for building native apps using a shared C# codebase. We provide separate bindings for Android and iOS (‘foxit_xamarin_android’ and ‘foxit_xamarin_ios’) for developers to seamlessly integrate powerful PDF functionality of Foxit PDF SDK into their Xamarin apps.

This section will help you get started with Foxit PDF SDK for Android and the Xamarin framework on Windows. For other operating systems, you can take this tutorial as reference.

System Requirements

  • Visual Studio 2017
  • Android SDK
  • JDK 1.8
  • Foxit PDF SDK for Android

Note: The version of Foxit PDF SDK for Android should match the version of the ‘foxit_xamarin_android‘ that you choose and download, or the version that you install from the NuGet.

Install Xamarin in Visual Studio 2017

Please refer to the official installation instructions to install Xamarin in Visual Studio 2017.

Integrate Foxit PDF SDK into your Xamarin project

There are two ways to integrate Foxit PDF SDK into your Xamarin project:

  • Integrate with NuGet (supported from version 6.3)
  • Integrate manually by building and referencing DLLs

Integrate with NuGet

It is now possible to integrate Foxit PDF SDK into your Xamarin project through NuGet packages, which is much easier than the second way “Integrate manually by building and referencing DLLs”, and can save you much time.

To integrate Foxit PDF SDK into your Xamarin project through Nuget packages, please follow the steps below:

1) In the Solution Explorer, right-click the References node of your project, and click Manage NuGet Packages… as shown in Figure 1-1.

Figure 1-1

2) Then select the Browse tab, search for Foxit.Android, Foxit.Android.UIExtensions and Foxit.Android.Cropper, and then install the three packages.

From plugin 7.0 version, Foxit.Android.RxAndroid package is required to be installed (See Figure 1-2).

Figure 1-2

3) (Optional) If you want to open a RMS protected PDF document, you should install the Foxit.Android.Microsoft.Aad.Aadl, Foxit.Android.RMSSDK, and Foxit.Android.RMSSDK.UI packages as shown in Figure 1-2.

Note: The Foxit.Android.Microsoft.Aad.Aadl, Foxit.Android.RMSSDK, and Foxit.Android.RMSSDK.UI packages are required by the section “Open a RMS protected document“, so we also install these three packages. After finishing the above installation, the References of your project will look like Figure 1-3.

Figure 1-3

Integrate manually by building and referencing DLLs

To integrate manually, you should first build DLLs, and then add it as a reference to your project.

Build DLLs

1) Download the ‘foxit_xamarin_android’ from GitHub, and then build the related projects and get the DLLs, please follow the steps below:

Download Foxit PDF SDK for Android package, extract it, and copy the following files (libraries and licenses) in the “libs” folder of the extracted package to “foxit_xamarin_android\libs” directory:

  • FoxitRDK.aar
  • FoxitRDKUIExtensions.aar
  • rdk_key.txt
  • rdk_sn.txt

2) Get FoxitRDK.dll which is the heart of the SDK including the core functionalities of Foxit PDF SDK for Android.

Load FoxitRDK.sln in Visual Studio 2017 under the “foxit_xamarin_android\FoxitRDK” directory. Choose Build -> Build Solution to build the project, then the FoxitRDK.dll will be generated in “foxit_xamarin_android\FoxitRDK\FoxitRDK\bin\Debug (or release)” directory.

3) Get FoxitUIExtensions.dll which extends more powerful PDF related features including UI and resource files.

Load FoxitUIExtensions.sln in Visual Studio 2017 under “foxit_xamarin_android\FoxitUIExtensions” directory. Choose Build -> Build Solution to build the project, then the FoxitUIExtensions.dll will be generated in “foxit_xamarin_android\FoxitUIExtensions\FoxitUIExtensions\bin\Debug (or release)” directory.

Note:

  • In the “foxit_xamarin_android” folder, the complete_pdf_viewer project is a sample for building a full-featured PDF Reader. Before building this project, you should build the Cropper, FoxitRDK, RxAndroid (required by plugin 7.0), and FoxitUIExtensions projects first.
  • If you want to open a RMS protected PDF document, you should build Microsoft_Aad_Aadl, RMSSDK, and RMSSDK_UI projects in the “foxit_xamarin_android” folder to get the DLLs. Then, add the DLLs as references to your project.
Add the built DLLs as references to your project

This section takes FoxitRDK.dll as an example to show you how to add it as a reference to your project. For other DLLs, do the same steps with FoxitRDK.dll. We assume that you have got the FoxitRDK.dll, if not, please refer to Build DLLs section to build the FoxitRDK project and generate the FoxitRDK.dll.

1) In the Solution Explorer, right-click the References node of your project and select Add Reference… as shown in Figure 1-4.

Figure 1-4

2) In the Reference Manager dialog, click Browse… (See Figure 1-5) to find the FoxitRDK.dll (in the “foxit_xamarin_android\FoxitRDK\FoxitRDK\bin\Debug (or release)” folder), select it and then click Add, which is shown in Figure 1-6.

Figure 1-5

Figure 1-6

Then, the FoxitRDK.dll will appear in the list and has already been checked (See Figure 1-7). Click OK.

Figure 1-7

3) After finishing the above steps, the References of your project will look like Figure 1-8.

Figure 1-8

Note:

The section “Open a RMS protected document” requires the libraries related with RMS, so please refer to Build DLLs section to build the Microsoft_Aad_Aadl, RMSSDK, and RMSSDK_UI projects in the “foxit_xamarin_android” folder to get the following three DLLs:

Microsoft_Aad_Aadl\Microsoft_Aad_Aadl\bin\Debug (or release)\Microsoft_Aad_Aadl.dll
RMSSDK\RMSSDK\bin\Debug (or release)\RMSSDK.dll
RMSSDK_UI\RMSSDK_UI\bin\Debug (or release)\RMSSDK_UI.dll

 

Add the above three DLLs libraries as references to the project (refer to the steps of adding FoxitRDK.dll).

  • For section “Build a full-featured PDF Reader, you should refer to Build DLLs section to build the Cropper, RxAndroid, and FoxitUIExtensions projects in the “foxit_xamarin_android” folder to get the following three DLLs:
Cropper\Cropper\bin\Debug (or release)\Cropper.dll
RxAndroid\RxAndroid\bin\Debug (or release)\RxAndroid.dll
FoxitUIExtensions\FoxitUIExtensions\bin\Debug (or release)\FoxitUIExtensions.dllCropper\Cropper\bin\Debug (or release)\Cropper.dll

 

Add the above three DLLs libraries as references to the project (refer to the steps of adding FoxitRDK.dll).

After finishing the above steps, the References of your project will look like Figure 1-9.

Figure 1-9

Note: When building the necessary libraries and resource files for your Xamarin project, please make sure to use the same environment and machine where your project is being developed, and avoid copying and pasting your DLLs on different machines or environments such as Windows to Mac or Linux and vice-versa. Although the DLLs are perfectly functional on testing environment, they can cause issues with the signing code when you publish your application.

Build a Xamarin Android project using Foxit PDF SDK for Android

This section will help you to quickly build a full-featured PDF Reader in Xamarin Android platform with step-by-step instructions provided.

Create a new Xamarin Android project

Open Visual Studio 2017, choose File -> New -> Project… to start the New Project wizard. Create a new Xamarin Android project called “TestXamarin” which is shown in Figure 1-10. Then click OK.

Figure 1-10

In the New Android App dialog, select Single View App as shown in Figure 1-11. Then, click OK.

Figure 1-11

Integrate Foxit PDF SDK into the project

Please refer to section “Integrate Foxit PDF SDK into your Xamarin project” to integrate the Foxit PDF SDK into the created project. We recommend using the first way “Integrate with NuGet”, which is more easy and convenient.

Initialize Foxit PDF SDK Library

Before calling any APIs, you must first initialize Foxit PDF SDK library by using the function Library.Initialize(sn, key). Below you can see an example of how to initialize the SDK library. The next section will show you where to include this code in “MainActivity.cs” file.

using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
...
String sn = "xxx";
String key = "xxx";
int errCode = Library.Initialize(sn, key);
if (errCode != Constants.EErrSuccess)
  return;

Note: The value of “sn” can be found in the “rdk_sn.txt” (the string after “SN=”) and the value of “key” can be found in the “rdk_key.txt” (the string after “Sign=”). The trial license files (rdk_sn.txt and rdk_key.txt) can be found in the “foxit_xamarin_android\libs” folder or in the “libs” folder of Foxit PDF SDK for Android package.

Display a PDF document using PDFViewCtrl

To display a PDF document, please follow the steps below:

1) Instantiate a PDFViewCtrl object to show an existing document.

In “MainActivity.cs” file, instantiate a PDFViewCtrl object, and call PDFViewCtrl.OpenDoc function to open and render the PDF document.

using Com.Foxit.Sdk;
...
private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";
private PDFViewCtrl pdfViewCtrl;
...
// Instantiate a PDFViewCtrl object.
pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);

// Open and Render a PDF document.
pdfViewCtrl.OpenDoc(path, null);
SetContentView(pdfViewCtrl);

Note: Please make sure you have pushed the “complete_pdf_viewer_guide_android.pdf” document found in the “samples/test_files” folder of Foxit PDF SDK for Android package into the SD card of the Android device or emulator that will be used to run this project. Certainly, you can change the file path with your own files.

Update MainActivity.cs as follows:

using System;
using Android.App;
using Android.OS;
using Android.Support.V7.App;

using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;

namespace TestXamarin
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {

        // The value of "sn" can be found in the "rdk_sn.txt".
        // The value of "key" can be found in the "rdk_key.txt".
        private String sn = "xxx";
        private String key = "xxx";

        private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";

        private PDFViewCtrl pdfViewCtrl;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Initialize Foxit SDK Library.
            int errCode = Library.Initialize(sn, key);
            if (errCode != Constants.EErrSuccess)
                return;

            // Instantiate a PDFViewCtrl object.
            pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);

            // Open and Render a PDF document.
            pdfViewCtrl.OpenDoc(path, null);
            SetContentView(pdfViewCtrl);
        }
    }
}

2) Set permissions to write and read the SD card of the Android devices or emulators.

Note: If you want to run this project on an Android 6.0 (API 23) or higher devices/emulators, you can do one of the following:

a) Change the android:targetSdkVersion inAndroidManifest.xml to a lower SDK version that is less than 23, such as 21.

b) Write additional code to require the authorization of runtime permissions.

a) Change the android:targetSdkVersion in “AndroidManifest.xml”

In this case, set the “users-permission” in the “AndroidManifest.xml” found in the “TestXamarin\Properties” to give the project permission to write and read the SD card of the Android devices or emulators (See Figure 1-12).

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Figure 1-12

b) Write additional code to require the authorization of runtime permissions.

In the MainActivity.cs file, add code to require the authorization of runtime permissions. So, update the whole MainActivity.cs as follows:

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;

using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
using Android;
using Android.Content.PM;
using Android.Support.V4.Content;
using Android.Support.V4.App;

namespace TestXamarin
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        public static int REQUEST_EXTERNAL_STORAGE = 1;
        private static string[] PERMISSIONS_STORAGE = {
            Manifest.Permission.ReadExternalStorage,
            Manifest.Permission.WriteExternalStorage
        };

        // The value of "sn" can be found in the "rdk_sn.txt".
        // The value of "key" can be found in the "rdk_key.txt".
        private String sn = "xxx";
        private String key = "xxx";

        private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";

        private PDFViewCtrl pdfViewCtrl;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Initialize Foxit SDK Library.
            int errCode = Library.Initialize(sn, key);
            if (errCode != Constants.EErrSuccess)
                return;

            // Instantiate a PDFViewCtrl object.
            pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);

            // Require the authorization of runtime permissions.
            if (Build.VERSION.SdkInt > BuildVersionCodes.M)
            {
                Permission permission = ContextCompat.CheckSelfPermission(this.ApplicationContext, Manifest.Permission.WriteExternalStorage);
                if (permission != Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }
            }

            // Open and Render a PDF document.
            pdfViewCtrl.OpenDoc(path, null);
            SetContentView(pdfViewCtrl);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            if (requestCode == REQUEST_EXTERNAL_STORAGE
             && grantResults[0] == Permission.Granted)
            {
                if (pdfViewCtrl != null)
                {
                    pdfViewCtrl.OpenDoc(path, null);
                }
            }
            else
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }
}

3) Enable Multi-Dex. If you don’t, you may encounter the problem “error MSB6006: “java.exe” exited with code 2.”

Right-click the TestXamarin project, choose Properties, check Enable Multi-Dex under Android Options -> Packaging properties (See Figure 1-13).

Figure 1-13

In this chapter, we build and run the project on an AVD targeting 9.0 (API 28), and use the second method (require authorization of runtime permissions) to get the permissions to write and read the SD card of the emulator.

After building the project and installing APK on the emulator, tap Allow on the pop-up window to allow the project to access files on the device, and then you will see that the “complete_pdf_viewer_guide_android.pdf” document is displayed as shown in Figure 1-14. Now, this sample app has some basic PDF features, such as zooming in/out and page turning.

Figure 1-14

Open a RMS protected document

To open a RMS protected document, please make sure you have added the references of the libraries related with RMS. If not, please refer to “Integrate Foxit PDF SDK into your Xamarin project”. Then, follow the steps below:

1) Set the associated activity before opening a PDF document, because it has UI operations when opening a RMS protected document.

pdfViewCtrl.AttachedActivity = this;

2) Handle the activity result from the UI operations. Call the API “pdfViewCtrl.HandleActivityResult()” on the related OnActivityResult function.

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    pdfViewCtrl.HandleActivityResult(requestCode, (int)resultCode, data);
}

Based on the previous section “Display a PDF document using PDFViewCtrl“, update the whole contents of MainActivity.cs as follows:

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;

using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
using Android;
using Android.Content.PM;
using Android.Support.V4.Content;
using Android.Support.V4.App;
using Android.Content;

namespace TestXamarin
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        public static int REQUEST_EXTERNAL_STORAGE = 1;
        private static string[] PERMISSIONS_STORAGE = {
            Manifest.Permission.ReadExternalStorage,
            Manifest.Permission.WriteExternalStorage
        };

        // The value of "sn" can be found in the "rdk_sn.txt".
        // The value of "key" can be found in the "rdk_key.txt".
        private String sn = "xxx";
        private String key = "xxx";

        private String path = "/mnt/sdcard/Sample_RMS.pdf";

        private PDFViewCtrl pdfViewCtrl;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Initialize Foxit SDK Library.
            int errCode = Library.Initialize(sn, key);
            if (errCode != Constants.EErrSuccess)
                return;

            // Instantiate a PDFViewCtrl object.
            pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);

            // Set the associated activity for RMS UI operations.
            pdfViewCtrl.AttachedActivity = this;

            // Require the authorization of runtime permissions.
            if (Build.VERSION.SdkInt > BuildVersionCodes.M)
            {
                Permission permission = ContextCompat.CheckSelfPermission(this.ApplicationContext, Manifest.Permission.WriteExternalStorage);
                if (permission != Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }
            }

            // Open and Render a PDF document.
            pdfViewCtrl.OpenDoc(path, null);
            SetContentView(pdfViewCtrl);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            if (requestCode == REQUEST_EXTERNAL_STORAGE
             && grantResults[0] == Permission.Granted)
            {
                if (pdfViewCtrl != null)
                {
                    pdfViewCtrl.OpenDoc(path, null);
                }
            }
            else
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }

        // Used for opening a RMS document.
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            pdfViewCtrl.HandleActivityResult(requestCode, (int)resultCode, data);
        }
    }
}

Note: Please make sure you have pushed a RMS protected document for example “Sample_RMS.pdf” into the SD card of the Android device or emulator that will be used to run this project.

Run the project

After building the project and installing APK on the emulator, tap Allow on the pop-up window to allow the project to access files on the device, and then you will see the following window (Figure 1-15) which prompts you to input your organizational email, and later input the password, then you can open the RMS protected document.

Figure 1-15

Build a full-featured PDF Reader

If you want to build a full-featured PDF reader (such like including commenting, editing features, and so on), please make sure you have added the references of the RxAndroid, Cropper and UIExtensions libraries. If not, please refer to “Integrate Foxit PDF SDK into your Xamarin project“. Then, follow the steps below:

Set the required permissions in the “AndroidManifest.xml” found in the “TestXamarin\Properties” (See Figure 1-16).

Figure 1-16

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

2) Add the core code below to “MainActivity.cs” file.

a) Set the system theme to “No Title” mode and set the window to Fullscreen, otherwise the layout of the built-in features might be affected.

// Turn off the title at the top of the screen.
RequestWindowFeature(WindowFeatures.NoTitle);

// Set the window to Fullscreen.
Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);

b) Instantiate a UIExtensionsManager object and set it to PDFViewCtrl.

using Com.Foxit.Uiextensions;


...
private UIExtensionsManager uiExtensionsManager;


...
uiExtensionsManager = new UIExtensionsManager(this.ApplicationContext, pdfViewCtrl);
uiExtensionsManager.AttachedActivity = this;
uiExtensionsManager.OnCreate(this, pdfViewCtrl, savedInstanceState);
pdfViewCtrl.UIExtensionsManager = uiExtensionsManager;

c) Open and render a PDF document, and set the content view. Call uiExtensionsManager.OpenDocument() function to open and render a PDF document instead of calling pdfViewCtrl.OpenDoc() function.

uiExtensionsManager.OpenDocument(path, null);
SetContentView(uiExtensionsManager.ContentView);

d) Add ConfigurationChanges = ConfigChanges.KeyboardHidden|ConfigChanges.Orientation|ConfigChanges.ScreenSize property to make sure that the project will only execute the onConfigurationChanged() function without recalling the activity lifecycle when rotating the screen. If you do not add this property, the signature feature will not work correctly.

[Activity(Label = "@string/app_name", ConfigurationChanges = ConfigChanges.KeyboardHidden|ConfigChanges.Orientation|ConfigChanges.ScreenSize, Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]

Update the whole content of MainActivity.cs file as follows:

Note: The Activity Lifecycle Events should be handled as below, otherwise some features may not work correctly.

using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;

using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
using Android;
using Android.Content.PM;
using Android.Support.V4.Content;
using Android.Support.V4.App;
using Android.Content;
using Com.Foxit.Uiextensions;
using Android.Views;
using Android.Content.Res;

namespace TestXamarin
{

    [Activity(Label = "@string/app_name", ConfigurationChanges = ConfigChanges.KeyboardHidden|ConfigChanges.Orientation|ConfigChanges.ScreenSize, Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        public static int REQUEST_EXTERNAL_STORAGE = 1;
        private static string[] PERMISSIONS_STORAGE = {
            Manifest.Permission.ReadExternalStorage,
            Manifest.Permission.WriteExternalStorage
        };

        // The value of "sn" can be found in the "rdk_sn.txt".
        // The value of "key" can be found in the "rdk_key.txt".
        private String sn = "xxx";
        private String key = "xxx";

        private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";

        private PDFViewCtrl pdfViewCtrl;
        private UIExtensionsManager uiExtensionsManager;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Initialize Foxit SDK Library.
            int errCode = Library.Initialize(sn, key);
            if (errCode != Constants.EErrSuccess)
                return;

            // Turn off the title at the top of the screen.
            RequestWindowFeature(WindowFeatures.NoTitle);

            // Set the window to Fullscreen.
            Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
            // Instantiate a PDFViewCtrl object.
            pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);

            // Set the associated activity for RMS UI operations.
            pdfViewCtrl.AttachedActivity = this;

            // Initialize a UIExtensionManager object and set it to PDFViewCtrl.
            uiExtensionsManager = new UIExtensionsManager(this.ApplicationContext, pdfViewCtrl);
            uiExtensionsManager.AttachedActivity = this;
            uiExtensionsManager.OnCreate(this, pdfViewCtrl, savedInstanceState);
            pdfViewCtrl.UIExtensionsManager = uiExtensionsManager;

            // Require the authorization of runtime permissions.
            if (Build.VERSION.SdkInt > BuildVersionCodes.M)
            {
                Permission permission = ContextCompat.CheckSelfPermission(this.ApplicationContext, Manifest.Permission.WriteExternalStorage);
                if (permission != Permission.Granted)
                {
                    ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
                    return;
                }
            }

            // Open and Render a PDF document.
            uiExtensionsManager.OpenDocument(path, null);
            SetContentView(uiExtensionsManager.ContentView);
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
        {
            if (requestCode == REQUEST_EXTERNAL_STORAGE
             && grantResults[0] == Permission.Granted)
            {
                if (uiExtensionsManager != null)
                {
                    uiExtensionsManager.OpenDocument(path, null);
                    SetContentView(uiExtensionsManager.ContentView);
                }
            }
            else
            {
                base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }

        // Used for opening a RMS document.
        protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
        {
            pdfViewCtrl.HandleActivityResult(requestCode, (int)resultCode, data);
        }

        protected override void OnStart()
        {
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnStart(this);
            }
            base.OnStart();
        }

        protected override void OnStop()
        {
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnStop(this);
            }
            base.OnStop();
        }

        protected override void OnPause()
        {
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnPause(this);
            }
            base.OnPause();
        }

        protected override void OnResume()
        {
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnResume(this);
            }
            base.OnResume();
        }

        protected override void OnDestroy()
        {
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnDestroy(this);
            }
            base.OnDestroy();
        }

        public override void OnConfigurationChanged(Configuration newConfig)
        {
            base.OnConfigurationChanged(newConfig);
            if (uiExtensionsManager != null)
            {
                uiExtensionsManager.OnConfigurationChanged(this, newConfig);
            }
        }

        public override bool OnKeyDown([GeneratedEnum] Keycode keyCode, KeyEvent e)
        {
            if (uiExtensionsManager != null && uiExtensionsManager.OnKeyDown(this, (int)keyCode, e)) return true;
            return base.OnKeyDown(keyCode, e);
        }
    }
}

Run the project

After building the project and installing APK on the emulator, tap Allow on the pop-up window, and then you will see that the “complete_pdf_viewer_guide_android.pdf” document is displayed as shown in Figure 1-17. Now, it is a full-featured PDF Reader which includes all the features in Foxit PDF SDK for Android and it also supports to open a RMS protected document.

Figure 1-17

Customize the UI

For how to customize the UI in your Xamarin Android project, you can refer to the “Customizing User Interface” section of the Developer Guide for Foxit PDF SDK for Android.

Updated on February 12, 2019

Was this article helpful?
Thanks for your feedback. If you have a comment on how to improve the article, you can write it here: