Add PDF functionality with MAUI

This guide walks you through the steps necessary to integrate Nutrient MAUI SDK into your desktop or mobile project. By the end, you’ll be able to present a PDF document in the Nutrient user interface (UI).

Requirements

Creating a new project

If you already have an existing MAUI project, skip to the Adding the Nutrient dependency to your project step. Otherwise, create a new one by following the steps below:

  1. Open Visual Studio and select Create a new project.

    New project wizard in Visual Studio
  2. In the new project wizard, select .NET MAUI App and click Next.

    Project template to select
  3. Configure your project by setting the name and location, as shown in the image below.

    Project config to select
  4. Specify the target framework. This example uses the default.

    Select the target framework

Adding the Nutrient dependency to your project

  1. Double-click the project name to access the project file. It’ll look similar to the image below.

    New project content
  2. Change the first line from <Project Sdk="Microsoft.NET.Sdk"> to <Project Sdk="Microsoft.NET.Sdk.Razor"> and save the project file.

  3. Open your app’s solution, and in the Solution Explorer, right-click on your project and click the Manage NuGet Packages... menu item. This will open the NuGet Package Manager for your solution.

    NuGet manager option
  4. Search for Nutrient.MAUI.SDK, and you’ll find the package on nuget.org(opens in a new tab).

  5. On the right side, in the panel describing the package, click Install to install the package.

    Nutrient NuGet package
  6. Once the package is installed, click MauiProgram.cs and call the RegisterPSPDFKitSdk() method on the builder. The resulting CreateMauiApp function will look similar to the one shown below:

    public static MauiApp CreateMauiApp()
    {
    var builder = MauiApp.CreateBuilder();
    builder
    .UseMauiApp<App>()
    .RegisterPSPDFKitSdk() // added
    .ConfigureFonts(fonts =>
    {
    fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
    fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
    });
    #if DEBUG
    builder.Logging.AddDebug();
    #endif
    return builder.Build();
    }

Displaying a PDF

  1. In the Resources/Raw folder, create an Assets folder and add a PDF file named demo.pdf.

    This is a special folder and it has to be named Assets. PDFs located in this folder can be loaded in the application using a specially designed API, LoadDocumentFromAssetsAsync.

  2. Open MainPage.xaml, remove all the content of ContentPage, and paste the following XML as its content:

    <pspdfkit:PDFView x:Name="PDFView" />
  3. You’ll see a green line under pspdfkit:PDFView. If you hover over it, you’ll be given the option for potential fixes. Apply the suggested fix, which will add the following namespace to <ContentPage>:

    xmlns:pspdfkit="clr-namespace:PSPDFKit.Sdk;assembly=Sdk"
  4. If you’ve purchased a license and assigned it to a bundle ID, you can add it to the correct platform here:

    <pspdfkit:PDFView x:Name="PDFView"
    License="{OnPlatform
    Android={StaticResource AndroidLicenseKey},
    iOS={StaticResource iOSLicenseKey},
    MacCatalyst={StaticResource MacCatalystLicenseKey},
    WinUI={StaticResource WindowsLicenseKey}}" />

    If not, omitting the License property will initialize the PDFView in trial mode.

  5. To subscribe to the Initialized event of PDFView, add the following code to the MainPage.xaml and MainPage.xaml.cs files. In this event handler, use the PDFView.Controller.LoadDocumentFromAssetsAsync function to load the demo.pdf file that was added to the Assets folder:

    MainPage.xaml
    <pspdfkit:PDFView x:Name="PDFView" Initialized="OnPDFViewInitialized" />
    MainPage.xaml.cs
    private async void OnPDFViewInitialized(object sender, EventArgs e)
    {
    try
    {
    var configuration = PDFView.Controller.CreateViewerConfiguration();
    await PDFView.Controller.LoadDocumentFromAssetsAsync("demo.pdf", configuration);
    }
    catch (Exception ex)
    {
    // Handle exception.
    }
    }

    You must wait for initialization of the PDFView to be complete before using PdfView.Controller.

  6. In the Build toolbar, choose Debug, and select the platform you’d like to build on — for example, x86 or x64.

  7. In the menu, select Build -> Build Solution.

  8. Start the application, and you’ll see demo.pdf loaded in PDFView.

Next steps