Blog Post

How to Build an Android PDF Viewer with PSPDFKit Library

Illustration: How to Build an Android PDF Viewer with PSPDFKit Library

In this post, you’ll learn how to use PSPDFKit’s library to build a PDF viewer for your Android application.

With more than 3 billion active devices, Android is one of the most widely used mobile operating systems. If your Android application has a use case for opening and viewing PDF documents, a PDF viewer is just what you need.

To get started, you’ll begin by setting up a new Android Studio project and opening a document from the android-assets folder. Then, you’ll learn how to open a document from your device’s local storage.

What Is an Android PDF Viewer?

An Android PDF viewer allows you to render and display PDF documents inside your app without your users needing to download them or use an external application like a PDF reader. You can build one from scratch, or you can save dev time by deploying a commercial Android PDF Viewer library, like the one PSPDFKit offers.

PSPDFKit Android PDF Viewer Library

PSPDFKit’s Android PDF viewer library uses a high-fidelity, reliable, PDFium-based rendering engine that’s fast, precise, and feature rich. It enables you to quickly and painlessly embed a fully configurable PDF viewer in your Android application.

PSPDFKit SDKs are deployed in some of the world’s most popular applications, including Autodesk, Disney, DocuSign, Dropbox, IBM, and Lufthansa.

Key capabilities include:

  • A customizable UI — Hide or add buttons, and match your look and feel.

  • 30+ features — Easily add features like PDF editing, digital signatures, form filling, real-time document collaboration, and more.

  • Dedicated support — Deploy faster by working 1-on-1 with our developers.

  • Page modes and transition — Use single or double page modes and multiple scroll modes.

  • Outline — Supports all custom variants and PDF action types.

  • Bookmarks — Users can add, remove, and sort bookmarks.

  • Reader view — Reflow text to present it in a single-column view.

  • PNG and JPG — Open images in the viewer (in addition to PDFs).

You can view our demo to see what PSPDFKit is capable of and determine how you can use it for your project.

Requirements

To get started, you’ll need:

  • Android Studio — The official integrated development environment for Android

Getting Started with PSPDFKit’s

The next few sections will walk you through the getting started process.

Creating a New Project

  1. Open Android Studio and select New Project. After that, go to File > New > New Project… to create a new project for your application.

Image showing how to create a new project in Android Studio

  1. Choose the correct template for your project. For this example, you’ll use Empty Activity.

Image showing a menu in Android Studio that lets user choose an activity

  1. When prompted, choose your app name (PSPDFKit Demo) and set the Save location and Language to your choice. Set the Minimum SDK to API 21.

Image showing a wizard for project setup

  1. Click Finish to let Android Studio create your project at your preferred save location.

Adding PSPDFKit’s Android PDF Viewer Library to Your Project

  1. Add the PSPDFKit Maven repository in settings.gradle, which is located at the root of your project:

dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
        url 'https://customers.pspdfkit.com/maven/'
        }
    }
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://customers.pspdfkit.com/maven")
        }
    }
}
  1. Add the PSPDFKit dependency in app/build.gradle:

dependencies {
    implementation 'com.pspdfkit:pspdfkit:8.2.1'
}
dependencies {
    implementation("com.pspdfkit:pspdfkit:8.2.1")
}

Configuring Your Build

PSPDFKit is supported on Android devices running API level 21 and newer and targeting the latest stable Android version 12 (API 31). Furthermore, PSPDFKit requires apps to enable Java 8 language features to build:

android {
    compileSdk 31
    buildToolsVersion '30.0.3'

    defaultConfig {
        applicationId 'com.example.app'
        minSdk 21
        targetSdk 31
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
android {
    compileSdk = 31
    buildToolsVersion = "30.0.3"

    defaultConfig {
        applicationId = "com.example.app"
        minSdk = 21
        targetSdk = 31
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

Displaying a PDF using PSPDFKit’s Android PDF Viewer Library

To verify that PSPDFKit is successfully integrated into your application, try opening a PDF file with the ready-to-use PdfActivity.

  1. Copy a PDF document to the assets directory of your Android project — for example, to src/main/assets/my-document.pdf.

  2. Add PdfActivity to your app’s AndroidManifest.xml:

<application>
    <activity
        android:name="com.pspdfkit.ui.PdfActivity"
        android:windowSoftInputMode="adjustNothing" />
</application>
  1. You can now start PdfActivity with the document from your assets directory:

val uri = Uri.parse("file:///android_asset/my-document.pdf")
val config = PdfActivityConfiguration.Builder(context).build()
PdfActivity.showDocument(this, uri, config)
final Uri uri = Uri.parse("file:///android_asset/my-document.pdf");
final PdfActivityConfiguration config = new PdfActivityConfiguration.Builder(context).build();
PdfActivity.showDocument(this, uri, config);
  1. PdfActivity will now present the document from your assets directory.

ℹ️ Note: This is a simple demonstration using the android_assets folder. This folder is a read-only directory that, during the build process, gets built into the .apk file in most Android applications. For information on use, refer to the following section.

Opening a Local PDF File on Android

In this section, you’ll display a PDF file from the device’s local storage. But before starting, it’s important to understand local storage.

The Android system provides two types of physical storage locations:

  • Internal storage, which is smaller and is always available.

  • External storage, which is usually larger than internal storage and might not always be available. This is why you need to ensure your application is able to handle opening files from both internal and external storage.

Opening a PDF from App-Specific Storage

Absolute paths must never be hardcoded. Directories where your app stores files aren’t guaranteed to be stable between different devices or even between app restarts. As such, we recommend using methods from Context to access different special file system directories.

  1. Internal Storage

Use the getFilesDir method from Context to get the path to the file system directory where your internal files are stored:

// This will fail if "my-document.pdf" isn't created in
// advance using `openFileOutput(String, int)`.

val uri = Uri.parse(getFilesDir() + "my-document.pdf")
val config = PdfActivityConfiguration.Builder(context).build()
PdfActivity.showDocument(this, uri, config)
// This will fail if "my-document.pdf" isn't created in
// advance using `openFileOutput(String, int)`.

final Uri uri = Uri.parse(getFilesDir() + "my-document.pdf");
final PdfActivityConfiguration config = new PdfActivityConfiguration.Builder(context).build();
PdfActivity.showDocument(this, uri, config);

ℹ Info: Learn more about openFileOutput(String, int) here.

  1. External Storage

Use Context#getExternalFilesDir(null) to access the directory that the system provides for your app on external storage:

// This will fail if external storage is not mounted correctly
// or if "my-document.pdf" isn't present in the directory.
// returned by getExternalFilesDir(null)

val uri = Uri.parse(getExternalFilesDir(null) + "my-document.pdf")
val config = PdfActivityConfiguration.Builder(context).build()
PdfActivity.showDocument(this, uri, config)
// This will fail if external storage is not mounted correctly
// or if "my-document.pdf" isn't present in the directory.
// returned by getExternalFilesDir(null)

final Uri uri = Uri.parse(getExternalFilesDir(null) + "my-document.pdf");
final PdfActivityConfiguration config = new PdfActivityConfiguration.Builder(context).build();
PdfActivity.showDocument(this, uri, config);

Opening a PDF from Shared Storage

Like the name suggests, shared storage is shared between all the applications running on a device. Shared storage directories and their data aren’t affected by a particular app’s uninstall.

The recommended method for accessing shared storage is through the Storage Access Framework (SAF). According to the documentation, “the SAF makes it simple for users to browse and open documents, images, and other files across all of their preferred document storage providers. A standard, easy-to-use UI lets users browse files and access recents in a consistent way across apps and providers.”

To open a PDF, invoke the ACTION_OPEN_DOCUMENT intent as follows:

// Request code for selecting a PDF document.
const val PICK_PDF_FILE = 1001

fun openFile(pickerInitialUri: Uri) {
    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
        addCategory(Intent.CATEGORY_OPENABLE)
        type = "application/pdf"

        // Optionally, specify a URI for the file that should appear in the
        // system file picker when it loads.
        putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
    }

    startActivityForResult(intent, PICK_PDF_FILE)
}

override fun onActivityResult(
        requestCode: Int, resultCode: Int, resultData: Intent?) {
    if (requestCode == PICK_PDF_FILE
            && resultCode == Activity.RESULT_OK) {
        // The result data contains a URI for the document or directory
        // the user selected.
        resultData?.data?.also { uri ->
            val documentUri = Uri.parse(uri)
            val config = PdfActivityConfiguration.Builder(context).build()
            PdfActivity.showDocument(this, documentUri, config)
        }
    }
}
// Request code for selecting a PDF document.
private static final int PICK_PDF_FILE = 1001;

private void openFile(Uri pickerInitialUri) {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("application/pdf");

    // Optionally, specify a URI for the file that should appear in the
    // system file picker when it loads.
    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);

    startActivityForResult(intent, PICK_PDF_FILE);
}

@Override
public void onActivityResult(int requestCode, int resultCode,
        Intent resultData) {
    if (requestCode == PICK_PDF_FILE
            && resultCode == Activity.RESULT_OK) {
        // The result data contains a URI for the document or directory
        // the user selected.
        Uri uri = null;
        if (resultData != null) {
            uri = resultData.getData();
            final Uri documentUri = Uri.parse(uri);
            final PdfActivityConfiguration config = new PdfActivityConfiguration.Builder(context).build();
            PdfActivity.showDocument(this, documentUri, config);
        }
    }
}

ℹ Info: Since the user is browsing and selecting the document through a file picker, they’re completely aware of what files your app has access to. Hence, your application doesn’t need to ask for runtime permissions.

Beyond Local Storage

PSPDFKit’s Android Library isn’t just limited to opening and manipulating documents from local storage. PSPDFKit can also open documents:

  • From remote storage

  • From PSPDFKit Server

  • From a custom data provider

  • That are password protected

To learn more about opening PDF documents in Android apps, refer to our related blog post.

Conclusion

In this post, we demonstrated how to integrate PSPDFKit’s Android PDF Viewer Library into an Android application, and we presented a PDF document from local directories. If you hit any snags, don’t hesitate to reach out to our support team for help.

At PSPDFKit, we offer a commercial, feature-rich, and completely customizable Android PDF library that’s easy to integrate and comes with well-documented APIs to handle advanced use cases. Try it for free, or visit our demo to see it in action.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
PRODUCTS  |  Android • Releases

PSPDFKit 8.3 for Android Adds Support for PDF Advanced Electronic Signatures

DEVELOPMENT  |  Android • iOS • Cordova • Flutter • Ionic • React Native • Xamarin

Cross-Platform Mobile Frameworks — An iOS Engineer's Perspective

DEVELOPMENT  |  Android • Tips

How to Load OBJ Files with MTL in Android