Display documents using PDFViewCTRL with Apryse Android SDK

If you are looking for a quick start on displaying documents in your application, please first take a look at Show a document in an Activity or Show a document in 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 to embed PDFViewCtrl in your own layout.

Before beginning, make sure the Apryse library is initialized prior to inflating the layout or calling setContentView in your activity.

PDFViewCtrl is a ViewGroup that can be embedded in any layout. It encapsulates a rich set of functionalities for interactive viewing of PDF documents, including multi-threaded rendering, PDF rendering settings, scrolling, zooming, page navigation, different page viewing modes, coordinates conversion, text selection, text search, etc.

View documents using PDFViewCtrl

In this tutorial you will display a PDF file in your activity by using PDFViewCtrl.

  1. In your AndroidManifest.xml, make sure you enable largeHeap in the <application> tag. Also, add a custom theme and set the android:windowSoftInputMode:"adjustPan" attribute in the <activity> tag as follow:

XML

1<!-- Include existing attributes in application -->
2<application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="false">
3 <!-- Include existing attributes in activity -->
4 <activity android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
5</application>

If your app is targeting Android SDK version 28 or higher, please also set the android:usesCleartextTraffic="true" attribute in your application tag to open HTTP files in the viewer. If you are only working with HTTPS files, this is not required.

  1. If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme for your activity in res/values/styles.xml:You can learn more about this in the customize the viewer's theme guide.

XML

1<resources>
2<style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <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>
3</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.

  1. Now, add PDFViewCtrl to your activity's XML layout. For example:

XML

1<?xml version="1.0" encoding="utf-8"?>
2<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
3
4 <com.pdftron.pdf.PDFViewCtrl android:id="@+id/pdfviewctrl" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical|horizontal"/>
5
6</FrameLayout>
  1. In your activity, get a reference to PDFViewCtrl after inflating the layout and call AppUtils.setupPDFViewCtrl.
1private PDFViewCtrl mPdfViewCtrl;
2// ...
3@Override
4protected void onCreate(Bundle savedInstanceState) {
5 super.onCreate(savedInstanceState);
6 setContentView(R.layout.my_activity_layout);
7
8 mPdfViewCtrl = findViewById(R.id.pdfviewctrl);
9 try {
10 AppUtils.setupPDFViewCtrl(mPdfViewCtrl);
11 } catch (PDFNetException e) {
12 // Handle exception
13 }
14}
  1. Next, choose a document to display by using the following options:Add a sample PDF to src/main/res/raw folder, then call:

View from resource

1import com.pdftron.pdf.utils.Utils;
2// ...
3private PDFDoc mPdfDoc;
4// ...
5public void viewFromResource(int resourceId, String fileName) throws PDFNetException {
6 File file = Utils.copyResourceToLocal(this, resourceId, fileName, ".pdf");
7 mPdfDoc = new PDFDoc(file.getAbsolutePath());
8 mPdfViewCtrl.setDoc(mPdfDoc);
9 // Alternatively, you can open the document using Uri:
10 // Uri fileUri = Uri.fromFile(file);
11 // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);
12}

View from local device storage

Storage Permission

Please follow the latest Android best practices and guidelines outlined here

Managing lifecycle

It is extremely important that you follow the Android activity/fragment lifecycle and clean up PDFViewCtrl and PDFDoc properly. Make sure you have the following in lifecycle callbacks:

1// ...
2@Override
3public void onPause() {
4 super.onPause();
5 if (mPdfViewCtrl != null) {
6 mPdfViewCtrl.pause();
7 mPdfViewCtrl.purgeMemory();
8 }
9}
10
11@Override
12public void onResume() {
13 super.onResume();
14 if (mPdfViewCtrl != null) {
15 mPdfViewCtrl.resume();
16 }
17}
18
19@Override
20public void onDestroy() {
21 super.onDestroy();
22 if (mPdfViewCtrl != null) {
23 mPdfViewCtrl.destroy();
24 mPdfViewCtrl = null;
25 }
26
27 if (mPdfDoc != null) {
28 try {
29 mPdfDoc.close();
30 } catch (Exception e) {
31 // handle exception
32 } finally {
33 mPdfDoc = null;
34 }
35 }
36}

Next steps

Check out the diagram of the overall view hierarchy for more.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales