Using PDFViewCtrl

This article only applies to Xamarin.Android.
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 of displaying any documents.

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

Before jumping into the layout, please make sure PDFTron library is initialized before inflating the layout or before setContentView of your Activity.

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

<application
    android:largeHeap="true"
    ...>
</application>

Then, add theme to Resources/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 AppCompat theme for material colors. Make sure the Theme value in Activity tag should also extends AppCompat theme.

Next, add Theme and WindowSoftInputMode to the Activity declaration. For example, in your MainActivity.cs:

[Activity(...
    WindowSoftInputMode = Android.Views.SoftInput.AdjustPan,
    Theme = "@style/CustomAppTheme")]

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

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

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

using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;

PDFViewCtrl mPdfViewCtrl;

mPdfViewCtrl = FindViewById<PDFViewCtrl>(Resource.Id.pdfviewctrl);
AppUtils.SetupPDFViewCtrl(mPdfViewCtrl);

Next, choose a document to display:

Option 1: from resource

Add a sample.pdf to Resources/raw folder. Set its Build Action to AndroidResource.

using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;
using Android.Net;

PDFDoc mPdfDoc;

var file = Utils.CopyResourceToLocal(this, Resource.Raw.sample, "sample", ".pdf");
mPdfDoc = new PDFDoc(file.AbsolutePath);
mPdfViewCtrl.SetDoc(mPdfDoc);
// Alternatively, you can open the document using Uri:
// mPdfDoc = mPdfViewCtrl.OpenPDFUri(Uri.FromFile(file), "");

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:

using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;
using Android.Net;

PDFDoc mPdfDoc = new PDFDoc("my_file_path");
mPdfViewCtrl.SetDoc(mPdfDoc);
// Alternatively, you can open the document using Uri:
// mPdfDoc = mPdfViewCtrl.OpenPDFUri(Uri.FromFile(new Java.IO.File("my_file_path")), "");

Option 3: from content uri

using Android.Net;

var contentUri = Uri.Parse("my_content_uri");
mPdfDoc = mPdfViewCtrl.OpenPDFUri(contentUri, "");

Option 4: from HTTP/HTTPS

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

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

Then:

var url = "http://www.pdftron.com/downloads/pdfref.pdf";
pdfViewCtrl.OpenUrlAsync(url, null, null, null);
// Alternatively, you can open the document using Uri:
// mPdfViewCtrl.OpenPDFUri(Android.Net.Uri.Parse(url), null);

Managing lifecycle

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

protected override void OnPause()
{
    base.OnPause();
    mPdfViewCtrl?.Pause();
}

protected override void OnResume()
{
    base.OnResume();
    mPdfViewCtrl?.Resume();
}

protected override void OnDestroy()
{
    base.OnDestroy();
    mPdfViewCtrl?.Destroy();
    try
    {
        mPdfDoc?.Close();
    }
    catch
    {
        // handle exception
    }
}
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.