How to Perform a Task When PdfView is Loaded

Sometimes you might want to perform a task after the SDK is loaded. This can be easily done with PdfView events.

In PdfView, there is an event, InitializationCompletedHandler, which is fired when the PdfView is ready to receive commands. You can subscribe to this event, and afterward, you can perform any task you need:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public OpenFileProgramatically()
{
    InitializeComponent();
    PDFView.InitializationCompletedHandler += PdfView_InitializationCompletedHandler;
}
private async void PdfView_InitializationCompletedHandler(PdfView sender, Document document)
{
    try
    {
        var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/pdfs/default.pdf"));
        if (file == null) return;
        await sender.OpenStorageFileAsync(file);
    }
    catch (Exception e)
    {
        var messageDialog = new MessageDialog(e.Message);
        await messageDialog.ShowAsync();
    }
}

The example above shows how to change the document shown in the PdfView when PdfView is loaded.