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

**Jump to example**

Prefer to jump straight to code? View the example repo on GitHub.

[Read more](https://github.com/PSPDFKit/pspdfkit-maui-catalog)

## Requirements

- For developing on Windows, you need Visual Studio 2022 17.3 or greater. For more information, refer to the guide for [prerequisites for MAUI installation on Windows](https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation?tabs=vswin#prerequisites).
  <!-- list separator -->

- For developing on a Mac, you need Visual Studio Code. For more information, refer to the guide for [prerequisites for MAUI installation on Mac](https://learn.microsoft.com/en-us/dotnet/maui/get-started/installation?view=net-maui-10.0&tabs=visual-studio-code#prerequisites-1).

## Creating a new project

If you already have an existing MAUI project, skip to the step for [adding the Nutrient dependency to your project](#adding-the-nutrient-dependency-to-your-project). Otherwise, create a new one by following the steps below.

### Visual Studio

**Steps:**

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

2. In the new project wizard, select **.NET MAUI App** and click **Next**.

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

4. Specify the target framework. This example uses the default.

### Command line

**Steps:**

1. Open a terminal and run the following command to create a new MAUI project:

   ```bash

   dotnet new maui -n "Nutrient Demo"
   ```

2. Navigate to the project directory:

   ```bash

   cd "Nutrient Demo"
   ```

## Adding the Nutrient dependency to your project

### Visual Studio

**Steps:**

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

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 your project and click the **Manage NuGet Packages...** menu item. This will open the NuGet Package Manager for your solution.

4. Search for `Nutrient.MAUI.SDK`, and you’ll find the package on [nuget.org](https://www.nuget.org/packages/Nutrient.MAUI.SDK).

5. On the right side, in the panel describing the package, click **Install** to install the 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:

   ```csharp <!-- Lines inserted: [6] -->

   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();
      }
   ```

### Command line

**Steps:**

1. Open your project file (`Nutrient Demo.csproj`) and change the first line from `<Project Sdk="Microsoft.NET.Sdk">` to `<Project Sdk="Microsoft.NET.Sdk.Razor">`.

2. Add the Nutrient NuGet package to your project:

   ```bash

   dotnet add package Nutrient.MAUI.SDK
   ```

3. Open `MauiProgram.cs` and call the `RegisterPSPDFKitSdk()` method on the builder. The resulting `CreateMauiApp` function will look similar to the one shown below:

   ```csharp <!-- Lines inserted: [6] -->

   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

### Visual Studio

**Steps:**

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`](https://www.nutrient.io/guides/maui/open-a-document/from-app-assets/).

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

   ```xml

   <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>`:

   ```xml

   xmlns:pspdfkit="clr-namespace:PSPDFKit.Sdk;assembly=Sdk"
   ```

4. If you’ve purchased a license and [assigned it to a bundle ID](https://www.nutrient.io/guides/maui/troubleshooting/what-is-a-bundle-id.md), you can add it to the correct platform here:

   ```xml

   <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:

   ```xml

   <pspdfkit:PDFView x:Name="PDFView" Initialized="OnPDFViewInitialized" />
   ```

   ```csharp

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

### Command line

**Steps:**

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`](https://www.nutrient.io/guides/maui/open-a-document/from-app-assets/).

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

   ```xml

   <pspdfkit:PDFView x:Name="PDFView" />
   ```

3. Add the following namespace to the `<ContentPage>` element:

   ```xml

   xmlns:pspdfkit="clr-namespace:PSPDFKit.Sdk;assembly=Sdk"
   ```

4. If you’ve purchased a license and [assigned it to a bundle ID](https://www.nutrient.io/guides/maui/troubleshooting/what-is-a-bundle-id.md), you can add it to the correct platform here:

   ```xml

   <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:

   ```xml

   <pspdfkit:PDFView x:Name="PDFView" Initialized="OnPDFViewInitialized" />
   ```

   ```csharp

   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. Build the project:

   ```bash

   dotnet build
   ```

7. Run the application on your target platform:

   ```bash

   dotnet build -t:Run -f net10.0-android   # For Android

   dotnet build -t:Run -f net10.0-ios       # For iOS

   dotnet build -t:Run -f net10.0-maccatalyst # For macOS

   dotnet build -t:Run -f net10.0-windows10.0.19041.0 # For Windows

   ```

8. You’ll see `demo.pdf` loaded in `PDFView`.

## Next steps

- [Add AI capabilities to Nutrient document viewer](https://www.nutrient.io/guides/maui/ai-assistant.md)

- [Customize the UI and other SDK functionality](https://www.nutrient.io/guides/maui/user-interface.md)

- [Convert Microsoft office documents to PDF](https://www.nutrient.io/guides/maui/conversion/office-to-pdf.md)

- [Add annotations to your document](https://www.nutrient.io/guides/maui/annotations.md)