Getting started on PSPDFKit.NET for Android

Requirements

Creating a New dotnet Solution

  1. Open your terminal and change the current working directory to the ~/Downloads directory:

cd ~/Downloads
  1. Use the dotnet CLI to create a new Android solution:

dotnet new android -n PSPDFKit-Demo
Information

You can use dotnet new android -h to learn more about the dotnet new android command.

  1. Navigate to your newly created .NET Android project directory, PSPDFKit-Demo:

cd ~/Downloads/PSPDFKit-Demo

Installing the PSPDFKit SDK via the dotnet CLI

  1. Use the dotnet CLI to add the PSPDFKit NuGet packages to your solution:

dotnet add package PSPDFKit.dotnet.Android
Information

You can use dotnet add package -h to learn more about the dotnet add package command.

  1. Open your solution in Visual Studio:

open PSPDFKit-Demo.csproj

Displaying a PDF

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

open <project-path>/AndroidManifest.xml
  1. 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>
  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. If you don’t have an Assets folder, create one.

drag-and-drop-document

  1. 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.
  1. Make sure to call PSPDFKitGlobal.Initialize with your license key to initialize PSPDFKit and add it to the OnCreate method in the generated MainActivity.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);
}
  1. 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 from Assets into your device memory. Add the following functions, which can be accessed from the MainActivity 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());
}
  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);

	// 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();
}
  1. 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.

Requirements

Installing the PSPDFKit 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.dotnet.

  2. Select the PSPDFKit.dotnet.Android package.

add-pspdfkit-nuget-packages

  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>/AndroidManifest.xml
  1. 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>
  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. If you don’t have an Assets folder, create one.

drag-and-drop-document

  1. 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.
  1. Make sure to call PSPDFKitGlobal.Initialize with your license key to initialize PSPDFKit and add it to the OnCreate method in the generated MainActivity.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);
}
  1. 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 from Assets into your device memory. Add the following functions, which can be accessed from the MainActivity 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());
}
  1. 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.

Requirements

Cloning the Sample Project

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

cd ~/Downloads
  1. Clone the PSPDFKit.NET (Android) repository:

git clone https://github.com/PSPDFKit/dotnet-pdf-library-for-android.git
  1. Change the current working directory to the root directory:

cd dotnet-pdf-library-for-android
  1. Run the following command to build and download PSPDFKit and its dependencies:

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

open samples/PSPDFKit.dotnet.Android.Samples.sln
  1. Build and run AndroidSample.csproj on an Android emulator.

catalog-emulator

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.