Open a Document in .NET MAUI PDF Viewer

22 Sep 20264 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.

  1. At the document – To indicate that the document is loading.
  2. At the pages – To indicate that the page content is loading.

To customize the appearance of the loading indicators, please refer to this section.

Loading a PDF in code-behind/programmatically

In code-behind a PDF can be loaded to SfPdfViewer just by using the LoadDocument method. Use the following code snippet in the code-behind of the XAML page.

//Load the document tp the PDF viewer.
Stream pdfDocumentStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("GettingStarted.Assets.PDF_Succinctly.pdf");
PdfViewer.LoadDocument(pdfDocumentStream);

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();

Opening a unsupported annotations

The .NET MAUI PDF Viewer it is possible to view the unsupported annotations like 3D, rich media and sound 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);

Check other PDF opening options

See Also