Requirements
-
The Android NDK
-
An Android Virtual Device or hardware device
Creating a New dotnet Solution
-
Open your terminal and change the current working directory to the
~/Downloads
directory:
cd ~/Downloads
-
Use the dotnet CLI to create a new Android solution:
dotnet new android -n PSPDFKit-Demo
You can use
dotnet new android -h
to learn more about thedotnet new android
command.
-
Navigate to your newly created .NET
Android
project directory,PSPDFKit-Demo
:
cd ~/Downloads/PSPDFKit-Demo
Installing the PSPDFKit SDK via the dotnet CLI
-
Use the dotnet CLI to add the PSPDFKit NuGet packages to your solution:
dotnet add package PSPDFKit.dotnet.Android
You can use
dotnet add package -h
to learn more about thedotnet add package
command.
-
Open your solution in Visual Studio:
open PSPDFKit-Demo.csproj
Displaying a PDF
-
Open the
AndroidManifest.xml
file in your text editor:
open <project-path>/AndroidManifest.xml
-
Edit the file to add the
PdfActivity
to the<application>
tag:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name="com.pspdfkit.ui.PdfActivity"/> ... </application>
-
Add the PDF document you want to display to your application by dragging it into your solution’s assets. You can download this Quickstart Guide PDF as an example. If you don’t have an Assets folder, create one.
-
Import the following namespaces at the top of your
MainActivity.cs
file:
using PSPDFKit.UI; // To display the PDF. using PSPDFKit.Configuration.Activity; // For `PdfActivityConfiguration`. using PSPDFKit.Configuration.Page; // For activity configuration properties. using Android.Content.Res; // For Assets access. using System.IO; // Path creation. using Java.IO; // File creation.
-
Make sure to call
PSPDFKitGlobal.Initialize
with your license key to initialize PSPDFKit and add it to theOnCreate
method in the generatedMainActivity.cs
code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
+ // Set your `licenseKey` here and initialize PSPDFKit.
+ PSPDFKitGlobal.Initialize (this, licenseKey: null);
// Set your view from the "main" layout resource.
SetContentView(Resource.Layout.activity_main);
}
-
Load your PDF document and display it. This can be done just after the main activity is created in
MainActivity::OnCreate
, when a user clicks a button in a button action handler, or any time during your app’s lifecycle. You also need some code to load the file fromAssets
into your device memory. Add the following functions, which can be accessed from theMainActivity
class:
// Read the contents of your asset. Java.IO.File GetFileFromAssets(string assetName) { AssetManager assets = this.Assets; var bytes = default(byte[]); using (StreamReader reader = new StreamReader(assets.Open(assetName))) { using (var memstream = new MemoryStream()) { reader.BaseStream.CopyTo(memstream); bytes = memstream.ToArray(); } } var tempDir = System.IO.Path.GetTempPath(); var filename = System.IO.Path.Combine(tempDir, assetName); Directory.CreateDirectory(tempDir); using (var fileOutputStream = new FileOutputStream(filename)) { fileOutputStream.Write(bytes); } return new Java.IO.File(filename); } // Display the PDF you added to your assets. void ShowPdfDocument() { var jfile = GetFileFromAssets("Document.pdf"); var docUri = Android.Net.Uri.FromFile(jfile); // The configuration object is optional and allows additional customization. var configuration = new PdfActivityConfiguration.Builder(this) .LayoutMode(PageLayoutMode.Single) .ScrollMode(PageScrollMode.Continuous) .ScrollDirection(PageScrollDirection.Vertical); // Show the PDF document. PdfActivity.ShowDocument(this, docUri, configuration.Build()); }
-
ShowPdfDocument()
can be called at the end of theOnCreate
method in the generatedMainActivity.cs
code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set your `licenseKey` here and initialize PSPDFKit.
PSPDFKitGlobal.Initialize (this, licenseKey: null);
// Set your view from the "main" layout resource.
SetContentView(Resource.Layout.activity_main);
+ ShowPdfDocument();
}
-
Build and run your application.
Next Steps
The PSPDFKit.NET (Android) SDK exposes the APIs from PSPDFKit’s Android SDK to .NET’s C# language. Refer to our guides, as they contain all the information you need to get started with PSPDFKit.
You can find more about the .NET integration in our GitHub project.