Welcome to foxit. This guide will show you how to integrate Foxit PDF SDK for Android into your Xamarin.Android/.NET Android application.
- System Requirements
- Integrate Foxit PDF SDK into your Xamarin project
- Integrate Foxit PDF SDK into your .NET project
- Build a Xamarin/.NET Android project using Foxit PDF SDK for Android
- How to run the complete_pdf_viewer
- Xamarin Demo
- .NET Demo
- Issues
- More
- License
- Visual Studio
Foxit PDF SDK Version >= 8.4.0: Visual Studio 2022 and above
Foxit PDF SDK Version < 8.4.0: Visual Studio 2019 and above - TargetFrameworkVersion >= Android 10.0
- Minimum Android Version: API 19 (Android4.4)
- JDK Version >= 1.8
There are two ways to integrate Foxit PDF SDK into your Xamarin project:
-
Integrate manually by building and referencing DLLs
It is now possible to integrate Foxit PDF SDK into your Xamarin project through NuGet packages, which is much easier than the second way Integrate manually by building and referencing DLLs , and can save you much time.
To integrate Foxit PDF SDK into your Xamarin project through Nuget packages, please follow the steps below:
-
In the Solution Explorer, right-click the References node of your project, and click
Manage NuGet Packages…
-
Then select the Browse tab, search for
Foxit.Android
,Foxit.Android.UIExtensions
and install them. -
Get key. If you do not already have a valid Foxit license key, please download
foxitpdfsdk_(version_no)_android.zip
from https://developers.foxitsoftware.com/pdf-sdk/android/ to get the key.
After finishing the above steps, the References of your project will look like:
To integrate manually, you should first build DLLs, and then add it as a reference to your project.
-
Download or Clone Foxit PDF SDK for Android, then copy the following files (libraries and licenses) in the
libs
folder of the extracted package tofoxit_xamarin_android\libs
directory:FoxitRDK.aar
FoxitRDKUIExtensions.aar
rdk_key.txt
rdk_sn.txt
-
Build DLLs
You need to manually compile to get the following dll:FoxitRDK.dll
MSAL.dll
MSCommon.dll
RMSSDK.dll
RMSSDK_UI.dll
XCrash.dll
FoxitUIExtensions.dll
RxAndroid.dll
Cropper.dll
Compilation steps:
Step1: LoadFoxitUIExtensions.sln
in Visual Studio 2019 under thefoxit_xamarin_android\FoxitUIExtensions
directory.
Step2: Right-click FoxitUIExtensions project, click Build/Rebuild .
Step3: If build is successful, the
FoxitRDK.dll
MSAL.dll
MSCommon.dll
RMSSDK.dll
RMSSDK_UI.dll
XCrash.dll
will be generated infoxit_xamarin_android\FoxitRDK\FoxitRDK\bin\Debug (or release)
directory.the
FoxitRDKUIExtensions.dll
RxAndroid.dll
Cropper.dll
will be generated infoxit_xamarin_android\FoxitUIExtensions\FoxitUIExtensions\bin\Debug (or release)
directory. -
Add the built DLLs as references to your project
This section takesFoxitRDK.dll
as an example to show you how to add it as a reference to your project. For other DLLs, do the same steps withFoxitRDK.dll
. We assume that you have got theFoxitRDK.dll
, if not, please refer to Build DLLs section to build the FoxitRDK project and generate theFoxitRDK.dll
.-
In the Solution Explorer, right-click the References node of your project and select
Add Reference…
-
In the Reference Manager dialog, click Browse… to find the
FoxitRDK.dll
(in thefoxit_xamarin_android\FoxitRDK\FoxitRDK\bin\Debug (or release)
folder), select it and then click Add:Then, the FoxitRDK.dll will appear in the list and has already been checked. Click OK:
-
-
Add the following dll to you demo refer to the steps of adding
FoxitRDK.dll
.MSAL.dll
MSCommon.dll
RMSSDK.dll
RMSSDK_UI.dll
XCrash.dll
FoxitUIExtensions.dll
RxAndroid.dll
Cropper.dll
-
Note:
If you integrate manually by building dlls, you must manually install the following dependencies.
Xamarin.AndroidX.Browser
Xamarin.Google.Android.Material
Xamarin.AndroidX.AppCompat
Xamarin.AndroidX.Legacy.Support.V4
Xamarin.AndroidX.Lifecycle.LiveData
Xamarin.AndroidX.ConstraintLayout
(It's required for opening a RMS protected PDF file)
After finishing the above steps, the References of your project will look like:
-
Similar to Xamarin, but the referencing DLLs method is more complex and .NET does not recommend using it. We recommend using nupkg, please refer to Integrate with NuGet.
This section will help you to quickly build a full-featured PDF Reader in Xamarin Android platform with step-by-step instructions provided.
- Create a new Xamarin Android project
- Integrate Foxit PDF SDK into the project
- Initialize Foxit PDF SDK Library
- Display a PDF document using PDFViewCtrl
Open Visual Studio 2019, choose File -> New -> Project…
to start the New Project. Let's take TestXamarin
as an example .Create a new Xamarin Android project called TestXamarin
,Then click OK:
In the New Android App dialog, select Single View App,Then, click OK:
Please refer to Integrate Foxit PDF SDK into your Xamarin project to integrate the Foxit PDF SDK into the created project. We recommend using the first way Integrate with NuGet, which is more easy and convenient.
Before calling any APIs, you must first initialize Foxit PDF SDK library by using the function
Library.Initialize(sn, key). Below you can see an example of how to initialize the SDK library. The
next section will show you where to include this code in MainActivity.cs
file:
using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
...
String sn = "xxx";
String key = "xxx";
int errCode = Library.Initialize(sn, key);
if (errCode != Constants.EErrSuccess)
return;
Note: The value of sn
can be found in the rdk_sn.txt
(the string after SN=
) and the value of key
can be found
in the rdk_key.txt
(the string after Sign=
). The trial license files (rdk_sn.txt and rdk_key.txt) can be found in the
foxit_xamarin_android\libs
folder or in the libs
folder of Foxit PDF SDK for Android package
To display a PDF document, please follow the steps below:
1) Instantiate a PDFViewCtrl object to show an existing document.
In MainActivity.cs
file, instantiate a PDFViewCtrl object, and call PDFViewCtrl#OpenDoc
function to open and render the PDF document:
using Com.Foxit.Sdk;
...
private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";
private PDFViewCtrl pdfViewCtrl;
...
// Instantiate a PDFViewCtrl object.
pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);
// Open and Render a PDF document.
pdfViewCtrl.OpenDoc(path, null);
SetContentView(pdfViewCtrl);
Note: Please make sure you have pushed the complete_pdf_viewer_guide_android.pdf document into the SD card of the Android device or emulator that will be used to run this project. Certainly, you can change the file path with your own files.
Update MainActivity.cs as follows:
using System;
using Android.App;
using Android.OS;
using AndroidX.AppCompat.App;
using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
namespace TestXamarin
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar",MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
// The value of "sn" can be found in the "rdk_sn.txt".
// The value of "key" can be found in the "rdk_key.txt".
private String sn = "xxx";
private String key = "xxx";
private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";
private PDFViewCtrl pdfViewCtrl;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Initialize Foxit SDK Library.
int errCode = Library.Initialize(sn, key);
if (errCode != Constants.EErrSuccess)
return;
// Instantiate a PDFViewCtrl object.
pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);
// Open and Render a PDF document.
pdfViewCtrl.OpenDoc(path, null);
SetContentView(pdfViewCtrl);
}
}
}
2) Set permissions to write and read the SD card of the Android devices or emulators
Note: If you want to run this project on an Android 6.0 (API 23) or higher devices/emulators, you
can do one of the following:
-
a) Change the android:targetSdkVersion in "AndroidManifest.xml" to a lower SDK version that is less than 23, such as 21
-
b) Write additional code to require the authorization of runtime permissions.
-
a) Change the android:targetSdkVersion in
AndroidManifest.xml
In this case, set the
users-permission
in theAndroidManifest.xml
found in theTestXamarin\Properties
to give the project permission to write and read the SD card of the Android devices or emulators:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- b) Write additional code to require the authorization of runtime permissions.
In theMainActivity.cs
file, add code to require the authorization of runtime permissions.
So, update the whole MainActivity.cs as follows:
using System;
using Android.App;
using Android.OS;
using Android.Runtime;
using AndroidX.AppCompat.App;
using Com.Foxit.Sdk;
using Com.Foxit.Sdk.Common;
using Android;
using Android.Content.PM;
using AndroidX.Core.Content;
using AndroidX.Core.App;
namespace TestXamarin
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar",MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
public static int REQUEST_EXTERNAL_STORAGE = 1;
private static string[] PERMISSIONS_STORAGE = {
Manifest.Permission.ReadExternalStorage,
Manifest.Permission.WriteExternalStorage
};
// The value of "sn" can be found in the "rdk_sn.txt".
// The value of "key" can be found in the "rdk_key.txt".
private String sn = "xxx";
private String key = "xxx";
private String path = "/mnt/sdcard/complete_pdf_viewer_guide_android.pdf";
private PDFViewCtrl pdfViewCtrl;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Initialize Foxit SDK Library.
int errCode = Library.Initialize(sn, key);
if (errCode != Constants.EErrSuccess)
return;
// Instantiate a PDFViewCtrl object.
pdfViewCtrl = new PDFViewCtrl(this.ApplicationContext);
// Require the authorization of runtime permissions.
if (Build.VERSION.SdkInt > BuildVersionCodes.M)
{
Permission permission =
ContextCompat.CheckSelfPermission(this.ApplicationContext,
Manifest.Permission.WriteExternalStorage);
if (permission != Permission.Granted)
{
ActivityCompat.RequestPermissions(this, PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE);
return;
}
}
// Open and Render a PDF document.
pdfViewCtrl.OpenDoc(path, null);
SetContentView(pdfViewCtrl);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
if (requestCode == REQUEST_EXTERNAL_STORAGE
&& grantResults[0] == Permission.Granted)
{
if (pdfViewCtrl != null)
{
pdfViewCtrl.OpenDoc(path, null);
}
}
else
{
base.OnRequestPermissionsResult(requestCode, permissions,
grantResults);
}
}
}
}
3) Enable Multi-Dex. If you don't, you may encounter the problem "error MSB6006: "java.exe" exited with code 2.
Right-click the Properties
node of TestXamarin project, find Android Options
then check Enable Multi-Dex
and set Dex compiler
to d8:
In this chapter, we build and run the project on an AVD targeting 9.0 (API 28), and use the second method (require authorization of runtime permissions) to get the permissions to write and read the SD card of the emulator.
After building the project and installing APK on the emulator, tap Allow on the pop-up window to allow
the project to access files on the device, and then you will see that the complete_pdf_viewer_guide_android.pdf
document is displayed . Now, this
sample app has some basic PDF features, such as zooming in/out and page turning.
1: Download foxitpdfsdk_(version_no)_android.zip
from https://developers.foxitsoftware.com/pdf-sdk/android/, then unzip the foxitpdfsdk_(version_no)_android.zip
, and enter the libs
directory,copy the following files into the foxit_xamarin_android/libs
directory:
rdk_key.txt
rdk_sn.txt
2:
2.1: Xamarin Demo Run
the FoxitRDKDemo.Xamarin\complete_pdf_viewer.sln
project in the simulator or on a physical device.
2.2: .NET Demo Run
the FoxitRDKDemo.Net\complete_pdf_viewer.sln
project in the simulator or on a physical device.
Please refer to FoxitRDKDemo.Xamarin\complete_pdf_viewer.sln
project.
Please refer to FoxitRDKDemo.Net\complete_pdf_viewer.sln
project.
Solution:Right-click the Properties
node of your project, and open it. Find Android Options
and click Advanced
button find Java Max Heap Size
set it to 2G. As show below:
2: invalid opcode ba - invokedynamic requires --min-sdk-version >= 26 (currently 13), Cannot fit requested classes in a single dex file (# methods: 87605 > 65536)
Solution:Right-click the Properties
node of your project, and open it. Find Android Options
and check Enable Multi-Dex
and set Dex compiler
to d8. As show below:
3: Attribute application@theme value=(@style/AppTheme) from AndroidManifest.xml is also present at AndroidManifest.xml value=(@style/fx_app_theme).
Suggestion: add 'tools:replace="android:theme"' to element at AndroidManifest.xml.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:tools="http://schemas.android.com/tools"
...>
...
...
<application
...
+ tools:replace="android:theme">
...
</application>
</manifest>
For more usage of xamarin, you can refer to foxitpdfsdk_(version_no)_android\docs\developer_guide_android.pdf
the section 9.3 . the foxitpdfsdk_(version_no)_android.zip
can download from https://developers.foxitsoftware.com/pdf-sdk/android/
https://github.com/edmodo/cropper
Copyright 2013, Edmodo, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this work except in compliance with the License. You may obtain a copy
of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
https://github.com/AzureAD/azure-activedirectory-library-for-android
Copyright (c) Microsoft Corporation.
All rights reserved. This code is licensed under the MIT License.
We Value and Adhere to the Microsoft Open Source Code of Conduct
This project has adopted the Microsoft Open Source Code of Conduct. For
more information see the Code of Conduct FAQ or contact opencode@microsoft
.com with any additional questions or comments.
https://github.com/google/gson
Copyright 2008 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
https://github.com/ReactiveX/RxAndroid
Copyright 2015 The RxAndroid authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
https://github.com/iqiyi/xCrash
xCrash is MIT licensed, as found in the LICENSE file.
xCrash documentation is Creative Commons licensed, as found in the LICENSE-docs file.