Some test text!

menu
search
2. View a documentkeyboard_arrow_down

View a document using DocumentActivity

This section will tell you how to open a document in a DocumentActivity. This is an AppCompatActivity which contains a PDFViewCtrlTabHostFragment and packages the all the standard features used during document viewing.

  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 DocumentAcivity.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest 
     ... (existing attributes)
    >
        <!-- Required permissions are added here -->
        <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.RECORD_AUDIO" />
    
        <!-- Add multidex support, enable largeHeap, and 
        enable usesCleartextTraffic in application attributes-->
        <application 
            ...(existing attributes)
            android:name="android.support.multidex.MultiDexApplication"
            android:largeHeap="true"
            android:usesCleartextTraffic="true">
    
            <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
            <!-- This is from the Integration stage-->
            <meta-data
                android:name="pdftron_license_key"
                android:value="${pdftronLicenseKey}"/>
    
            <!-- Document viewer activity declaration-->
            <activity android:name="com.pdftron.pdf.controls.DocumentActivity"
                android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
                android:windowSoftInputMode="adjustPan"
                android:theme="@style/CustomAppTheme"/>
            ...
        </application>
    </manifest>

    A more detailed description of permission requirements can be found here.

    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.
    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.
  2. Import the following in your MainActivity.java file.

    import android.net.Uri;
    import java.io.File;
    import com.pdftron.pdf.config.ViewerConfig;
    import com.pdftron.pdf.controls.DocumentActivity;
  3. Inside the onCreate method for your MainActivity class, add the following lines to open a demo pdf from URL.

    ViewerConfig config = new ViewerConfig.Builder().openUrlCachePath(this.getCacheDir().getAbsolutePath()).build();
    final Uri fileLink = Uri.parse("https://pdftron.s3.amazonaws.com/downloads/pl/PDFTRON_mobile_about.pdf");
    DocumentActivity.openDocument(this, fileLink, config);

    You can open documents using DocumentActivity in many ways. Find out more here.

DocumentActivity has limited runtime customizability.

If you require more customizability options, please use PdfViewCtrlTabHostFragment since it gives you full control over UI and behavior customization. You can find out more about the different components here.

Your app should look something like this:

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

linkMore 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:

import android.net.Uri;
import java.io.File;
import com.pdftron.pdf.config.ViewerConfig;
import com.pdftron.pdf.controls.DocumentActivity;

// Set the cache location using the config to store the cache file
ViewerConfig config = new ViewerConfig.Builder().openUrlCachePath(this.getCacheDir().getAbsolutePath()).build();

// 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 (the file must be placed in the `res/raw folder`)
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);
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.

linkNext step

Customize

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn More
close