Some test text!


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 PDF using PDFViewCTRL in Android

If you are looking for a quick start on displaying documents in your application, please first take a look at Show a document in an Activity or Show a document in a Fragment as they are easier to setup and ready to launch from any activity or fragment. Continue reading this article if you are looking to embed PDFViewCtrl in your own layout.
Before beginning, make sure the PDFTron library is initialized prior to inflating the layout or calling setContentView in your activity.

PDFViewCtrl is a ViewGroup that can be embedded in any layout. It encapsulates a rich set of functionalities for interactive viewing of PDF documents, including multi-threaded rendering, PDF rendering settings, scrolling, zooming, page navigation, different page viewing modes, coordinates conversion, text selection, text search, etc.

View documents using PDFViewCtrl

In this tutorial you will display a PDF file in your activity by using PDFViewCtrl.

  1. In your AndroidManifest.xml, make sure you enable largeHeap in the <application> tag. Also, add a custom theme and set the android:windowSoftInputMode:"adjustPan" attribute in the <activity> tag as follow:

    <!-- Include existing attributes in application -->
        <!-- Include existing attributes in activity -->
    If your app is targeting Android SDK version 28 or higher, please also set 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.
  2. If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme for your activity in res/values/styles.xml:

    <style name="PDFTronAppTheme" parent="PDFTronAppThemeBase">
        <item name="colorPrimary">#3F51B5</item>
        <item name="colorPrimaryDark">#303F9F</item>
        <item name="colorAccent">#FF4081</item>
        <!-- Action bar -->
        <item name="actionModeBackground">?attr/colorPrimary</item>
        <item name="windowActionModeOverlay">true</item>

    You can learn more about this in the customize the viewer's theme guide .

    PDFViewCtrl uses the AppCompat theme for material colors. Make sure that the value of android:theme in your <activity> tag also extends the AppCompat theme.
  3. Now, add PDFViewCtrl to your activity's XML layout. For example:

    <?xml version="1.0" encoding="utf-8"?>
  4. In your activity, get a reference to PDFViewCtrl after inflating the layout and call AppUtils.setupPDFViewCtrl.

    private PDFViewCtrl mPdfViewCtrl;
    // ...
    protected void onCreate(Bundle savedInstanceState) {
        mPdfViewCtrl = findViewById(;
        try {
        } catch (PDFNetException e) {
            // Handle exception
  5. Next, choose a document to display by using the following options:

    View from resource

    Add a sample PDF to src/main/res/raw folder, then call:

    import com.pdftron.pdf.utils.Utils;
    // ...
    private PDFDoc mPdfDoc;
    // ...
    public void viewFromResource(int resourceId, String fileName) throws PDFNetException {
        File file = Utils.copyResourceToLocal(this, resourceId, fileName, ".pdf");
        mPdfDoc = new PDFDoc(file.getAbsolutePath());
        // Alternatively, you can open the document using Uri:
        // Uri fileUri = Uri.fromFile(file);
        // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);

    View from local device storage

    To access device storage, add the following permissions to the AndroidManifest.xml file in the <manifest> tag (outside of the <application> tag):

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    Then call:

    private PDFDoc mPdfDoc;
    // ...
    public void viewFromLocalStorage(String myFilePath) throws PDFNetException {
        mPdfDoc = new PDFDoc(myFilePath);
        // Alternatively, you can open the document using Uri:
        // Uri fileUri = Uri.fromFile(new File(myFilePath));
        // mPdfDoc = mPdfViewCtrl.openPDFUri(fileUri, null);

    View from content uri

    private PDFDoc mPdfDoc;
    // ...
    public void viewFromContentUri(Uri contentUri) throws PDFNetException, FileNotFoundException {
        mPdfDoc = mPdfViewCtrl.openPDFUri(contentUri, null);

    View from HTTP/HTTPS

    To access via the internet, add the following permissions to the AndroidManifest.xml file in the <manifest> tag (outside of the <application> tag):

    <uses-permission android:name="android.permission.INTERNET"/>

    Then call:

    public void viewFromHttpUrl(String myHttpUrl) throws PDFNetException {
        mPdfViewCtrl.openUrlAsync(myHttpUrl, null, null, null);
        // Alternatively, you can open the document using Uri:
        // mPdfViewCtrl.openPDFUri(Uri.parse(myHttpUrl), null);

Managing lifecycle

It is extremely important that you follow the Android activity/fragment lifecycle and clean up PDFViewCtrl and PDFDoc properly. Make sure you have the following in lifecycle callbacks:

// ...
public void onPause() {
    if (mPdfViewCtrl != null) {

public void onResume() {
    if (mPdfViewCtrl != null) {

public void onDestroy() {
    if (mPdfViewCtrl != null) {
        mPdfViewCtrl = null;

    if (mPdfDoc != null) {
        try {
        } catch (Exception e) {
            // handle exception
        } finally {
            mPdfDoc = null;

Next steps

Get the answers you need: Support

Upcoming Webinar: PDFTron SDK Tech Review | Nov 29, 2022 at 2 pm ET


The Platform


© 2022 PDFTron Systems Inc. All rights reserved.


Terms of Use