Some test text!
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.
PDFViewCtrl
in your own layout.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.
In this tutorial you will display a PDF file in your activity by using PDFViewCtrl
.
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:
<application
...
android:name="androidx.multidex.MultiDexApplication"
android:largeHeap="true"
android:usesCleartextTraffic="true">
...
<activity
...
android:windowSoftInputMode="adjustPan"
android:theme="@style/PDFTronAppTheme"/>
</application>
android:usesCleartextTraffic="true"
attribute in your application tag.If you would like to customize the appearance of the viewer activity, define PDFTronAppTheme
for your activity in res/values/styles.xml
:
<resources>
<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>
</style>
</resources>
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.Now, add PDFViewCtrl
to your activity's XML layout. For example:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<pdftron.pdf.PDFViewCtrl
android:id="@+id/pdfviewctrl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical|horizontal"/>
</FrameLayout>
In your activity, get a reference to PDFViewCtrl
after inflating the layout and call AppUtils.SetupPDFViewCtrl
.
using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;
private PDFViewCtrl mPdfViewCtrl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
mPdfViewCtrl = FindViewById<PDFViewCtrl>(Resource.Id.pdfviewctrl);
AppUtils.SetupPDFViewCtrl(mPdfViewCtrl);
}
Next, choose a document to display by using the following options:
Add a sample PDF to src/main/res/raw
folder, then call:
using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;
using Android.Net;
private PDFDoc mPdfDoc;
void ViewFromResource(int resourceId, string fileName)
{
var file = Utils.CopyResourceToLocal(this, resourceId, fileName, ".pdf");
mPdfDoc = new PDFDoc(file.AbsolutePath);
mPdfViewCtrl.SetDoc(mPdfDoc);
// Alternatively, you can open the document using Uri:
// mPdfDoc = mPdfViewCtrl.OpenPDFUri(Uri.FromFile(file), "");
}
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:
using pdftron.PDF;
using pdftron.PDF.Config;
using pdftron.PDF.Tools.Utils;
using Android.Net;
private PDFDoc mPdfDoc;
void ViewFromLocalStorage(string myFilePath)
{
mPdfDoc = new PDFDoc(myFilePath);
mPdfViewCtrl.SetDoc(mPdfDoc);
// Alternatively, you can open the document using Uri:
// mPdfDoc = mPdfViewCtrl.OpenPDFUri(Uri.FromFile(new Java.IO.File(myFilePath)), null);
}
using Android.Net;
private PDFDoc mPdfDoc;
void ViewFromContentUri(Uri contentUri)
{
mPdfDoc = mPdfViewCtrl.OpenPDFUri(contentUri, null);
}
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:
void ViewFromHttpUrl(string myHttpUrl)
{
pdfViewCtrl.OpenUrlAsync(myHttpUrl, null, null, null);
// Alternatively, you can open the document using Uri:
// mPdfViewCtrl.OpenPDFUri(Android.Net.Uri.Parse(myHttpUrl), null);
}
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:
// ...
protected override void OnPause()
{
base.OnPause();
mPdfViewCtrl?.Pause();
}
protected override void OnResume()
{
base.OnResume();
mPdfViewCtrl?.Resume();
}
protected override void OnDestroy()
{
base.OnDestroy();
mPdfViewCtrl?.Destroy();
mPdfDoc?.Close();
}
PDFViewCtrl
? Check out the Setup ToolManager guide.PDFViewCtrl
? Check out the viewing other document types guide.Get the answers you need: Support
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.
Web
Android
iOS
Windows
Linux
Unlimited usage. No email address required.