Best Android PDF Libraries 2018

android pdf viewer

Best Android PDF Libraries in 2018

PDF stands for Portable Document Format. It's a file format developed in the 1990s to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems.

PDF was first released 24 years ago in the year 15 June 1993 by Adobe, however is now an open standard maintained by the International Organization for Standardization (ISO).

There are plenty of free and commercial PDF Reader applications for android devices. However, we as developers should build our own little pdf readers that maybe we and our friends can use.

It's not as difficult as you would think and several open source libraries exist to aid in this.

In this piece we will look at some of these libraries and probably snippets of how to use them.

Let's start.

1. AndroidPdfViewer

This is an open source libary for displaying PDF documents.These PDFs are rendered with PdfiumAndroid.

AndroidPdfViewer is currently the most popular Android PDF View library. It's maintained by bartesk and he has released various versions of the library independently.

For example, alot of people are still using AndroidPdfView, yet there is a AndroidPdfViewV1 and AndroidPdfViewV2.

This library has support for animations, gestures, zoom and double tap support.

Using this library is easy, very easy.

First you just include it in your app level build.gradle:

compile 'com.github.barteksc:android-pdf-viewer:2.8.2'

Then in your layout:

<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

Then you can load the pdf from various sources:

pdfView.fromUri(Uri)
or
pdfView.fromFile(File)
or
pdfView.fromBytes(byte[])
or
pdfView.fromStream(InputStream) // stream is written to bytearray - native code cannot use Java Streams
or
pdfView.fromSource(DocumentSource)
or
pdfView.fromAsset(String)
    .pages(0, 2, 1, 3, 3, 3) // all pages are displayed by default
    .enableSwipe(true) // allows to block changing pages using swipe
    .swipeHorizontal(false)
    .enableDoubletap(true)
    .defaultPage(0)
    // allows to draw something on the current page, usually visible in the middle of the screen
    .onDraw(onDrawListener)
    // allows to draw something on all pages, separately for every page. Called only for visible pages
    .onDrawAll(onDrawListener)
    .onLoad(onLoadCompleteListener) // called after document is loaded and starts to be rendered
    .onPageChange(onPageChangeListener)
    .onPageScroll(onPageScrollListener)
    .onError(onErrorListener)
    .onPageError(onPageErrorListener)
    .onRender(onRenderListener) // called after document is rendered for the first time
    // called on single tap, return true if handled, false to toggle scroll handle visibility
    .onTap(onTapListener)
    .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
    .password(null)
    .scrollHandle(null)
    .enableAntialiasing(true) // improve rendering a little bit on low-res screens
    // spacing between pages in dp. To define spacing color, set view background
    .spacing(0)
    .invalidPageColor(Color.WHITE) // color of page that is invalid and cannot be loaded
    .load();
View Download
View Direct Download

2. PdfiumAndroid

The same author of AndroidPdfViewer, bartesk forked PdfiumAndroid from it's original repository and has been maintaining and added some documentation as well.

The original PdfiumAndroid hasn't been maintained.

So the [forked version(https://github.com/barteksc/PdfiumAndroid)] has some documentation and is being actively mainatinaed.

He forked it so as to use it with the popular AndroidPdfViewer.

However you can use it independently as well.

First you'd need to add it as a dependency:

compile 'com.github.barteksc:pdfium-android:1.8.2'

Then here's a simple example:

void openPdf() {
    ImageView iv = (ImageView) findViewById(R.id.imageView);
    ParcelFileDescriptor fd = ...;
    int pageNum = 0;
    PdfiumCore pdfiumCore = new PdfiumCore(context);
    try {
        PdfDocument pdfDocument = pdfiumCore.newDocument(fd);

        pdfiumCore.openPage(pdfDocument, pageNum);

        int width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNum);
        int height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNum);

        // ARGB_8888 - best quality, high memory usage, higher possibility of OutOfMemoryError
        // RGB_565 - little worse quality, twice less memory usage
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.RGB_565);
        pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageNum, 0, 0,
                width, height);
        //if you need to render annotations and form fields, you can use
        //the same method above adding 'true' as last param

        iv.setImageBitmap(bitmap);

        printInfo(pdfiumCore, pdfDocument);

        pdfiumCore.closeDocument(pdfDocument); // important!
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}

public void printInfo(PdfiumCore core, PdfDocument doc) {
    PdfDocument.Meta meta = core.getDocumentMeta(doc);
    Log.e(TAG, "title = " + meta.getTitle());
    Log.e(TAG, "author = " + meta.getAuthor());
    Log.e(TAG, "subject = " + meta.getSubject());
    Log.e(TAG, "keywords = " + meta.getKeywords());
    Log.e(TAG, "creator = " + meta.getCreator());
    Log.e(TAG, "producer = " + meta.getProducer());
    Log.e(TAG, "creationDate = " + meta.getCreationDate());
    Log.e(TAG, "modDate = " + meta.getModDate());

    printBookmarksTree(core.getTableOfContents(doc), "-");

}

public void printBookmarksTree(List<PdfDocument.Bookmark> tree, String sep) {
    for (PdfDocument.Bookmark b : tree) {

        Log.e(TAG, String.format("%s %s, p %d", sep, b.getTitle(), b.getPageIdx()));

        if (b.hasChildren()) {
            printBookmarksTree(b.getChildren(), sep + "-");
        }
    }
}
View Download
View Direct Download

3. PdfBox-Android

PdfBox-Android is a port of Apache's PdfBox library to be usable on Android. Most features should in the parent libray are implemented already in PdfBox-Android.

PdfBox-Android requires Android API 19 and greater for full functionality.

PdfBox-Android is yet another library allowing us to render PDF documents. It was written by Tom Roush and has several contributors.

The main code of the PdfBox-Android project is licensed under the Apache 2.0 License, found here.

This library has existed for more than 4 years but still get updated regularly.

Installation of PdfBox-Android

Here's how to install PdfBox-Android.

Go over to your app level build.gradle and add the the implementation statememt:

dependencies {
    implementation 'com.tom_roush:pdfbox-android:1.8.10.0'
}

You can check the latest version here.

If you are using Maven then:

<dependency>
  <groupId>com.tom_roush</groupId>
  <artifactId>pdfbox-android</artifactId>
  <version>1.8.10.0</version>
  <type>pom</type>
</dependency>

Before calls to PDFBox are made it is highly recommended to initialize the library's resource loader. Add the following line before calling PDFBox methods:

PDFBoxResourceLoader.init(getApplicationContext());

Get PdfBox-Android below:

No. Location Link
1. GitHub Direct Download
2. GitHub Browse

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post Next Post