Step 2: View a document in Xamarin.Android

If you do not already have an existing application, create a new Visual Studio Xamarin.Android project with Single View App.

  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:

XML

1<!-- Enable usesCleartextTraffic in application attributes if you are working with HTTP files. If you are only working with HTTPS files, this is not required. -->
2<application ... android:largeHeap="true" android:usesCleartextTraffic="false">
3 ...
4</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 the <meta-data> tag containing a reference to your license key in the AndroidManifest.xml file. Also, declare DocumentActivity in the same file. The final AndroidManifest.xml file should look something like this:

XML

1<?xml version="1.0" encoding="utf-8"?>
2<manifest ...>
3 <!-- Required permissions are added here -->
4 <uses-permission android:name="android.permission.INTERNET"/>
5 <uses-permission android:name="android.permission.RECORD_AUDIO" />
6
7 <!-- Add multidex support, enable largeHeap, and enable usesCleartextTraffic. Also enable usesCleartextTraffic in application attributes if you are working with HTTP files. If you are only working with HTTPS files, this is not required. -->
8 <application ... android:largeHeap="true" android:usesCleartextTraffic="true">
9
10 <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
11 <meta-data android:name="pdftron_license_key" android:value="INSERT_COMMERCIAL_LICENSE_KEY_HERE_AFTER_PURCHASE"/>
12
13 <!-- Document viewer activity declaration-->
14 <activity android:name="com.pdftron.pdf.controls.DocumentActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
15 ...
16 </application>
17</manifest>

Storage Permission

Please follow the latest Android best practices and guidelines outlined here

  1. Add the following two lines at the end of the OnCreate method in MainActivity.cs:

C#

1var config = new pdftron.PDF.Config.ViewerConfig.Builder().OpenUrlCachePath (this.CacheDir.AbsolutePath).Build();
2var intent = DocumentActivity.IntentBuilder.FromActivityClass(this, Java.Lang.Class.FromType(typeof(DocumentActivity)))
3 .WithUri(Android.Net.Uri.Parse("https://pdftron.s3.amazonaws.com/downloads/pdfref.pdf"))
4 .UsingConfig(config)
5 .Build();
6StartActivity(intent);

Note that using the cache is a temporary solution and you should request runtime permissions for external storage.

  1. Build your project and deploy it to your Android Device/Emulator. You should see the viewer start up:
Apryse Docs Image

Next step

To view a document from other sources into DocumentActivity, check out this guide .

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales