This tutorial only applies to Xamarin.Android.
See Xamarin.iOS equivalent here . Supporting Sdk versions:
Minimum: API 16 (Using Android Support Libraries) Recommended target: API 34 This guide demonstrates how to display a PDF. To begin working with Apryse SDK, you must have first added it to your project , and initialized the library .
First make sure the following is in your <application>
tag in AndroidManifest.xml
.
XML 1 < application ... android : largeHeap = " true " android : usesCleartextTraffic = " false " >
2
3 < activity android : name = " com.pdftron.pdf.controls.DocumentActivity " android : configChanges = " keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize " android : windowSoftInputMode = " adjustPan " android : theme = " @style/PDFTronAppTheme " />
4 </ application >
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 to open HTTP files in the viewer. If you are only working with HTTPS files, this is not required. Next, add permissions to AndroidManifest.xml
file in the <manifest>
tag (outside the <application>
tag):
XML 1 < uses-permission android : name = " android.permission.INTERNET " />
2 < uses-permission android : name = " android.permission.RECORD_AUDIO " />
Permission
Purpose
android.permission.INTERNET
Internet permission for downloading online documents
android.permission.RECORD_AUDIO
Audio recording permission for creating sound annotations
Storage Permission Please follow the latest Android best practices and guidelines outlined here
Add license key and activity declarations to AndroidManifest.xml
file inside the <application>
tag:
XML 1 <!-- Add license key in meta-data tag here. This should be inside the application tag. -->
2 < meta-data android : name = " pdftron_license_key " android : value = " Insert_commercial_license_key_here_after_purchase " />
3
4 < activity android : name = " com.pdftron.pdf.controls.DocumentActivity " android : configChanges = " keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize " android : windowSoftInputMode = " adjustPan " android : theme = " @style/PDFTronAppTheme " />
Finally, if you would like to customize the appearance of the viewer activity, define PDFTronAppTheme in Resources/values/styles.xml
:
XML 1 < resources >
2 < style name = " PDFTronAppTheme " parent = " PDFTronAppThemeBase " > < 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 >
3 </ resources >
If you are using your own theme, please don't forget to add/change the theme tag of your AndroidManifest file's corresponding activity declaration. To open a PDF file:
C# 1 // Open our sample document in the 'Resources/raw' resource folder
2 pdftron.PDF.Controls.DocumentActivity. OpenDocument (Resource.Raw.getting_started);
You will see:
Please note that changes for files opened from res will not be saved on disk.
To open your own file in DocumentActivity, use:
C# 1 // from internal storage
2 var localFile = Android.Net.Uri. FromFile ( new Java . IO . File ( " myLocalFilePath " ));
3 pdftron.PDF.Controls.DocumentActivity. OpenDocument ( this , localFile);
4
5 // from content uri
6 var contentUri = Android.Net.Uri. Parse ( " myContentUri " );
7 pdftron.PDF.Controls.DocumentActivity. OpenDocument ( this , contentUri);
8
9 // from http/https
10 var fileLink = Android.Net.Uri. Parse ( " myFileLink " );
11 pdftron.PDF.Controls.DocumentActivity. OpenDocument ( this , fileLink);
12
13 // from res
14 pdftron.PDF.Controls.DocumentActivity. OpenDocument ( this , Resource.Raw.my_file_res_id);