PdfSharpCore
PdfSharpCore copied to clipboard
Exception thrown when setting trailer info element to null
When opening specific documents I get an exception:
{"Value cannot be null. (Parameter 'value')"}
Unfortunately, these documents come from an external source, and all of them have this issue. As a work around I have to manually open the pdf in acrobat or something and then save it (without doing anything). This seems to add something to the "INFO" where it does not throw an exception. I would like to be able to still open the file in code.
Stack trace:
at PdfSharpCore.Pdf.PdfDictionary.DictionaryElements.set_Item(String key, PdfItem value) in PdfSharpCore.Pdf\PdfDictionary.cs:line 49
at PdfSharpCore.Pdf.Advanced.PdfTrailer.Finish() in PdfSharpCore.Pdf.Advanced\PdfTrailer.cs:line 158
at PdfSharpCore.Pdf.IO.PdfReader.Open(Stream stream, String password, PdfDocumentOpenMode openmode, PdfPasswordProvider passwordProvider, PdfReadAccuracy accuracy) in PdfSharpCore.Pdf.IO\PdfReader.cs:line 380
This happens in this code when trying to set the info element to null: https://github.com/ststeiger/PdfSharpCore/blob/cdf089b6c4d6b379aead95f463911dd009ae194e/PdfSharpCore/Pdf.Advanced/PdfTrailer.cs#L191C13-L198C14
iref = _document._trailer.Elements[PdfTrailer.Keys.Info] as PdfReference;
if (iref != null && iref.Value == null)
{
iref = _document._irefTable[iref.ObjectID]; // <-- this comes back as `null`
Debug.Assert(iref.Value != null);
_document._trailer.Elements[Keys.Info] = iref; // <-- this causes the exception
}
Expected behavior:
not to crash - allow setting the value to null or skip setting the element if the value is null