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.

Show a document in an Activity

This tutorial only applies to Xamarin.Android.

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

Apryse Docs Image

Prerequisites

  • Integrated Apryse into your project and added the PDFTron.Android.Tools NuGet package into your project.
  • Minimum API: 16 (using AndroidX)
  • Compile API: 34
  • Recommended target API: 34

License Key Required

The trial of Apryse SDK requires a trial license key, which is provided in the box titled "License Key". A commercial license key is required for use in a production environment. Please contact sales to purchase a commercial key or if you need any other license key assistance.

Keep your license keys confidential.

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).

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

XML

1<application ... android:largeHeap="true" android:usesCleartextTraffic="false">
2</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 to open HTTP files in the viewer. If you are only working with HTTPS files, this is not required.

  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:

XML

1<manifest ...>
2 <!-- Required permissions are added here -->
3 <uses-permission android:name="android.permission.INTERNET"/>
4 <uses-permission android:name="android.permission.RECORD_AUDIO" />
5 <!-- Add enable largeHeap -->
6 <application ... android:largeHeap="true" android:usesCleartextTraffic="false">
7
8 <meta-data android:name="pdftron_license_key" android:value="your_license_key_goes_here"/>
9
10 <!-- Document viewer activity declaration-->
11 <activity android:name="com.pdftron.pdf.controls.DocumentActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
12 ...
13 </application>
14</manifest>

Step 2: Launch the viewer

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

C#

1// from internal storage
2var uri = Android.Net.Uri.FromFile(new Java.IO.File("myLocalFilePath"));
3// from content uri
4var uri = Android.Net.Uri.Parse("myContentUri");
5// from http/https
6var uri = Android.Net.Uri.Parse("myFileLink");
7// from assets
8var uri = Android.Net.Uri.Parse("file:///android_asset/my_file.pdf");
9
10// intent builder
11var intent = DocumentActivity.IntentBuilder.FromActivityClass(this, Java.Lang.Class.FromType(typeof(DocumentActivity)))
12 .WithUri(uri)
13 .Build();
14StartActivity(intent);

Step 3: (Optional) Customize the viewer

  • If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme in res/values/styles.xml:You can learn more about this in the customize the viewer's theme guide .

XML

1<style name="PDFTronAppTheme" parent="PDFTronAppThemeBase"> <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>

DocumentActivity uses the AppCompat theme for material colors. Make sure that the value of android:theme in your activity tag also extends the AppCompat theme.

  • Also, if you would like to customize the UI components in DocumentActivity, you can use ViewerConfig.Builder. For example:For details on customizing the UI and using ViewerConfig.Builder, check out the configuration tutorial .

C#

1var builder = new pdftron.PDF.Config.ViewerConfig.Builder();
2var config = builder
3 .FullscreenModeEnabled(true)
4 .MultiTabEnabled(true)
5 .DocumentEditingEnabled(true)
6 .LongPressQuickMenuEnabled(true)
7 .ToolbarTitle("Simple Reader")
8 .ShowSearchView(true)
9 .Build();

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales