Open a PDF Document from Application Assets in MAUI

PSPDFKit for MAUI supports opening a document from a number of sources, including application assets. A document is considered an application asset if it’s marked in its properties as MauiAsset.

Introduction

To open a document, you need to add PDFView to your desired XAML. You’ll also need to assign a Name for interacting with it through code. In this example, PDFView is named PDFView:

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

The rest of the document opening process needs to be done after the PDFView control is loaded. The easiest way to ensure it’s loaded is by using the PDFView.Initialized event as shown above. Alternatively, you can subscribe to it in code-behind as follows:

PDFView.Initialized += (sender, e) =>
{
    // ...
};

If you don’t intend to open a document straight away and you know that PDFView will always be initialized, you can skip this step and open the document whenever it’s convenient.

Opening a Document from Application Assets

A document can be loaded using this method if it’s a raw asset located in a folder named Assets. The following example code demonstrates how to load a document from your application assets:

try
{
    string assetDocumentPath = "subFolder/document.pdf"; // Path to the document in the Assets folder.
    await PSPDFKitController.LoadDocumentFromAssetsAsync(assetDocumentPath);
}
catch (Exception ex)
{
    // Handle exception.
}
Information

We recommend using this method to load a document, as it’s more optimized than other methods.

Trying It Out in Catalog

  1. Add a PDF document to the Resources/Raw/Assets folder in the Catalog project. By default, the folder contains the default.pdf, demo.pdf, and PSPDFKit.pdf files.

  2. After running the Catalog app, select the Load Document example in the sidebar.

  3. Select Application Assets as the document source.

  4. In the text box, enter the name of document added to the Resources/Raw/Assets folder. For example, choose demo.pdf and press Load.