New to Telerik UI for Xamarin? Download free 30-day trial

Source Exception Handling

In certain cases RadPdfViewer will not be able to load the passed Pdf document - it can be due to invalid stream/inaccessible URL or some invalid data in the document itself. RadPdfViewer provides a way to catch these cases through its SourceException event. In addition, you could show a user-friendly message to the users through SourceExceptionTemplate.

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

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

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

Where the telerikPdfViewer namespace is the following:

xmlns:telerikPdfViewer="clr-namespace:Telerik.XamarinForms.PdfViewer;assembly=Telerik.XamarinForms.PdfViewer"

And here is the SourceException event handler:

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

Check below how the defined SourceTemplateException looks:

PdfViewer SourceException

A sample SourceExceptionTemplate example can be found in the PdfViewer/Features folder of the SDK Samples Browser application.

See Also

In this article