Getting Started on Xamarin.Android

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 on the Next button and select the location to save the solution:

create-app

  1. Click on the Create button to finish.

Install the PSPDFKit SDK via NuGet

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

  2. Right-click on 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 on 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. We also need some code to load the file from Assets into our 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 pages.

Requirements

Install the PSPDFKit SDK via NuGet

  1. Open your solution in Visual Studio:

open path/to/YourSolution.sln
  1. Right-click on 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 on 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. We also need some code to load the file from Assets into our 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 pages.

Requirements

Cloning the Catalog Sample Project

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

cd ~/Downloads
  1. Clone the PSPDFKit for Xamarin 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 PSPDFKit 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 PSPDFKit dependencies:

./build.sh
  1. Open the PSPDFKit 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 pages.