Skip to main content

Load Document

  • 2 minutes to read

This help topic describes how to load PDF files from different sources (file path, stream, or file URI) and specify document display settings.

Load a PDF File

Initialize the DocumentSource property with a UriPdfDocumentSource or StreamPdfDocumentSource object to load a PDF file by its URI or from a stream.

You can load a PDF file directly by its name if it is located in the Resources\Raw folder. Since the DocumentSource property is a content property, you can skip DocumentSource tags in your markup:

<dx:PdfViewer x:Name="pdfViewer">
    <dx:UriPdfDocumentSource Uri="TestDoc.pdf"/>
</dx:PdfViewer>

The PdfViewer control can convert a UriPdfDocumentSource object from a string path. This means you can specify the DocumentSource property as follows:

<dx:PdfViewer x:Name="pdfViewer1" DocumentSource="TestDoc.pdf"/>
<dx:PdfViewer x:Name="pdfViewer2" DocumentSource="https://dxtestsite.com/testdoc.pdf"/>

Instead of creating a UriPdfDocumentSource object, you can directly assign a Uri object to the DocumentSource property:

pdfViewer.DocumentSource = new Uri("TestDoc.pdf", UriKind.RelativeOrAbsolute);

You can also call the following methods to load PDF files from different sources:

View Example

pdfViewer.DocumentSource = PdfDocumentSource.FromFile("TestDoc.pdf");

Call the SetDocumentSourceAsync method to load a PDF document to the viewer asynchronously:

public MainPage() {
    InitializeComponent();
    Init();
}

async void Init() {
    await pdfViewer.SetDocumentSourceAsync("TestDoc.pdf");
}

Once the document is successfully loaded, the PDF viewer raises the DocumentLoaded event.

The CurrentPageNumber property allows you to navigate to a specific page. To obtain the total number of document pages, use the PageCount property.

To close the current document, call the PdfViewer.CloseDocument method or use the PdfViewerCommands.CloseDocument command.

Known Issues

A PDF document that contains JPX images may require significant resources to be processed and decoded, which can lead to performance degradation.

Invoke File Explorer

Call the PdfViewer.ShowOpenFileDialogAsync method or bind to the PdfViewerCommands.ShowOpenFileDialog command to invoke the file explorer (platform dependent) to select a PDF file.

<Grid RowDefinitions="40,*">
    <dx:DXToolbar Grid.Row="0">
        <dx:ToolbarButton Icon="open" Command="{Binding Commands.ShowOpenFileDialog, Source={x:Reference pdfViewer}}"/>
    </dx:DXToolbar>
    <dx:PdfViewer x:Name="pdfViewer" Grid.Row="1" ShowToolbar="False"/>
</Grid>

Open Protected Files

The PDF viewer shows the following dialog message that suggests to enter the password to open a protected PDF file:

DevExpress PDF viewer for .NET MAUI - Protected file

If you want to specify a protected document password without using the built-in input form, handle the PdfViewer.PasswordRequested event and set the Password property in the event handler.