Using an Activity

This article only applies to Xamarin.Android.

PDFTron ships with DocumentActivity which is an all-in-one document reader and PDF editor. In addition to PDF files, it also supports viewing of other file extensions such as .docx, .doc, .pptx, .xlsx, .md, .cbz and various image formats. It allows stream conversion of these non-pdf documents to PDF format so you can view the document while conversion happens. In this activity, you can read, annotate, sign, fill in PDF forms and more. You can find the java equivalent here: Using an Activity.

DocumentActivity extends Android's AppCompatActivity and follows Material design guidelines.

Please note that DocumentActivity requires the PDFTron.Android.Tools NuGet package.

Here are a few simple steps to get started with DocumentActivity. You can also see a working version in CompleteReaderAndroid sample project included in the download package.

  1. First, add activity declarations to AndroidManifest.xml file:
<application
    ...
    android:supportsRtl="true"
    android:largeHeap="true">
    ...
    <activity android:name="com.pdftron.pdf.controls.DocumentActivity"
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/CustomAppTheme"/>
</application>
  1. Then, add theme to Resources/values/styles.xml:
<resources>
    <style name="CustomAppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
        <item name="colorPrimary">@color/app_color_primary_day</item>
        <item name="colorPrimaryDark">@color/app_color_primary_dark_day</item>
        <item name="colorAccent">@color/app_color_accent</item>
        <!-- Action bar -->
        <item name="actionModeBackground">?attr/colorPrimary</item>
        <item name="windowActionModeOverlay">true</item>
    </style>
</resources>

Alternatively, if you cannot use NoActionBar as a base theme:

DocumentActivity extends AppCompatActivity and uses AppCompat Toolbar. It is important that you define a Theme.AppCompat-based theme. The recommended theme is Theme.AppCompat.*.NoActionBar. However you can also use other custom themes, be sure to disable the action bar manually:

<style name="CustomAppTheme" parent="Theme.AppCompat.*">
    ...
    <!-- Action bar -->
    <item name="windowActionModeOverlay">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
  1. Then, if you would like to customize the UI of DocumentActivity, you can use PdfFragmentConfig builder. For example:
var builder = new pdftron.PDF.Config.PdfFragmentConfig.Builder();
var config = builder
    .FullscreenModeEnabled(true)
    .MultiTabEnabled(true)
    .DocumentEditingEnabled(true)
    .LongPressQuickMenuEnabled(true)
    .ToolbarTitle("Simple Reader")
    .ShowSearchView(true)
    .Build();

For details on customizing the UI interface and sample usage of PdfFragmentConfig builder, check out the configuration tutorial.

  1. If you need storage permission for accessing local documents, add the following permissions to AndroidManifest.xml file outside of the <application> tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Storage permission
Please note that from Android 6.0, API 23, and up, application needs to request storage permission at runtime before accessing any files on device.
  1. If you need internet permission for downloading online documents, add the following permissions to AndroidManifest.xml file outside of the <application> tag:
<uses-permission android:name="android.permission.INTERNET"/>
  1. Lastly, launch the activity from anywhere in your application:
// from internal storage
var localFile = Android.Net.Uri.FromFile(new Java.IO.File("myLocalFilePath"));
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, localFile);
// or if using config
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, localFile, config);
// or if using config and opening password protected file
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, localFile, password, config);

// from content uri
var contentUri = Android.Net.Uri.Parse("myContentUri");
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, contentUri);
// or if using config
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, contentUri, config);
// or if using config and opening password protected file
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, contentUri, password, config);

// from http/https
var fileLink = Android.Net.Uri.Parse("myFileLink");
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, fileLink);
// or if using config
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, fileLink, config);
// or if using config and opening password protected file
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, fileLink, password, config);

// from res
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, Resource.Raw.my_file_res_id);
// or if using config
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, Resource.Raw.my_file_res_id, config);
// or if using config and opening password protected file
pdftron.PDF.Controls.DocumentActivity.OpenDocument(this, Resource.Raw.my_file_res_id, password, config);