PDF is a popular file format used for books, documents, forms, invoices, etc. To read these PDF files on an android device we use PDF reader android applications. Some of the android devices come packed with PDF reader application and some are not.

Let’s learn how to build a PDF reader android application from scratch. For this, we are going to use the AndroidPdfViewer library. It comes with a lot of features like Night Mode, horizontal swipe, page snap, antialiasing on low res devices, and many more. Let’s take a deep dive and learn how we can integrate this library to build PDF reader android application.

Demo

Building PDF reader android application from scratch

Source Code

Building PDF reader android application from scratch

Let’s understand operations we can perform by using AndroidPdfViewer library.

OperationDescription
fromUri()Load PDF from URI Resouce.
fromFile()Load PDF from the file.
fromBytes()Load PDF from bytes.
fromStream()Load PDF from the stream.
fromAsset()Load PDF from assets.
pages()By default, all pages are loaded. Use this method to load specific pages.
enableSwipe()Allows blocking changing pages using swipe.
enableDoubletap()This enables double-tap to zoom in the PDF file.
enableAntialiasing()Improves rendering a little bit on low-res screens.
password()Takes password to open Password protected PDF.
nightMode()To Enable or Disable Night mode.
spacing()spacing between pages in dp. To define spacing color, set view background
load()Readers PDF in the view.
AndroidPdfViewer library operations

Now we understood the basic operations that we can perform with this library. Let’s integrate with our application.

The first step is to add Gradle dependency for AndroidPdfViewer. Create or choose an existing android studio project. To add the dependency go to the build.gradle file in the dependencies section add below line.

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

Note: This dependency was the latest while writing the article. You can find the latest dependency from the AndroidPdfViewer GitHub page.

Create PDFVIew widget layout. This will hold our PDF view. Create new layout in res -> layout -> activity_main.xml. Add below lines to it.

<com.github.barteksc.pdfviewer.PDFView android:id="@+id/PDFView"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" />

Create new activity in java -> Package -> MainActivity.java. Intilize PDFView view. Paste your PDF in res->raw foder. Add below lines to the MainActivity.java.

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.github.barteksc.pdfviewer.PDFView;

import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
    // Define view
    PDFView pdfView;
    // Define Night mode variable and set it false default
    boolean isNightMode = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize view
        pdfView = findViewById(R.id.PDFView);

        // Load PDF from resource raw folder
        InputStream is = getResources().openRawResource(R.raw.brochure);

        // Load PDF in the view
        pdfView.fromStream(is).load();
    }

    // Optional - Override to create overflow menu in toolbar
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    // Optional - This method is called when Toggle Night Mode option is selected
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.toggleNightMode:

                // Check PDF night mode is on or off
                if (isNightMode) {
                    // Turn off PDF night mode
                    pdfView.setNightMode(false);
                    pdfView.requestLayout();
                    isNightMode = false;
                } else {
                    // Turn on PDF Night Mode
                    pdfView.setNightMode(true);
                    isNightMode = true;
                    pdfView.requestLayout();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }

    }
}

Don’t forget to add this activity to your manifest file. Finally run the application.

PDF Reader Application
PDF Reader Application

If you still have any queries, please post them in the comments section below, I will be happy to help you.

Author

Hello there, My name is Amardeep founder of loopwiki.com. I have experience in many technologies like Android, Java, Php, etc. In this variety of technologies, I love Android App Development. If you have any idea and you want me to develop for you then let's have chat Conatct

Write A Comment