Some test text!

platform
search

Show a document in an Activity

Contents

Prerequisites
Step 1: Update AndroidManifest.xml
Step 2: Launch the viewer
Step 3: Customize the viewer
This tutorial only applies to Xamarin.Android.
DocumentActivity requires the full version of the PDFTron library. See PDFTron full or standard?

PDFTron's Android SDK ships with DocumentActivity, an all-in-one document reader and PDF editor. In addition to PDF files, it also supports viewing other file formats such as .docx, .doc, .pptx, .xlsx, .md, .cbz and various image formats. In this activity you can also read, annotate, sign, fill in PDF forms and more.

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

linkPrerequisites

  • Integrated PDFTron into your project and added the PDFTron.Android.Tools NuGet package into your project.
  • A valid evaluation or commercial license key. If you do not have a license key, please contact sales for a commercial license key or click here to get an evaluation key.
  • Minimum API: 16 (using Android Support Libraries)
  • Recommended target API: 28

linkStep 1: Update AndroidManifest.xml

  1. In order to support all the features in DocumentActivity, we will need to add the Android permissions listed in this table. However if you would like to disable certain features and customize your document viewer, you should leave out unnecessary permissions.
Storage permission
Please note that from Android 6.0 (API 23) and up, applications need to request storage permission at runtime before accessing any files on device.
  1. Enable largeHeap and usesClearTextTraffic:

    <application
        ...
        android:largeHeap="true"
        android:usesCleartextTraffic="true">
    </application>
If your app is targeting Android SDK version 28 or higher, you will need to add the android:usesCleartextTraffic="true" attribute in your application tag.
  1. If you have not done so already, add your license in the AndroidManifest.xml file, and also declare DocumentActivity in the manifest file. The final AndroidManifest.xml file should look something like this:

    <manifest ...>
        <!-- Required permissions are added here -->
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
        
        
        <!-- Add license key in meta-data tag here -->
        <meta-data
            android:name="pdftron_license_key"
            android:value="your_license_key_goes_here"/>
        
        <!-- Add enable largeHeap and usesCleartextTraffic -->
        <application 
            ...
            android:largeHeap="true"
            android:usesCleartextTraffic="true">    
                
            <!-- Document viewer activity declaration-->
            <activity android:name="com.pdftron.pdf.controls.DocumentActivity"
                android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
                android:windowSoftInputMode="adjustPan"
                android:theme="@style/CustomAppTheme"/>
            ...
        </application>
    </manifest>

linkStep 2: Launch the viewer

Launch DocumentActivity by specifiying a local file path, an HTTP/HTTPS url, or a Content Uri:

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

linkStep 3: (Optional) Customize the viewer

  • If you would like to customize the appearance of the viewer activity, define CustomAppTheme in res/values/styles.xml:

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

    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 theme for CustomAppTheme. The recommended theme is Theme.AppCompat.*.NoActionBar. Other custom themes can also be used, however 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>
  • Also, if you would like to customize the UI components in DocumentActivity, you can use ViewerConfig.Builder. For example:

    var builder = new pdftron.PDF.Config.ViewerConfig.Builder();
    var config = builder
        .FullscreenModeEnabled(true)
        .MultiTabEnabled(true)
        .DocumentEditingEnabled(true)
        .LongPressQuickMenuEnabled(true)
        .ToolbarTitle("Simple Reader")
        .ShowSearchView(true)
        .Build();

    For details on customizing the UI and using ViewerConfig.Builder, check out the configuration tutorial.

Get the anwsers you need: Support

Contents

Prerequisites
Step 1: Update AndroidManifest.xml
Step 2: Launch the viewer
Step 3: Customize the viewer