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).

Requirements

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.

  1. Use the dotnet CLI(opens in a new tab) to create a new Android solution:

    Terminal window
    dotnet new android -n Nutrient-Demo

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

  2. 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.

Terminal window
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

  1. Open your solution in Visual Studio.

    Terminal window
    open path/to/YourSolution.sln
  2. Right-click your solution in Visual Studio and select the Manage NuGet Packages….

    Add nuget package using NuGet manager
  3. In the Browse section of nuget.org, search for Nutrient.dotnet.

  4. Select the Nutrient.dotnet.Android package.

    Add nuget package using NuGet manager
  5. Tap the Add Packages button to add the NuGet packages to your solution.

Displaying document in your app

  1. Open your solution/csproj in Visual Studio if not already open.

    Terminal window
    open path/to/YourSolution.sln
  2. Open the AndroidManifest.xml file.

  3. 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>
  4. 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.

    Drag and drop document to display in Visual Studio
  5. 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.
  6. Make sure to call NutrientGlobal.Initialize with your license key to initialize Nutrient 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 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);
    }
  7. 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.
    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);
    }
  8. 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 Nutrient.
    NutrientGlobal.Initialize (this, licenseKey: null);
    // Set your view from the "main" layout resource.
    SetContentView(Resource.Layout.activity_main);
    ShowPdfDocument();
    }
  9. 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.