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 DocumentUnhandledException 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 corrupted 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 DocumentUnhandledException 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;
}
Exceptions
Exception | Description |
---|---|
DuplicatedEmbeddedFileNameException | Represents an exception for embedding a file with a duplicated name. |
InvalidActionException | Represents an exception for importing an invalid action. |
InvalidGraphicOperandsCountException | Represents an exception for importing a graphic operator with an invalid number of operands. |
NotSupportedActionException | Represents an exception for an action which is not supported. |
NotSupportedCCITTFaxDecodeFilterException | Represents an exception for a scan decoder which is not supported. |
NotSupportedCharsetFormatException | Represents an exception for an charset format which is not supported. This exception has a CharsetFormat property which specifies the name of the CharsetFormat. |
NotSupportedColorSpaceException | Represents an exception for a color space which is not supported. This exception has a ColorSpace property which specifies the name of the ColorSpace. |
NotSupportedCompressionMethodException | Represents an exception for importing a FlateDecode method which is not supported. |
NotSupportedEncryptionException | Represents an exception for an encryption which is not supported. This exception has e EncryptionCode property which specifies the code of the encryption. |
NotSupportedEncryptionRevisionException | Represents an exception for an encryption revision which is not supported. This exception has a RevisionCode property which specifies the name of the RevisionCode. |
NotSupportedFeatureException | Represents an exception for a feature which is not supported. |
NotSupportedFilterException | Represents an exception for a filter which is not supported. This exception has a FilterName property which specifies the name of the filter. |
NotSupportedFontException | Represents an exception for a font which is not supported. This exception has a FontType property which specifies the type of the font. |
NotSupportedFontFamilyException | Represents an exception for a font family which is not supported. |
NotSupportedFunctionTypeException | Represents an exception for a function type which is not supported. This exception has a FunctionType property which specifies the name of the FunctionType. |
NotSupportedPaintTypeException | Represents an exception for a paint type which is not supported. This exception has a PaintType property which specifies the name of the PaintType. |
NotSupportedPredefinedCMapException | Represents an exception for a predefined CMap which is not supported. This exception has a CMapName which specifies the name of the predefined CMap. |
NotSupportedReservedMethodException | Represents an exception for importing a FlateDecode reserved method which is not supported. |
NotSupportedScanDecoderException | Represents an exception for a document with a scan decoder which is not supported. |
NotSupportedScanEncoderException | Represents an exception for a scan decoder which is not supported. |
NotSupportedShadingTypeException | Represents an exception for a shading type which is not supported. This exception has e ShadingType property which specifies the type of the shading. |
NotSupportedStreamTypeException | Represents an exception for a stream type which is not supported. A stream is not supported if it does not support read or seek. This exception has a SupportSeek and SupportRead properties which specify whether the stream supports them. |
NotSupportedXObjectTypeException | Represents an exception for document with an XObject type which is not supported. |