Try demo in your projectkeyboard_arrow_down

Try the PDFTron demo in your application

Supported SDK versions:

  • Minimum: API 16 (using Android Support Libraries)
  • Recommended target: API 27

The following are a few simple steps that will prepare your Android Studio project for your use of the PDFTron demo Activities in one of your own projects.

First, add credentials to your project's gradle.properties file:

AWS_ACCESS_KEY=YOUR_ACCESS_KEY_GOES_HERE
AWS_SECRET_KEY=YOUR_SECRET_KEY_GOES_HERE
PDFTRON_LICENSE_KEY=YOUR_PDFTRON_LICENSE_KEY_GOES_HERE

Next, add rules to your project's root-level build.gradle file in order to include PDFTron's Maven repository. For example:

Copy the following as is, do not import org.gradle.api.credentials.Credentials:

allprojects {
    ...
    repositories {
        ...
        maven {
            url "s3://pdftron-maven/release"
            credentials(AwsCredentials) {
                accessKey AWS_ACCESS_KEY
                secretKey AWS_SECRET_KEY
            }
        }
    }
}
Please make sure the above is added to the allprojects block, NOT the buildscript block.

Then, in your module's Gradle file (usually app/build.gradle), add:

android {
    defaultConfig {
        ...
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
        manifestPlaceholders = [pdftronLicenseKey:PDFTRON_LICENSE_KEY]
    }
    configurations.all {
        resolutionStrategy.force "com.android.support:appcompat-v7:27.0.2"
        resolutionStrategy.force "com.android.support:support-v4:27.0.2"
        resolutionStrategy.force "android.arch.lifecycle:runtime:1.0.3"
    }
}

dependencies {
    ...
    implementation "com.pdftron:pdftron:6.8.+"
    implementation "com.pdftron:tools:6.8.+"
    implementation "com.pdftron:demo:6.8.+"

    implementation 'com.android.support:multidex:1.0.3'
}
If you encountered any issue with gradle sync, check out the Troubleshooting guide.

Next, add permissions to the AndroidManifest.xml file (outside the <application> tag):

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

Add a license key placeholder and activity declarations to the AndroidManifest.xml file (inside the <application> tag):

<application
    android:name="android.support.multidex.MultiDexApplication" >
    <meta-data
        android:name="pdftron_license_key"
        android:value="${pdftronLicenseKey}"/>

    <activity android:name="com.pdftron.demo.app.SimpleReaderActivity"
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/CustomAppTheme"/>

    <activity android:name="com.pdftron.demo.app.AdvancedReaderActivity"
        android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
        android:windowSoftInputMode="adjustPan"
        android:theme="@style/CustomAppTheme"/>

    <activity android:name="com.pdftron.demo.app.SettingsActivity"
        android:parentActivityName="com.pdftron.demo.app.AdvancedReaderActivity"
        android:theme="@style/CustomAppTheme" />
</application>

Finally, add a style to res/values/styles.xml:

<resources>
    <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>
        <!-- Drawer -->
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
        <!-- Action bar -->
        <item name="actionModeBackground">?attr/colorPrimary</item>
        <item name="windowActionModeOverlay">true</item>
    </style>
</resources>
If you are using your own theme, please don't forget to add or change the theme tag of your AndroidManifest file's corresponding activity declaration.

To open the Advanced Reader demo, use the following code:

import com.pdftron.demo.app.AdvancedReaderActivity;
...
AdvancedReaderActivity.open(this);

You will see the following:

To open the Simple Reader demo, use the following code:

import com.pdftron.demo.app.SimpleReaderActivity;
...
SimpleReaderActivity.open(this);

You will see the following:

Please note that any changes made to files opened from res will not be saved on the disk.

To open your own file in the Simple Reader demo, use the following code:

import com.pdftron.demo.app.SimpleReaderActivity;
...
// from internal storage
final Uri localFile = Uri.fromFile(new File("myLocalFilePath"));
SimpleReaderActivity.openDocument(context, localFile);

// from content uri
final Uri contentUri = Uri.parse("myContentUri");
SimpleReaderActivity.openDocument(context, contentUri);

// from http/https
final Uri fileLink = Uri.parse("myFileLink");
SimpleReaderActivity.openDocument(context, fileLink);

// from res
SimpleReaderActivity.openDocument(context, R.raw.my_file_res_id);