Top PDF Viewer Libraries for Android

Best PDF Viewer Libraries for Kotlin Android (Open Source)

Ever found yourself in a tangled mess trying to display PDFs in your Android app? Struggling to find a smooth, efficient, and user-friendly way to let users interact with those pesky documents.

I’ve handpicked some of the finest open-source PDF viewer libraries for Kotlin Android to help you get started.

Let's dive into the toolbox of awesomeness and find the perfect fit for your needs!

1. PDFView (Barteksc/Pdfium-Android)

PDFView is one of the most popular and robust open-source libraries for displaying PDF files. Created by Bartek Szopka, this library is based on the Pdfium library and offers a seamless way to view PDFs with minimal setup.

Features

  • Smooth scrolling through PDF pages.

  • Zoom in and out capabilities.

  • Horizontal and vertical scrolling.

  • Page snapping for better user experience.

  • Night mode reading.

  • Dual page mode for landscape.

Installation

Add the following dependencies to your build.gradle file:.

dependencies {
    implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
}

Code Example

Here’s a simple example to get you started:.

import com.github.barteksc.pdfviewer.PDFView
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    lateinit var pdfView: PDFView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        pdfView = findViewById(R.id.pdfView)
        pdfView.fromAsset("sample.pdf").load()
    }
}

Explanation:.

  • PDFView: This is the widget from the library used to display the PDF.
  • fromAsset("sample.pdf").load(): This line loads a PDF file named sample.pdf from the assets folder.

Comparison

Compared to other libraries, PDFView stands out due to its simplicity and efficiency. It’s user-friendly and offers excellent customization options, making it easier to fit various use cases.

PDFView GitHub Repository


2. MuPDF

MuPDF is an extremely lightweight PDF, XPS, and E-book viewer that supports a wide range of document formats. Developed by Artifex Software, it’s known for its speed and performance.

Features

  • Fast rendering of PDF files.

  • Supports various document formats like PDF, XPS, CBZ, and EPUB.

  • Highlight, underline, and strikeout text.

  • Annotation tools.

  • Link navigation.

Installation

Add the following dependencies in your build.gradle:.

dependencies {
    implementation 'com.github.mupdf:android-viewer:1.13.0'
}

Code Example

Here’s how to use MuPDF:.

import org.ebookdroid.core.Page
import org.ebookdroid.core.events.CurrentPageListener
import org.ebookdroid.core.hooks.NativeLibLoader
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        NativeLibLoader.init(this)
        val file = File(Environment.getExternalStorageDirectory(), "sample.pdf")
        val docView = findViewById<org.ebookdroid.ui.viewer.ViewerActivity>(R.id.docView)

        docView.open(file)
    }
}

Explanation:.

  • NativeLibLoader.init(this): Initializes the native library required for MuPDF.
  • open(file): Opens the specified PDF file located in the external storage.

Comparison

MuPDF excels in its performance and file format support. It might be a bit more complex to set up compared to PDFView but offers more powerful features like annotation and multi-format support.

MuPDF GitHub Repository


3. PSPDFKit

PSPDFKit is a commercial library that also offers an open-source version for basic PDF viewing. It's renowned for its extensive feature set and professional-quality rendering capabilities.

Features

  • High-quality rendering and fast performance.

  • Annotations and forms support.

  • Digital signatures.

  • Document editing tools.

  • Search and text extraction.

Installation

For the open-source version, you can add it as follows:.

dependencies {
    implementation 'com.pspdfkit:pspdfkit:6.5.1'
}

Code Example

Here’s an example to get started:.

import com.pspdfkit.PSPDFKit
import com.pspdfkit.document.PdfDocument
import com.pspdfkit.ui.PdfActivity
import android.os.Bundle

class MainActivity : PdfActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        PSPDFKit.initialize(this, "YOUR_LICENSE_KEY_GOES_HERE")

        val configuration = PdfActivityConfiguration.Builder(this).build()
        PdfActivity.showDocument(this, "file:///android_asset/sample.pdf", configuration)
    }
}

Explanation:.

  • PSPDFKit.initialize: Initializes the PSPDFKit with your license key.
  • PdfActivity.showDocument: Opens the specified PDF document using PSPDFKit's PDFActivity.

Comparison

PSPDFKit probably offers the most features among all the libraries discussed, but it comes at a cost. The free-tier might be limiting if you need more advanced features. However, if you do decide to splurge a bit, it’s worth every penny for enterprise-level applications.

PSPDFKit GitHub Repository


4. PdfiumAndroid

Introduction and Key Features: PdfiumAndroid is based on Google's Pdfium engine, ensuring fast and efficient PDF rendering. It's great for rendering PDF pages as bitmap images.

Installation:

dependencies {
    implementation 'com.github.mshockwave:PdfiumAndroid:1.9.0'
}

Code Example: To render a PDF page using PdfiumAndroid:

PdfiumCore pdfiumCore = new PdfiumCore(context);
try {
    ParcelFileDescriptor fd = ParcelFileDescriptor.open(new File("path/to/file"), ParcelFileDescriptor.MODE_READ_ONLY);
    PdfDocument pdfDocument = pdfiumCore.newDocument(fd);
    pdfiumCore.openPage(pdfDocument, 0);
    int width = pdfiumCore.getPageWidthPoint(pdfDocument, 0);
    int height = pdfiumCore.getPageHeightPoint(pdfDocument, 0);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    pdfiumCore.renderPageBitmap(pdfDocument, bitmap, 0, 0, 0, width, height);
    imageView.setImageBitmap(bitmap);
    pdfiumCore.closeDocument(pdfDocument); // important!
} catch(Exception e) {
    // handle exception
}

This snippet opens a PDF, renders its first page into a Bitmap, and sets it to an ImageView. It showcases PdfiumAndroid's capability for high-quality rendering.

GitHub Link: Learn more at PdfiumAndroid on GitHub.

Additional Libraries

Here are a few more PDF Viewer libraries for Android that you might find useful:

  • AndroidPdfViewerV1: A previous version of AndroidPdfViewer with support for older Android versions.
  • Vudroid: A lightweight PDF Viewer library based on Poppler and Cairo.

And there you have it! Three amazing libraries to help you conquer the world of PDFs in your Kotlin Android apps.

Thanks for reading.