Open a PDF from Base64 Data in MAUI

PSPDFKit for MAUI supports opening a document from a number of formats, including Base64 strings.

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 a Base64 String

The following example code demonstrates how to open a document from a Base64 string:

try
{
    await PSPDFKitController.LoadDocumentFromBase64StringAsync(documentAsBase64String);
}
catch (Exception ex)
{
    // Handle exception.
}

Trying It Out in Catalog

If you don’t have a Base64 string handy and you want to quickly try out loading a document from a Base64 string, you can use this site to convert a document to a Base64 string and then try loading a document in our Catalog app.