How can I help you?
Open a Document in .NET MAUI PDF Viewer (SfPdfViewer)
25 Mar 20265 minutes to read
The SfPdfViewer allows you to open PDF documents from various sources, such as local storage or URLs. It also supports viewing password-protected documents.
This section walks you through loading and unloading documents in the SfPdfViewer and handling load-specific events.
In This Section
| Topic | Description |
|---|---|
| Open a Document | Load from a stream or byte array, unload documents, and handle loading events. |
| Open from a URL | Download and display a PDF directly from a remote URL. |
| Open from Local Storage | Open a PDF from the device’s file system using the file picker. |
| Open from Base64 | Load a PDF encoded as a Base64 string. |
| Open a Password-Protected Document | Display a password dialog and open encrypted PDF documents. |
| Document Load Notifications | Subscribe to document and page load/unload events for custom logic. |
Document and page loading indications
The SfPdfViewer displays a built-in ActivityIndicator (loading indicator) under the following scenarios to indicate if there are any lengthy loadings.
-
At the document– To indicate that the document is loading. -
At the pages– To indicate that the page content is loading.
To customize the appearance of the loading indicators, please refer to this section.
Document source types
The document source types accepted by the SfPdfViewer are Stream and byte[]. You can load the PDF document from a specified stream or bytes.
Load the document from the stream
// Load the document from Stream
Stream pdfDocumentStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.PDF_Succinctly.pdf");
PdfViewer.DocumentSource = pdfDocumentStream;Load the document from Byte array
// Load the document from a Byte array
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("https://www.syncfusion.com/downloads/support/directtrac/general/pd/PDF_Succinctly1928776572");
PdfViewer.DocumentSource = await response.Content.ReadAsByteArrayAsync();Unload a document
The SfPdfViewer allows you to unload and clear the resources occupied by the PDF document loaded using the UnloadDocument method, as shown below.
NOTE
- While changing or opening different documents on the same page, the previously loaded document will be unloaded automatically by the SfPdfViewer.
- And, if you are using multiple pages in your application, then make sure to unload the document from the SfPdfViewer while leaving the page that has it to release the memory and resources consumed by the PDF document that is loaded. The unloading of documents can be done by calling the UnloadDocument method.
//Unload the document from the PDF viewer.
PdfViewer.UnloadDocument();Opening a PDF document with annotations
The .NET MAUI PDF Viewer does not currently support annotations comparable to Xamarin.Forms. However, it is possible to view the unsupported annotations in a non-interactive manner. To achieve this, provide the flattenOptions (an optional parameter) as Unsupported in the LoadDocument methods. See the following code example:
Stream pdfDocumentStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.PDF_Succinctly.pdf");
FlattenOptions flattenOption = FlattenOptions.Unsupported;
// Loads the PDF document from the stream with the flatten option to render unsupported annotations.
PdfViewer.LoadDocument(pdfDocumentStream, flattenOptions: flattenOption);NOTE
- All the LoadDocument methods accept the flatten options parameter.
- Refer to this section for the upcoming annotation features in the SfPdfViewer.
Optimizing document loading on Android
When your application handles large images, complex graphics, or memory-intensive operations, the default heap size may not be sufficient, leading to performance issues or crashes. Enabling a larger heap allows the app to allocate more memory, ensuring smooth performance and preventing out-of-memory errors in such scenarios. You can enable this by adding the following highlighted attribute in your AndroidManifest.xml under the
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round"
android:largeHeap="true" ></application>
https://help.syncfusion.com/document-processing/pdf/pdf-viewer/maui.
</manifest>