Open a document (Android)

You have a few options to open a document such as with an activity, fragment, or view. A diagram of the overall view hierarchy can be found here: View hierarchy.

Display PDF using PDFViewCTRL in Android

This tutorial only applies to Xamarin.Android. See Xamarin.iOS equivalent here .

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 <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.

C#

1using pdftron.PDF;
2using pdftron.PDF.Config;
3using pdftron.PDF.Tools.Utils;
4
5private PDFViewCtrl mPdfViewCtrl;
6
7protected override void OnCreate(Bundle bundle)
8{
9 base.OnCreate(bundle);
10 SetContentView(Resource.Layout.Main);
11
12 mPdfViewCtrl = FindViewById<PDFViewCtrl>(Resource.Id.pdfviewctrl);
13 AppUtils.SetupPDFViewCtrl(mPdfViewCtrl);
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

C#

1using pdftron.PDF;
2using pdftron.PDF.Config;
3using pdftron.PDF.Tools.Utils;
4using Android.Net;
5
6private PDFDoc mPdfDoc;
7
8void ViewFromResource(int resourceId, string fileName)
9{
10 var file = Utils.CopyResourceToLocal(this, resourceId, fileName, ".pdf");
11 mPdfDoc = new PDFDoc(file.AbsolutePath);
12 mPdfViewCtrl.SetDoc(mPdfDoc);
13 // Alternatively, you can open the document using Uri:
14 // mPdfDoc = mPdfViewCtrl.OpenPDFUri(Uri.FromFile(file), "");
15}

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:

C#

1// ...
2protected override void OnPause()
3{
4 base.OnPause();
5 mPdfViewCtrl?.Pause();
6}
7
8protected override void OnResume()
9{
10 base.OnResume();
11 mPdfViewCtrl?.Resume();
12}
13
14protected override void OnDestroy()
15{
16 base.OnDestroy();
17 mPdfViewCtrl?.Destroy();
18 mPdfDoc?.Close();
19}

Next steps

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales