New to Telerik UI for .NET MAUI? Start a free 30-day trial

Events

The Telerik UI for .NET MAUI PDF Viewer provides various events that allow you to control the behavior of the component.

Page Elements Loaded

The PDF Viewer raises the PageElementsLoaded event when all elements on the page are loaded. Use this event to alter the page content before it is rendered. This event is raised on a background thread.

The PageElementsLoaded event handler receives two parameters:

  • sender—The PDF Viewer control.
  • PageElementsLoadedEventArgs—The Object which has a reference to the Page(RadFixedPage).

The following example demonstrates how to use the PageElementsLoaded event:

<Grid RowDefinitions="{OnIdiom Desktop='Auto, *', Phone='*, Auto'}">
    <telerik:RadPdfViewerToolbar Grid.Row="{OnIdiom Desktop=0, Phone=1}"
                                 PdfViewer="{x:Reference pdfViewer}">
        <telerik:PdfViewerFitToWidthToolbarItem />
        <telerik:PdfViewerZoomInToolbarItem />
        <telerik:PdfViewerZoomOutToolbarItem />
        <telerik:PdfViewerNavigateToPreviousPageToolbarItem />
        <telerik:PdfViewerNavigateToNextPageToolbarItem />
    </telerik:RadPdfViewerToolbar>
    <telerik:RadPdfViewer x:Name="pdfViewer"
                          Grid.Row="{OnIdiom Desktop=1, Phone=0}" />
</Grid>

The next example shows how to add the event handler:

private void OnPageElementsLoaded(object sender, PageElementsLoadedEventArgs e)
{
    foreach (var item in e.Page.Content)
    {
        if (item is Telerik.Windows.Documents.Fixed.Model.Graphics.Path path)
        {
            if (path.StrokeThickness == 0)
            {
                path.StrokeThickness = 5;
            }
        }
    }
}

Handling Exceptions in the Source

In some cases, the PDF Viewer will fail to load the passed PDF document. The reason can be an invalid stream/inaccessible URL or invalid data in the document itself. To handle these corner cases, use the SourceException event of the PDF Viewer. In addition, you can show a user-friendly message to the users through SourceExceptionTemplate.

The example below shows how to use the RadPdfViewer API for handling source exceptions.

1,. Define the RadPdfViewer control and apply a SourceExceptionTemplate. The snippet below demonstrates a sample template with only one Label which holds the message.

<telerik:RadPdfViewer x:Name="pdfViewer" SourceException="PdfViewerSourceException">
    <telerik:RadPdfViewer.SourceExceptionTemplate>
        <DataTemplate>
            <Label Text="Sorry, something with loading the pdf document went wrong." 
                   TextColor="#404040"
                   HorizontalTextAlignment="Center"
                   VerticalTextAlignment="Center"
                   LineBreakMode="WordWrap" />
        </DataTemplate>
    </telerik:RadPdfViewer.SourceExceptionTemplate>
</telerik:RadPdfViewer>

2. Add the telerik namespace:

xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"

3. And the SourceException event handler:

private void PdfViewerSourceException(object sender, SourceExceptionEventArgs e)
{
    var error = e.Exception.Message;
}

For a runnable PDF Viewer example with the Source Exception event, see the SDKBrowser Demo Application and go to PdfViewer > Features.

See Also

In this article