Quick start - view a document in Android

Prerequisites

  • Minimum API: 16 (using AndroidX)
  • Compile API: 34
  • Recommended target API: 34
  • The latest version of Android Studio.
  • If your app is using AndroidX you will also need to add android.useAndroidX=true and android.enableJetifier=true in your gradle.properties file. Learn more about requirements in our AndroidX FAQ.

No trial license key required.

The trial of Apryse Mobile SDK does not require a trial key. A commercial license key is required for use in a production environment. Please contact sales to purchase a commercial key or if you need any other license key assistance.

Keep your license keys confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

In this quick start tutorial you will create a simple Android app that will open a PDF document stored in your Android project by using DocumentActivity. The sample code for this tutorial is available at our GitHub repository.

  1. On the Android welcome screen, click Start a new Android Studio project:
Apryse Docs Image
  1. Create a new Android Studio project with an Empty Activity and set the minimum SDK to API 16. Learn more about Apryse's system requirements in Android supported API FAQ.
  2. For simplicity, we'll integrate the Apryse SDK into our project using Gradle. You can learn more about how Gradle is used in Android Studio at the Gradle guides.Find your gradle.properties file in the root folder of your project and add your license key to this file:

sh

1PDFTRON_LICENSE_KEY=INSERT_COMMERCIAL_LICENSE_KEY_HERE_AFTER_PURCHASE

No trial license key required.

The trial of Apryse Mobile SDK does not require a trial key. A commercial license key is required for use in a production environment. Please contact sales to purchase a commercial key or if you need any other license key assistance.

Keep your license keys confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

  1. Now open the settings.gradle file located in your project's root directory and add the Apryse Maven repository:

Groovy

1dependencyResolutionManagement {
2 repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3 repositories {
4 google()
5 mavenCentral()
6 maven {
7 url "https://pdftron-maven.s3.amazonaws.com/release"
8 }
9 jcenter() // Warning: this repository is going to shut down soon
10 }
11}
  1. Then in your app module's build.gradle file (usually app/build.gradle) add the following:You should also sync your project when you make changes in your Gradle files.

Groovy

1android {
2 defaultConfig {
3 ...
4 multiDexEnabled true
5 vectorDrawables.useSupportLibrary = true
6 manifestPlaceholders = [pdftronLicenseKey:PDFTRON_LICENSE_KEY]
7 }
8}
9
10dependencies {
11 ...
12 implementation "com.pdftron:pdftron:11.0.0"
13 implementation "com.pdftron:tools:11.0.0"
14
15 implementation 'androidx.multidex:multidex:2.0.1'
16}

If you encountered any issue with gradle sync, check out the Troubleshooting guide.

  1. In order to support all the features in DocumentActivity, we need to include the Android permissions listed in the table below. However if you would like to disable certain features and customize your document viewer, you should leave out unnecessary permissions. You can learn more about Android Mobile SDK permissions.In this sample we'll add all the permissions to AndroidManifest.xml so we can support all the features in the viewer. In this file, we'll also need to add a reference to our Apryse license key. The resulting AndroidManifest.xml file should look something like this:

Feature

Relevant permission

  • Opening a PDF from a URL
  • HTML to PDF conversion
  • Realtime collaboration

android.permission.INTERNET

  • Creating sound annotations

android.permission.RECORD_AUDIO

Storage Permission

Please follow the latest Android best practices and guidelines outlined here

XML

1<?xml version="1.0" encoding="utf-8"?>
2<!-- Include existing attributes in manifest -->
3<manifest>
4
5 <!-- Required permissions are added here -->
6 <uses-permission android:name="android.permission.INTERNET"/>
7 <uses-permission android:name="android.permission.RECORD_AUDIO" />
8
9 <!-- Add multidex support and enable largeHeap. Also enable usesCleartextTraffic in application attributes if you are working with HTTP files. If you are only working with HTTPS files, this is not required.-->
10 <!-- Include existing attributes in application -->
11 <application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="false">
12
13 <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
14 <meta-data android:name="pdftron_license_key" android:value="${pdftronLicenseKey}"/>
15
16 <!-- Document viewer activity declaration-->
17 <activity android:name="com.pdftron.pdf.controls.DocumentActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
18 </application>
19</manifest>
  1. If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme (referenced by DocumentActivity in AndroidManifest.xml) in res/values/styles.xml:You can learn more about this in the customize the viewer's theme guide.

XML

1<!-- Include existing attributes in resources -->
2<resources>
3 <!-- Custom theme that will be used by the document reader -->
4 <style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <item name="colorPrimary">@color/app_color_primary_day</item> <item name="colorPrimaryDark">@color/app_color_primary_dark_day</item> <item name="colorAccent">@color/app_color_accent</item> <!-- Action bar --> <item name="actionModeBackground">?attr/colorPrimary</item> <item name="windowActionModeOverlay">true</item> </style>
5</resources>
  1. Now add a PDF file to the res/raw folder of your project (you can use our sample file) and call it sample.pdf, we are going to reference this file in the next step.
Apryse Docs Image
  1. In onCreate of your launcher activity, call DocumentActivity.openDocument(Context, int) to open this PDF file with the document reader:This launches DocumentActivity with our sample PDF document with default viewer configurations, and you should see the following: You can also view your document in DocumentActivity by specifiying a local file path, an HTTP/HTTPS url, or a Content Uri:Please note that any changes made to files opened from res/raw will not be saved on the disk.The source code for this tutorial can be found on our GitHub repository.
1// ...
2@Override
3protected void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 // Open our sample document in the 'res/raw' resource folder
6 DocumentActivity.openDocument(this, R.raw.sample);
7}
Apryse Docs Image
1// Open a local document given a path
2private void openLocalDocument(Context context, String localFilePath) {
3 final Uri localFile = Uri.fromFile(new File(localFilePath));
4 DocumentActivity.openDocument(context, localFile);
5}
6
7// Open a document given a Content Uri
8private void openContentUriDocument(Context context, Uri contentUri) {
9 DocumentActivity.openDocument(context, contentUri);
10}
11
12// Open a document from HTTP/HTTPs
13private void openHttpDocument(Context context, String url) {
14 final Uri fileLink = Uri.parse(url);
15 DocumentActivity.openDocument(context, fileLink);
16}
17
18// Open a document stored in the 'src/main/res/raw' folder
19private void openRawResourceDocument(Context context, @IdRes int fileResId) {
20 DocumentActivity.openDocument(context, fileResId);
21}

Next steps

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales