Some test text!

keyboard_arrow_down

Get started

Download PDFTron SDKIntegrate with NuGetIntegrate manuallyRun samplesAdd license keyQuick start - view a documentUpdate to latest version
keyboard_arrow_down

Xamarin.Forms

keyboard_arrow_down

Fundamentals

Java binding (Android)Objective-C binding (iOS)Code sharing
keyboard_arrow_down

Document Viewer (Android)

keyboard_arrow_down

Document Viewer (iOS)

Show a document in a ViewControllerShow documents in a tabbed ViewController
keyboard_arrow_down

Document Viewer components

PDFViewCtrl (Android)PDFViewCtrl (iOS)ReflowAnnotation toolbarAnnotation style editorPage sliderAnnotation listDocument outlineUser bookmark listList containerThumbnail browserAdd pages dialogRotate pages dialogView mode dialogCrop pages dialog
keyboard_arrow_down

Annotation and collaboration

OverviewToolManager setupDisable annotation creation/editingOverride annotation handling
keyboard_arrow_down

Advanced customization

Add a custom view to a pageCreate a custom toolPage coloring modePage fit modePage layout modeRealtime collaboration
keyboard_arrow_down

Conversion

Convert documents to PDFConvert HTML to PDF
keyboard_arrow_down

Search

Inline text search
keyboard_arrow_down

Advanced topics

Document savingDocument locking
keyboard_arrow_down

FAQ

System RequirementsPDFTron full vs. standardBuild split APKsLocalizationIcon cheat sheet
keyboard_arrow_down

PDF Processing API

What is PDFNet?Opening a documentSerializing (saving) a documentWorking with pagesWorking with Page ContentWorking with BookmarksWorking with Interactive Forms (AcroForms)PDF SecurityLow-level PDF APIError handling
PDFViewCtrl (Android)keyboard_arrow_down

PDFViewCtrl

In this document
chevron_rightOption 1: from resource
chevron_rightOption 2: from local device storage
chevron_rightOption 3: from content uri
chevron_rightOption 4: from HTTP/HTTPS
chevron_rightManaging lifecycle
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:

linkOption 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), "");

linkOption 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")), "");

linkOption 3: from content uri

using Android.Net;

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

linkOption 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);

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