How to Display a PDF with our Android PDF SDK

This tutorial walks you through using Foxit MobilePDF SDK to make an Android which displays PDF files. With step-by-step instructions, and only a few lines of code needed, it’s extremely easy to add PDF functionality to your mobile app!

There are only 5 steps:

Download Foxit MobilePDF SDK

Create a new Android project

Integrate Foxit MobilePDF SDK

Initialize Foxit MobilePDF SDK and add code to display a PDF

Build and Run the project

Requirements:

Runtime requirements:

Android 2.2 or newer/ API 8 or higher

32/64-bit ARM (armeabi-v7a / arm64-v8a) or 32-bit Intel x86 CPU

Support Android AAR archives: Gradle 2.1.0 or later

2. An up-to-date version of Android Studio.

This tutorial uses Android Studio 2.1.1 and JDK 1.7.0.79. However you can use other versions without issue.

1. Download Foxit MobilePDF SDK

If you haven’t already downloaded Foxit MobilePDF SDK, download it for free by clicking the link below:

3. Integrate Foxit MobilePDF SDK

a) Copy and paste the “FoxitRDKUIExtensions.aar” file from the “libs” folder of the download package to “Test_android\app\libs” folder.

b) Copy and paste the “librdk.so” file from the “libs\x86” folder of the download package to “Test_android\app\libs\x86” (note that you might need to create this folder by yourself).

Note: For this project, we will run the project on an emulator with x86 architecture, so we just add the “x86/librdk.so” library into the project. If you are not clear about the architecture of the device you want to use, you can add all of the “.so” libraries, because the linker can distinguish the device’s architecture and then choose the proper Android PDF SDK library.

Then, the project should look like this:

c) Add a reference to the “librdk.so” and Include Foxit MobilePDF SDK as a dependency in the project. Update the app’s “build.gradle” as follows:

4. Initialize Foxit MobilePDF SDK and add code to display a PDFNote So far, we set the compileSdkVersion and targetSdkVersion to API 23. If you also want to use API 23, please make sure you have already installed the SDK Platform Android 6.0, API 23. If you have not already done this, open the Android SDK Manager to download and install it first.

Currently we’ve just created a new Android app with a blank activity. Now, we’ll update the following three files in order to run the Test_Android project:

activity_main.xml

MainActivity.java

AndroidManifest.xml

Add the PDF View Control to show the PDF in the “activity_main.xml” found in the “app/src/main/res/layout”.

In the “MainActivity.java“, instantiate a PDFDoc object to load an existing PDF document (“/mnt/sdcard/Sample.pdf”); and instantiate a PDFViewCtrl object to show the document. Please make sure you have already pushed the “Sample.pdf” onto the Android device or emulator’s SD card.

Note: If you want to run this project on an Android 6.0 (API 23) or higher devices/emulators, you need to write some additional code to require the authorization of runtime permissions, or you can change the targetSdkVersion in app’s “build.gradle” from 23 to the SDK version that is just less than 23, such as 21.

5. Build and Run the project

Congratulations! We have now finished building a simple Android app which uses Foxit Mobile PDF SDK to display a PDF document. And, it only required a few lines of code too!

Now you should be able to build and run it. The “Sample.pdf” document should display as follows:

In addition, this sample app already has some basic PDF features, such as zooming in/out and page turning. (See following figures)