How to view document in Android with DocumentActivity

This section will show you how to open a document in a DocumentActivity. This is an AppCompatActivity which contains a PdfViewCtrlTabHostFragment2 and packages all the standard features used during document viewing. You can learn more in our document viewer overview.

  1. The AndroidManifest.xml file needs to be updated to request permissions to allow DocumentActivity to function properly. You also need to add some attributes to the <application> tag as well as a new <activity> tag to define the DocumentActivity.Read more: detailed description of permission requirements.

XML

1<?xml version="1.0" encoding="utf-8"?>
2<!-- Include existing attributes in manifest -->
3<manifest>
4 <!-- Required permissions are added here -->
5 <uses-permission android:name="android.permission.INTERNET"/>
6 <uses-permission android:name="android.permission.RECORD_AUDIO" />
7
8 <!-- Add multidex support and enable largeHeap. 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. -->
9 <!-- Include existing attributes in application -->
10 <application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="false">
11
12 <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
13 <!-- This is from the Integration stage-->
14 <meta-data android:name="pdftron_license_key" android:value="${pdftronLicenseKey}" />
15
16 <!-- Document viewer activity declaration-->
17 <activity android:name="com.pdftron.pdf.controls.DocumentActivity" android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize" android:windowSoftInputMode="adjustPan" android:theme="@style/PDFTronAppTheme"/>
18 </application>
19</manifest>

Storage Permission

Please follow the latest Android best practices and guidelines.

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. Import the following in your MainActivity.java file.
1import android.net.Uri;
2import java.io.File;
3import com.pdftron.pdf.config.ViewerConfig;
4import com.pdftron.pdf.controls.DocumentActivity;
  1. Inside the onCreate method for your MainActivity class, add the following lines to open a demo pdf from URL.You can open documents using DocumentActivity in many ways.
1ViewerConfig config = new ViewerConfig.Builder().openUrlCachePath(this.getCacheDir().getAbsolutePath()).build();
2final Uri fileLink = Uri.parse("https://pdftron.s3.amazonaws.com/downloads/pl/PDFTRON_mobile_about.pdf");
3DocumentActivity.openDocument(this, fileLink, config);

DocumentActivity has limited runtime customizability.

If you require more customizability options, please use PdfViewCtrlTabHostFragment2 since it gives you full control over UI and behavior customization. Read more on the different components in the viewer overview guide for Andriod .

Your app should look something like this:

Apryse Docs Image

You can explore the features of the viewer and also many customization options.

More loading options

DocumentActivity can open documents from various resources including internal storage, URIs, web servers and the application's res folder.

You can open files from the these sources using the code below:

1import android.net.Uri;
2import java.io.File;
3import com.pdftron.pdf.config.ViewerConfig;
4import com.pdftron.pdf.controls.DocumentActivity;
5
6// Set the cache location using the config to store the cache file
7ViewerConfig config = new ViewerConfig.Builder().openUrlCachePath(this.getCacheDir().getAbsolutePath()).build();
8
9// from internal storage
10final Uri uri = Uri.fromFile(new File("myLocalFilePath"));
11// from content uri
12final Uri uri = Uri.parse("myContentUri");
13// from http/https
14final Uri uri = Uri.parse("myFileLink");
15// from assets
16final Uri uri = Uri.parse("file:///android_asset/my_file.pdf");
17
18// intent builder
19Intent intent = DocumentActivity.IntentBuilder.fromActivityClass(this, DocumentActivity.class)
20 .withUri(uri)
21 .usingConfig(config)
22 .usingTheme(R.style.PDFTronAppTheme)
23 .build();
24startActivity(intent);

A cache location is preferred when loading contents from web servers.

If you did not request Storage permission, the app sandbox is recommended for cache path, otherwise you have control over where you would like to store the cache file.

Next step

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales