Using an Activitykeyboard_arrow_down

Using an Activity

DocumentActivity requires the full version of PDFTron library. See PDFTron full or standard?

PDFTron ships with DocumentActivity which is an all-in-one document reader and PDF editor. In addition to PDF files, it also supports viewing of other file extensions such as .docx, .doc, .pptx, .xlsx, .md, .cbz and various image formats. It allows stream conversion of these non-pdf documents to PDF format so you can view the document while conversion happens. In this activity, you can read, annotate, sign, fill in PDF forms and more. See detailed features list here.

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

Please note that DocumentActivity requires the com.pdftron:tools package.

Here are a few simple steps to get started with DocumentActivity.

  1. First, add activity declarations to AndroidManifest.xml file:
<application
    ...
    android:supportsRtl="true"
    android:largeHeap="true">
    ...
    <activity android:name="com.pdftron.pdf.controls.DocumentActivity"
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/CustomAppTheme"/>
</application>
  1. Then, add theme to 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-based theme. The recommended theme is Theme.AppCompat.*.NoActionBar. Other custom themese 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>
  1. Then, if you would like to customize the UI of DocumentActivity, you can use ViewerConfig builder. For example:
import com.pdftron.pdf.config.ViewerConfig;

ViewerConfig.Builder builder = new ViewerConfig.Builder();
ViewerConfig config = builder
    .fullscreenModeEnabled(true)
    .multiTabEnabled(true)
    .documentEditingEnabled(true)
    .longPressQuickMenuEnabled(true)
    .toolbarTitle("Simple Reader")
    .showSearchView(true)
    .build();

For details on customizing the UI interface and sample usage of ViewerConfig builder, check out the configuration tutorial.

  1. If you need storage permission for accessing local documents, add the following permissions to AndroidManifest.xml file outside of the <application> tag:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Storage permission
Please note that from Android 6.0, API 23, and up, application needs to request storage permission at runtime before accessing any files on device.
  1. If you need internet permission for downloading online documents, add the following permissions to AndroidManifest.xml file outside of the <application> tag:
<uses-permission android:name="android.permission.INTERNET"/>
  1. Lastly, launch the activity from anywhere in your application:
import android.net.Uri;
import java.io.File;
import com.pdftron.pdf.controls.DocumentActivity;

// from internal storage
final Uri localFile = Uri.fromFile(new File("myLocalFilePath"));
DocumentActivity.openDocument(context, localFile);
// or if using config
DocumentActivity.openDocument(context, localFile, config);
// or if using config and opening password protected file
DocumentActivity.openDocument(context, localFile, password, config);

// from content uri
final Uri contentUri = Uri.parse("myContentUri");
DocumentActivity.openDocument(context, contentUri);
// or if using config
DocumentActivity.openDocument(context, contentUri, config);
// or if using config and opening password protected file
DocumentActivity.openDocument(context, contentUri, password, config);

// from http/https
final Uri fileLink = Uri.parse("myFileLink");
DocumentActivity.openDocument(context, fileLink);
// or if using config
DocumentActivity.openDocument(context, fileLink, config);
// or if using config and opening password protected file
DocumentActivity.openDocument(context, fileLink, password, config);

// from res
DocumentActivity.openDocument(context, R.raw.my_file_res_id);
// or if using config
DocumentActivity.openDocument(context, R.raw.my_file_res_id, config);
// or if using config and opening password protected file
DocumentActivity.openDocument(context, R.raw.my_file_res_id, password, config);