I just came across a document that contained the following :
Code:
....
9 0 obj
null
....
trailer
<</Size 26/Root 3 0 R/Info 9 0 R>>
startxref
....
As one can see, the /Info entry in the trailer is a reference to a null-object.
When loading the document with PdfSharp, this results in an assertion in debug mode ("Bug in PdfSharp. Send this document...") and a crash in release mode:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'PdfSharp.Pdf.PdfNullObject' to type 'PdfSharp.Pdf.PdfDictionary'.
at PdfSharp.Pdf.PdfDictionary.DictionaryElements.GetValue(String key, VCF options)
at PdfSharp.Pdf.PdfDocument.get_Info()
at PdfSharp.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider)
at PdfSharp.Pdf.IO.PdfReader.Open(String path, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider provider)
Digging into this, i was able to fix this with the following changes:
In PdfDictionary.GetValue(String key, VCF options), change the first if-statement from
Code:
if (value == null)
to
Code:
if (value == null || value is PdfNull || (value is PdfReference && ((PdfReference)value).Value is PdfNullObject))
thus treating a reference to a null-object like null.
If this may lead to regressions for some reason, please let me know.