Open a document

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.

Display document in fragment on Android

All actions related to the PDF viewer are handled through the PdfViewCtrlTabHostFragment2. This fragment extends androidx.fragment.app.Fragment and is responsible for showing documents in tabs.

Apryse Docs Image

Prerequisites

No trial license key required.

The trial of Apryse Mobile SDK does not require a trial 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 PdfViewCtrlTabHostFragment2, 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, usesClearTextTraffic, and set android:name in the <application> tag to MultiDexApplication:

XML

1<!-- Include existing attributes in application -->
2<application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="true">
3</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. Declare your activity in the manifest file:

XML

1<!-- Include existing attributes in application -->
2<application>
3 <!-- Declare your activity that will use PdfViewCtrlTabHostFragment2 -->
4 <!-- Include existing attributes in activity -->
5 <activity android:theme="@style/PDFTronAppTheme" android:windowSoftInputMode="adjustPan" />
6</application>

Note that your activity must extend AppCompatActivity.

  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. The final AndroidManifest.xml file should look something like this:

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, enable largeHeap, and enable usesCleartextTraffic -->
9 <!-- Include existing attributes in application -->
10 <application android:name="androidx.multidex.MultiDexApplication" android:largeHeap="true" android:usesCleartextTraffic="true">
11
12 <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
13 <meta-data android:name="pdftron_license_key" android:value="${pdftronLicenseKey}"/>
14
15 <!-- Document viewer activity declaration-->
16 <!-- Include existing attributes in activity -->
17 <activity android:theme="@style/PDFTronAppTheme" android:windowSoftInputMode="adjustPan"/>
18 </application>
19</manifest>

Step 2: Launch the viewer

Use ViewerBuilder2 to create an instance of PdfViewCtrlTabHostFragment2, and add it to your activity layout. To add a document viewer fragment for a given password-protected file, call the following method in your activity:

1// Add a viewer fragment to the layout container in the specified
2// activity, and returns the added fragment
3public PdfViewCtrlTabHostFragment2 addViewerFragment(@IdRes int fragmentContainer,
4 @NonNull AppCompatActivity activity, @NonNull Uri fileUri, @Nullable String password) {
5
6 // Create the viewer fragment
7 PdfViewCtrlTabHostFragment2 fragment =
8 ViewerBuilder2.withUri(fileUri, password).build(activity);
9
10 // Add the fragment to the layout fragment container
11 activity.getSupportFragmentManager().beginTransaction()
12 .replace(fragmentContainer, fragment)
13 .commit();
14
15 return fragment;
16}

where fragmentContainer is the resource id of a layout in your activity that will contain your fragment:

For example:

XML

1<?xml version="1.0" encoding="utf-8"?>
2<!-- This FrameLayout will contain our viewer fragment-->
3<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>

Alternatively if you have extended PdfViewCtrlTabFragment2 or PdfViewCtrlTabHostFragment2, you can specify your custom classes using the ViewerBuilder2.usingTabClass() method and the ViewerBuilder2.build() method as follows:

1// Add a viewer fragment to the layout container in the specified
2// activity, and returns the added fragment
3public MyCustomTabHostFragment addViewerFragment(@IdRes int fragmentContainer,
4 @NonNull AppCompatActivity activity, @NonNull Uri fileUri, @Nullable String password) {
5
6 // Create the viewer fragment with a custom
7 // PdfViewCtrlTabFragment2 and PdfViewCtrlTabHostFragment2
8 PdfViewCtrlTabHostFragment2 fragment =
9 ViewerBuilder2.withUri(fileUri, password)
10 .usingTabClass(MyCustomTabFragment.class)
11 .build(activity, MyCustomTabHostFragment.class);
12
13 // Add the fragment to the layout fragment container
14 activity.getSupportFragmentManager().beginTransaction()
15 .replace(fragmentContainer, fragment)
16 .commit();
17
18 return fragment;
19}

Since Apryse uses the Fragment class from the Support Library, your activity must extend AppCompatActivityand call getSupportFragmentManager() to get the FragmentManager.

Step 3: Customize the viewer

Customize document viewer style

  • If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme in 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>

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

Customize using ViewerConfig

  • If you would like to customize certain viewer settings or the UI of PdfViewCtrlTabHostFragment2, you can use ViewerConfig.Builder. For example:
1// Pass a custom ViewerConfig object to initialize your viewer fragment
2public PdfViewCtrlTabHostFragment2 createUsingViewerConfig(@NonNull Context context,
3 @NonNull Uri fileUri, @Nullable String password) {
4
5 // Create a ViewerConfig object with custom settings
6 ViewerConfig config = new ViewerConfig.Builder()
7 .fullscreenModeEnabled(true)
8 .multiTabEnabled(true)
9 .documentEditingEnabled(true)
10 .longPressQuickMenuEnabled(true)
11 .toolbarTitle("Host Fragment")
12 .showSearchView(true)
13 .build();
14
15 // Pass in the ViewerConfig object into the ViewerBuilder2
16 return ViewerBuilder2.withUri(fileUri, password)
17 .usingConfig(config)
18 .build(context);
19}

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

Customize the options toolbar

Apryse Docs Image

The default toolbar menu consists of the following buttons on phones:

  • App navigation
  • Toolbar switcher
  • Tab switcher
  • Overflow menu
Apryse Docs Image

The default toolbar menu consists of the following buttons on tablets:

  • App navigation
  • Toolbar switcher
  • Document text search
  • View mode configuration
  • Thumbnails browser
  • List container
  • Overflow menu

You can fully customize the toolbar menu and the navigation icon by calling the following in ViewerBuilder2, with custom menu resource and drawable resource files:

1public PdfViewCtrlTabHostFragment2 createUsingCustomToolbar(@NonNull Context context,
2 @NonNull Uri fileUri, @DrawableRes int navIcon, @MenuRes int[] menuRes) {
3 return ViewerBuilder2.withUri(fileUri)
4 .usingCustomToolbar(menuRes)// Specify a custom toolbar
5 .usingNavIcon(navIcon) // Specify a custom navigation component
6 .build(context);
7}

To change the icon color and overflow icon color, in styles.xml:

XML

1<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar"> <item name="colorControlNormal">@color/red</item> <item name="iconTint">@color/red</item> </style>

To change the navigation icon color, in styles.xml (assume MyTheme is used as the app theme):

XML

1<style name="MyTheme" parent="PDFTronAppTheme"> <item name="toolbarNavigationButtonStyle">@style/ToolbarButtonNavigationStyle</item> </style>
2
3<style name="ToolbarButtonNavigationStyle" parent="Widget.AppCompat.Toolbar.Button.Navigation"> <item name="android:tint">@color/red</item> </style>

Step 4: Interact with the fragment

If you would like to interact with the host fragment you can call addHostListener(TabHostListener) and override the methods that you are interested in. For example, you may want to override onToolbarOptionsItemSelected(MenuItem) when you add a new menu item, so when the item is clicked you can get a callback. As another example, you can get the callback when the navigation icon is clicked if you override onNavButtonPressed().

Here's an example that replaces the default navigation icon and uses a custom toolbar:

1public class CustomToolbarActivity extends AppCompatActivity implements PdfViewCtrlTabHostFragment2.TabHostListener {
2
3@Nullable
4private PdfViewCtrlTabHostFragment2 mPdfViewCtrlTabHostFragment;
5
6// Method used to initialize the viewer fragment with a custom toolbar.
7public void createCustomToolbarFragment(@NonNull Uri fileUri) {
8 mPdfViewCtrlTabHostFragment = ViewerBuilder2.withUri(fileUri)
9 // Specify a custom toolbar
10 .usingCustomToolbar(new int[] {R.menu.my_custom_toolbar})
11 // Specify a custom navigation component
12 .usingNavIcon(R.drawable.ic_arrow_back_white_24dp)
13 .build(this);
14}
15
16@Override
17public boolean onToolbarOptionsItemSelected(MenuItem item) {
18 if (item.getItemId() == R.id.action_show_toast) {
19 Toast.makeText(this, "Show toast is clicked!", Toast.LENGTH_SHORT).show();
20 }
21 return false;
22}
23
24@Override
25public void onNavButtonPressed() {
26 // called when navigation button has been clicked
27}
28
29@Override
30protected void onDestroy() {
31 super.onDestroy();
32 if (mPdfViewCtrlTabHostFragment != null) {
33 mPdfViewCtrlTabHostFragment.removeHostListener(this);
34 }
35}
36
37// ...

where ic_arrow_back_white_24dp.xml is a drawable resource file for a back arrow icon, and my_custom_toolbar.xml is a menu resource file that contains:

XML

1<?xml version="1.0" encoding="utf-8"?>
2<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
3 <item android:id="@+id/action_settings" android:icon="@drawable/ic_settings" android:title="@string/action_settings" app:showAsAction="ifRoom"/>
4 <item android:id="@+id/action_show_toast" android:title="Show toast" app:showAsAction="never" />
5</menu>

This sample replaces the navigation icon, removes all toolbar buttons except the annotation toolbar button, and adds a new Show Toast button:

Apryse Docs Image

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales