Add PDF functionality on Android using .NET
This guide explains how to integrate Nutrient .NET Mobile SDK for Android into a new or existing .NET project. By the end, you’ll be able to display a PDF document using the default Nutrient user interface (UI).
View the example repo on GitHub.
View the example repo on GitHub.
Requirements
- Latest stable version of Visual Studio(opens in a new tab)
- Latest stable version of the .NET SDK(opens in a new tab)
- Latest stable version of the
android
workload(opens in a new tab) - Latest stable version of Microsoft Build of OpenJDK(opens in a new tab)
- Latest stable version of Android Studio(opens in a new tab)
- The Android Native Development Kit (NDK)(opens in a new tab)
- An Android Virtual Device(opens in a new tab) or hardware device
Creating a new project
If you already have an existing .NET for Android or MAUI project that runs on Android using the latest version of the android
or maui
workloads, jump to the Adding Nutrient to your project section.
Use the dotnet CLI(opens in a new tab) to create a new Android solution:
Terminal window dotnet new android -n Nutrient-DemoYou can use
dotnet new android -h
to learn more about the dotnet new android command.Navigate to your newly created .NET
Android
project directory,Nutrient-Demo
:Terminal window cd ./Nutrient-Demo
Adding Nutrient to your project
Using .NET CLI
Use the dotnet CLI(opens in a new tab) to add the Nutrient NuGet packages to your solution.
dotnet add package Nutrient.dotnet.Android
You can use dotnet add package -h
to learn more about the dotnet add package command.
Using Visual Studio
Open your solution in Visual Studio.
Terminal window open path/to/YourSolution.slnRight-click your solution in Visual Studio and select the Manage NuGet Packages….
In the Browse section of nuget.org, search for
Nutrient.dotnet
.Select the
Nutrient.dotnet.Android
package.Tap the Add Packages button to add the NuGet packages to your solution.
Displaying document in your app
Open your solution/csproj in Visual Studio if not already open.
Terminal window open path/to/YourSolution.slnOpen the
AndroidManifest.xml
file.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 our sample 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
NutrientGlobal.Initialize
with your license key to initialize Nutrient 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 Nutrient.// If you are using our free trial, set the licenseKey to null.NutrientGlobal.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.Android.Net.Uri GetFileFromAssets(Context ctx, string assetName){var docPath = Path.Combine (ctx.GetExternalFilesDir (null)!.AbsolutePath, assetName);if (!File.Exists (docPath)) {using (var br = new BinaryReader (ctx.Assets!.Open (assetName))) {using (var bw = new BinaryWriter (new FileStream (docPath, FileMode.Create))) {var buffer = new byte[2048];var len = 0;while ((len = br.Read (buffer, 0, buffer.Length)) > 0) {bw.Write (buffer, 0, len);}}}}var file = new Java.IO.File (docPath);return Android.Net.Uri.FromFile (file)!;}// Display the PDF you added to your assets.void ShowPdfDocument(){var docUri = GetFileFromAssets("Document.pdf");var nutrientConfiguration = new PdfActivityConfiguration.Builder(ApplicationContext).ScrollDirection(PageScrollDirection.Horizontal!).PageLabelsEnabled(true).ThumbnailGridEnabled(true).FitMode(PageFitMode.FitToWidth!).Build();if (!NutrientGlobal.IsOpenableUri(this, docUri)){ShowError("This document uri cannot be opened \n " + docUri.ToString());return;}PdfActivity.ShowDocument(this, docUri, nutrientConfiguration);}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 Nutrient.NutrientGlobal.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 Nutrient .NET Mobile SDK for Android exposes the APIs from Nutrient Android SDK to .NET’s C# language. Refer to our guides, as they contain all the information you need to get started with Nutrient.