Open a PDF document from application assets in MAUI
Nutrient MAUI SDK 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:
string assetDocumentPath = "subFolder/document.pdf"; // Path to the document in the Assets folder. try { var document = await PSPDFKitController.LoadDocumentFromAssetsAsync( assetDocumentPath, PSPDFKitController.CreateViewerConfiguration()); } catch (Exception ex) { // Handle exception. }
We recommend using this method to load a document, as it’s more optimized than other methods.
Trying it out in Catalog
-
Add a PDF document to the
Resources/Raw/Assets
folder in the Catalog project. By default, the folder contains thedefault.pdf
,demo.pdf
, andPSPDFKit.pdf
files. -
After running the Catalog app, select the Load Document example in the sidebar.
-
Select Application Assets as the document source.
-
In the text box, enter the name of document added to the
Resources/Raw/Assets
folder. For example, choosedemo.pdf
and press Load.