Using PDFViewCtrl

If you are looking for a quick start on displaying documents in your application, please first take a look at Using an Activity or Using a Fragment as they are easier to setup and ready to launch from any Activity or Fragment. Continue reading this article if you are looking for embedding PDFViewCtrl in your own layout.

PDFViewCtrl extends Android's ViewGroup and can be embedded in any layout. It is the core functionality for displaying documents.

In this tutorial you will be able to display a PDF file in PDFViewCtrl.

Before beginning, make sure the PDFTron library is initialized prior to inflating the layout or calling setContentView of your Activity.

First, make sure the following is in your <application> tag in AndroidManifest.xml.

<application
    android:largeHeap="true"
    ...>
    
    <activity
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/CustomAppTheme"
        .../>
</application>

Then, add a theme to res/values/styles.xml:

<resources>
  <style name="CustomAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="colorPrimary">#3F51B5</item>
    <item name="colorPrimaryDark">#303F9F</item>
    <item name="colorAccent">#FF4081</item>
    <!-- Action bar -->
    <item name="actionModeBackground">?attr/colorPrimary</item>
    <item name="windowActionModeOverlay">true</item>
  </style>
</resources>
PDFViewCtrl uses the AppCompat theme for material colors. Make sure that the value of android:theme in your activity tag also extends the AppCompat theme.

Now, add PDFViewCtrl to your XML layout.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.myapplication.MainActivity">

    <com.pdftron.pdf.PDFViewCtrl
        android:id="@+id/pdfviewctrl"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical|horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Please note that you need to change com.myapplication.MainActivity to your own Activity path.

Then, get a reference to PDFViewCtrl in code after inflating the layout.

import com.pdftron.pdf.PDFViewCtrl;
import com.pdftron.pdf.config.PDFViewCtrlConfig;
import com.pdftron.pdf.utils.AppUtils;
...

PDFViewCtrl mPdfViewCtrl;

mPdfViewCtrl = findViewById(R.id.pdfviewctrl);
AppUtils.setupPDFViewCtrl(mPdfViewCtrl);

Next, choose a document to display:

Option 1: from resource

Add a sample PDF to src/main/res/raw folder.

import com.pdftron.pdf.utils;
import java.io.File;

PDFDoc mPdfDoc;

try {
    File file = Utils.copyResourceToLocal(this, R.raw.sample, "sample", ".pdf");
    mPdfDoc = new PDFDoc(file.getAbsolutePath());
    mPdfViewCtrl.setDoc(mPdfDoc);
    // Alternatively, you can open the document using Uri:
    // Uri fileUri = Uri.fromFile(file);
    // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);
} catch (Exception ex) {
    ex.printStackTrace();
}

Option 2: from local device storage

To access device storage, add the following permissions to AndroidManifest.xml file outside of the <application> tag:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Then:

try {
    mPdfDoc = new PDFDoc("my_file_path");
    mPdfViewCtrl.setDoc(mPdfDoc);
    // Alternatively, you can open the document using Uri:
    // Uri fileUri = Uri.fromFile(new File("my_file_path"));
    // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);
} catch (Exception ex) {
    ex.printStackTrace();
}

Option 3: from content uri

try {
    Uri contentUri = Uri.parse("my_content_uri");
    mPdfDoc = mPdfViewCtrl.openPDFUri(contentUri, null);
} catch (Exception ex) {
    ex.printStackTrace();
}

Option 4: from HTTP/HTTPS

To access via the internet, add the following permissions to AndroidManifest.xml file outside of the <application> tag:

<uses-permission android:name="android.permission.INTERNET"/>

Then:

try {
    String url = "http://www.pdftron.com/downloads/pdfref.pdf";
    mPdfViewCtrl.openUrlAsync(url, null, null, null);
    // Alternatively, you can open the document using Uri:
    // mPdfViewCtrl.openPDFUri(Uri.parse(url), null);
} catch (Exception ex) {
    ex.printStackTrace();
}

Managing lifecycle

It is extremely important that you follow Android Activity/Fragment lifecycle and cleanup properly. Make sure you have the following:

@Override
public void onPause() {
    super.onPause();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.pause();
        mPdfViewCtrl.purgeMemory();
    }
}

@Override
public void onResume() {
    super.onResume();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.resume();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl.destroy();
        mPdfViewCtrl = null;
    }

    if (mPdfDoc != null) {
        try {
            mPdfDoc.close();
        } catch (Exception e) {
            // handle exception
        } finally {
            mPdfDoc = null;
        }
    }
}
Want to annotate on PDF files in PDFViewCtrl? Check out the Setup ToolManager guide.
Want to display non-PDF files in PDFViewCtrl? Check out the viewing other document types guide.