Three most used PDF libraries in Android

If you want to develop your PDF reader or were looking to integrate a PDF library into your Android Studio project, look no more! Your search for “Most popular PDF libraries for Android” has landed you in the perfect place. This blog will be looking at three of the most used PDF libraries for Android along with a detailed how-to. First, we will be covering some basic definitions and then move on to the PDF libraries. So let’s get started without further ado.

What are PDF files?

PDF files stand for Portable Document Format files. They are one of the most commonly used file types today. If you’ve ever downloaded a printable form or document from the Web, there’s a good chance it was a PDF file. Developed by Acrobat, PDF files are used for saving textual and visual(images) content and hyperlinks. They are mostly used as standard printable documents.

What is an external library file in Android Studio?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. But, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.

We can use two types of libraries in our Android Studio project:

  • Android Library Module: Android Library Module compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module. It allows you to add Android-specific components like resources and manifest files, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
  • Java Library: Java library builds a Java ARchive file (JAR file). A JAR file is useful for many projects especially when you want to share code with other platforms. But it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So when you do not need any android specific resources in a library you should create a java library.

Benefits of using external libraries in your Android Studio project

Using external libraries in an Android Studio project makes things easier. The following are some of the advantages of doing so:

  • When you’re building multiple apps that use some of the same components, such as activities, services, or UI layouts, you can simply copy-paste the same libraries in your multiple apps.
  • When you are using a service whose code has already been written, you can simply add its library as opposed to writing the entire code by yourself.
  • When you’re building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both, you can simply add the same libraries for both.

Now that we know what PDF and external libraries are, let’s look at three of Android’s most-used PDF libraries.

AndroidPdfViewer

About: AndroidPdfViewer is an android library for displaying PDF documents on Android, with animations, gestures, zoom,double-tap support and a lot more. It is based on PdfiumAndroid for decoding PDF files. The Android PdfViewer works effortlessly on API 11 (Android 3.0) and higher. It is licensed under Apache License 2.0.

There are two versions of AndroidPdfViewer. One goes with the version names AndroidPdfViewer 1.x and the other goes with AndroidPdfViewer 2.x. AndroidPdfViewer 1.x is separately available on the AndroidPdfViewerV1 repo, where it can be developed independently. The difference between the two versions is that Version 1.x uses a different engine for drawing documents on canvas. Therefore, you have two versions to choose from that best suit your needs. However, the only downside of AndroidPdfViewer is that your resultant APK files can be a bit large in size. This is because AndroidPdfViewer depends on PdfiumAndroid, which is a set of native libraries (almost 16 MB) for many architectures. An APK must contain all these libraries to run on every device available on the market, causing a tad bit hike in the file size.

How to add it to your Android project:

You can add this library by simpling navigating to your build.gradle file in Android Studio and adding the following dependency. To navigate to your app’s build.gradle file, make sure that your project view is set to Android and go to Gradle Scripts->build.gradle(Module:YourProjectName.app) and add the given dependency:

implementation ‘com.github.barteksc:android-pdf-viewer:3.2.0-beta.1’

PdfBox-Android

About: PdfBox-Android is a port of Apache’s original PdfBox library that can be used on Android. It is still under active development although most of its features should be implemented by now. Feature requests can also be added to the issue tracker. Stable releases can be added as a Gradle dependency from Maven Central. All in all, the Pdfbox-Android library is an open-source Java tool for working with PDF documents. This project allows the creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. The main code of this project, Apache PDFBox, is published under Apache License v2.0.

How to add it to your Android project:

You can add this library by simpling navigating to your build.gradle file in Android Studio and adding the following dependency. To navigate to your app’s build.gradle file, make sure that your project view is set to Android and go to Gradle Scripts->build.gradle(Module:YourProjectName.app) and add the given dependency:

implementation ‘com.tom-roush:pdfbox-android:2.0.2.0’

Android PDF Writer

About: Android PDF Writer (APW) is a simple Java library to generate simple PDF documents in Google’s Android devices released under the BSD license. It is known for being very lightweight and easy to use. This library is specifically developed for simple PDF creation. It is just large enough to get the job done without any unnecessary overhead. Hence, if your work is to just create simple PDFs, you should definitely go with this library.

How to add it to your Android project:

You can add this library to your project by downloading it as a JAR file from here and then following the given steps:

  • The first step is to of course open the Android Studio project that you are working on. If you want to create a new project, you can do so by selecting an Empty Activity and naming your project.
  • Once you are in your project, change the project view from Android to Project Files.

    Change project view to Project Files

  • Copy the JAR file of the library that you previously downloaded.
  • Once you have copied the JAR file, navigate to the libs folder by going to app->libs in the Project Files view.

    Navigate to the libs folder

  • Right-click on the libs folder and hit Paste.
  • Right-click on the pasted JAR file and select Add As Library… from the drop-down menu.

    Select Add as Library by right-clicking

You have now successfully added the Android PDF Writer library to your Android Studio project.

In conclusion, these were the top three highly used PDF libraries for Android. Hope you use them.