Getting Started on Xamarin.Android

Warning

Nutrient Xamarin SDK (formerly PSPDFKit Xamarin for iOS and PSPDFKit Xamarin for Android) has been deprecated and replaced by Nutrient .NET for iOS and Nutrient .NET for Android. All Nutrient Xamarin licenses will remain valid and fully supported until 1 December 2025, or until your license expires, whichever occurs later. Interested in transitioning to another Nutrient product? Contact us and we’ll be happy to help.

Requirements

Creating a new Xamarin solution

  1. Open Visual Studio and create a new solution by selecting File > New Solution… in the menu:

create-new-solution

  1. Choose the Blank App template for your solution:

app-template

  1. When prompted, choose your app name (“PSPDFKit-Demo”) and use the default options:

configure-ios-app

  1. Click the Next button and select the location to save the solution:

create-app

  1. Click the Create button to finish.

Install the Nutrient SDK via NuGet

  1. Make sure the PSPDFKit-Demo.sln is loaded in Visual Studio.

  2. Right-click your solution in Visual Studio and select the Manage NuGet Packages… menu:

manage-nuget-packages

  1. In the Browse section of nuget.org, search for PSPDFKit.

  2. Select the PSPDFKit.Android package.

add-pspdfkit-nuget-packages

  1. Android also requires the following package. Search for it and tick the box to install:

  1. Tap the Add Packages button to add the NuGet packages to your solution.

Displaying a PDF

  1. Open the AndroidManifest.xml file in your text editor.

open <project-path>/Properties/AndroidManifest.xml
  1. 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>
  1. 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.

drag-and-drop-document

  1. 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.
  1. Load your PDF document and display it. This can be done in a button action handler or the MainActivity::OnCreate or similar. You also need some code to load the file from Assets into your device memory. Add the following functions that can be accessed from the MainActivity 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());
}
  1. ShowPdfDocument() can be called at the end of the OnCreate method in the generated MainActivity.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();
}
  1. Build and run your application.

Next steps

You can find more about Xamarin integration in our GitHub project, and our guides.

Warning

Nutrient Xamarin SDK (formerly PSPDFKit Xamarin for iOS and PSPDFKit Xamarin for Android) has been deprecated and replaced by Nutrient .NET for iOS and Nutrient .NET for Android. All Nutrient Xamarin licenses will remain valid and fully supported until 1 December 2025, or until your license expires, whichever occurs later. Interested in transitioning to another Nutrient product? Contact us and we’ll be happy to help.

Requirements

Install the Nutrient SDK via NuGet

  1. Open your solution in Visual Studio:

open path/to/YourSolution.sln
  1. Right-click your solution in Visual Studio and select the Manage NuGet Packages… menu:

manage-nuget-packages

  1. In the Browse section of nuget.org, search for PSPDFKit.

  2. Select the PSPDFKit.Android package.

add-pspdfkit-nuget-packages

  1. Android also requires the following package. Search for it and tick the box to install:

  1. Tap the Add Packages button to add the NuGet packages to your solution.

Displaying a PDF

  1. Open the AndroidManifest.xml file in your text editor.

open <project-path>/Properties/AndroidManifest.xml
  1. 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>
  1. 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.

drag-and-drop-document

  1. 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.
  1. Load your PDF document and display it. This can be done in a button action handler or the MainActivity::OnCreate or similar. You also need some code to load the file from Assets into your device memory. Add the following functions that can be accessed from the MainActivity 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());
}
  1. Build and run your application.

Next steps

You can find more about Xamarin integration in our GitHub project, and our guides.

Warning

Nutrient Xamarin SDK (formerly PSPDFKit Xamarin for iOS and PSPDFKit Xamarin for Android) has been deprecated and replaced by Nutrient .NET for iOS and Nutrient .NET for Android. All Nutrient Xamarin licenses will remain valid and fully supported until 1 December 2025, or until your license expires, whichever occurs later. Interested in transitioning to another Nutrient product? Contact us and we’ll be happy to help.

Requirements

Cloning the Catalog sample project

  1. Open the Terminal app and change the current working directory. In this case, use the ~/Downloads directory:

cd ~/Downloads
  1. Clone the Nutrient Xamarin for Android repository:

git clone https://github.com/PSPDFKit/Xamarin-Android.git
  1. Change the current working directory to the root directory:

cd Xamarin-Android
  1. Download the Nutrient Android .aar file.

  2. Copy or move the file you’ve just downloaded to the Jars directory and rename it to pspdfkit-6.6.0.aar:

mv ~/Downloads/PSPDFKit-for-Android-AAR-6.6.0.aar PSPDFKit.Android/Jars/pspdfkit-6.6.0.aar
  1. Run the following command to download the remaining Nutrient dependencies:

./build.sh
  1. Open the Nutrient solution (PSPDFKit.Android.sln) in Visual Studio:

open PSPDFKit.Android.sln
  1. Build and run the PSPDFCatalog on an Android emulator.

catalog-emulator

Next steps

You can find more about Xamarin integration in our GitHub project, and our guides.