Available for: UI for ASP.NET Core | UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Silverlight | UI for Xamarin

Handling Exceptions

Since R2 2020 RadPdfProcessing has and exception handling mechanism. It allows to intercept and handle exceptions when the document is imported or loaded. This functionality introduces two events.

  • PdfImportSettings.DocumentUnhandledException: The event is fired when an exception occurs during document import. If the ReadingMode is set to AllAtOnce the entire document will be loaded on document import and there is no need to use the other event.
  • RadFixedDocument.DocumentUnhandledException: The event is fired when an exception occurs while loading the document pages. This event can be fired when the document is imported with OnDemand ReadingMode and a particular page is loaded after the import.

When both events are raised, the DocumentUnhandledExceptionEventArgs argument is passed. This argument contains two properties:

  • Exception: Gets the document exception.
  • Handled: Gets or sets if the exception should be handled. The default value is false.

The exception handling mechanism handles exceptions at the very beginning of the import as well. In such a case, the event will be raised and an empty document instance is returned. The exception handling mechanism does not handle exceptions while parsing fonts glyph data or parsing images during document rendering in the UI viewers.

Using ImportSettings.DocumentUnhandledException

To use this functionality you should handle the PdfImportSettings.DocumentUnhandledException event. The Handled property in the event arguments indicates if the exception is handled by the code in the event handler or the exception should be thrown.

Example 1: Using the OnException event while loading the entire document

public RadFixedDocument ImportDocument() 
{ 
    PdfFormatProvider provider = new PdfFormatProvider(); 
    provider.ImportSettings.ReadingMode = ReadingMode.AllAtOnce; 
    provider.ImportSettings.DocumentUnhandledException += ImportSettings_DocumentUnhandledException; 
    var document = provider.Import(File.ReadAllBytes("SampleDoc.pdf")); 
    return document; 
} 
 
private void ImportSettings_DocumentUnhandledException(object sender, DocumentUnhandledExceptionEventArgs e) 
{ 
    MessageBox.Show("The document is corupted and cannot be loaded: " + e.Exception.Message); 
    e.Handled = true; 
} 

Using RadFixedDocument.DocumentUnhandledException

When using the OnDemand reading mode you should handle the RadFixedDocument.DocumentUnhandledException event. The Handled option in the event arguments indicates if the exception is handled by the code in the event handler or the exception should be thrown.

Example 2: Using the OnException event while loading on demand

public void LoadDocument() 
{ 
    PdfFormatProvider provider = new PdfFormatProvider(); 
    provider.ImportSettings.ReadingMode = ReadingMode.OnDemand; 
    var document = provider.Import(File.ReadAllBytes("SampleDoc.pdf")); 
    document.DocumentUnhandledException += Document_DocumentUnhandledException; 
 
} 
 
private void Document_DocumentUnhandledException(object sender, DocumentUnhandledExceptionEventArgs e) 
{ 
    MessageBox.Show("The document is corupted and some pages cannot be loaded: " + e.Exception.Message); 
    e.Handled = true; 
} 
In this article
Not finding the help you need? Improve this article