Open a PDF from Local Storage in MAUI

PSPDFKit for MAUI supports opening a document from a number of sources, including local storage.

Note that there’s a platform-specific setup required for FilePicker. It’s worth reading about the setup here if you’re not familiar with it.

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 Local Storage

The following example code demonstrates how to show the file picker to let the user select a document to open:

FileResult result = null;

// Ensure that the file picker is shown on the UI thread.
await Application.Current!.Dispatcher.DispatchAsync(
    async () =>
    {
        result = await FilePicker.Default.PickAsync();
    });

if (result == null)
{
    return;
}

try
{
    await PSPDFKitController.LoadDocumentAsync(result.FullPath);
}
catch (Exception ex)
{
    // Handle exception.
}