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:
1) 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.
Download Foxit MobilePDF SDK
If you haven’t already downloaded Foxit MobilePDF SDK, download it for free by clicking the link below:
Click here to get your free trial of Foxit MobilePDF SDK!
Once you’ve filled out the form, you’ll receive an email with download links and some helpful documents.
Create a new Android project
Open Android Studio, and choose File -> New -> New Project… to start the Create New Project wizard, and then fill the New Project dialog as shown here. After filling, click Next.
In the Target Android Devices dialog, select “Phone and Tablet” to run your app, and set the Minimum SDK to API 8 as in the following image. Then, click Next.
In the Add an activity to Mobile dialog, select “Empty Activity” as in the following image, and then click Next.
In the Customize the Activity dialog, customize your activity as desired, here, we use the default setting, and then click Finish.
Integrate Foxit MobilePDF SDK
To add “FoxitRDKUIExtensions.aar” and “librdk.so” (libs/armeabi-v7a, libs/arm64-v8a, libs/x86) into Test_android project. Follow the steps below:
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:
apply plugin:
'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig{
applicationId "com.foxit.test_android"
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile (name:'FoxitRDKUIExtensions', ext:'aar')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
Initialize Foxit MobilePDF SDK
Also 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”.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.foxit.test_android.MainActivity">
<com.foxit.sdk.PDFViewCtrl
android:id="@+id/pdfviewer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical|horizontal"/>
</RelativeLayout>
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.
package com.foxit.test_android;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.PDFException;
import com.foxit.sdk.PDFViewCtrl;
import com.foxit.sdk.pdf.PDFDoc;
public class MainActivity extends FragmentActivity {
private PDFViewCtrl pdfViewCtrl = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The value of "sn" can be found in the "rdk_sn.txt".
// The value of "key" can be found in the "rdk_key.txt".
String sn = " ";
String key = " ";
// Load "librdk.so" library.
System.loadLibrary("rdk");
try {
// initialize the library
Library.init(sn, key);
} catch (PDFException e) {
e.printStackTrace();
return;
}
// Inflate the view and get a reference to PDFViewCtrl
setContentView(R.layout.activity_main);
pdfViewCtrl = (PDFViewCtrl) findViewById(R.id.pdfviewer);
// Load a document
String path = "/mnt/sdcard/Sample.pdf";
try {
PDFDoc document = PDFDoc.createFromFilePath(path);
document.load(null);
pdfViewCtrl.setDoc(document);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
try {
Library.release();
} catch (PDFException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
if(pdfViewCtrl != null)
pdfViewCtrl.requestLayout();
}
}
Set the “users-permission” in the “AndroidManifest.xml” found in the “app/src/main” to give the project permission to write and read the SD card of the Android devices or emulators.
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.foxit.test_android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
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.
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)