Requirements
-
The Android NDK
-
An Android Virtual Device or hardware device
Creating a new Xamarin Solution
-
Open Visual Studio and create a new solution by selecting
File > New Solution...
in the menu:
-
Choose the
Blank App
template for your solution:
-
When prompted, choose your app name (“PSPDFKit-Demo”) and use the default options:
-
Click on the
Next
button and select the location to save the solution:
-
Click on the
Create
button to finish.
Install the PSPDFKit SDK via NuGet
-
Make sure the
PSPDFKit-Demo.sln
is loaded in Visual Studio. -
Right-click on your solution in Visual Studio and select the “Manage NuGet Packages…” menu:
-
In the Browse section of nuget.org, search for PSPDFKit.
-
Select the
PSPDFKit.Android
package.
-
Android also requires the following package. Search for it and tick the box to install:
-
Tap on the
Add Packages
button to add the NuGet packages to your solution.
Displaying a PDF
-
Open the
AndroidManifest.xml
file in your text editor.
open <project-path>/Properties/AndroidManifest.xml
-
Edit the file to add the
PdfActivity
Activity 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.
-
Import the following namespaces at the top of your
MainActivity.cs
:
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.
-
Load your PDF document and display it. This can be done in a button action handler or the
MainActivity::OnCreate
or similar. We also need some code to load the file fromAssets
into our device memory. Add the following functions that can be accessed from theMainActivity
class:
// Read the contents of our 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 we added to our 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);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
+ ShowPdfDocument();
}
-
Build and run your application.
Next Steps
You can find more about Xamarin integration in our GitHub project, and our guides pages.