ststeiger / PdfSharpCore

Port of the PdfSharp library to .NET Core - largely removed GDI+ (only missing GetFontData - which can be replaced with freetype2)
Other
1.08k stars 237 forks source link

SplitDestinationPage in PdfOutine.cs blows up on this PDF #336

Open gevorgter opened 1 year ago

gevorgter commented 1 year ago

1217405.pdf

Exception: System.InvalidCastException: 'Unable to cast object of type 'PdfSharpCore.Pdf.PdfNull' to type 'PdfSharpCore.Pdf.Advanced.PdfReference'.'

This program would reproduce it var file = "1217405.pdf"; var document = PdfReader.Open(file, PdfDocumentOpenMode.Import); if (document.Outlines != null) Console.WriteLine("All good"); document.Close();

Problem is: destination.Elements[0] is PdfNull so line in SplitDestinationPage method blows up. PdfDictionary destPage = (destination.Elements[0] is PdfInteger) ? destination.Owner.Pages[((PdfInteger)destination.Elements[0]).Value] : (PdfDictionary)((PdfReference)destination.Elements[0]).Value;

The fix is: to check if it's PdfNull and exit. Repercussion is that DestinationPage will not be set. But i believe it does not need to be set in case we going over Outline in existent PDF.

void SplitDestinationPage(PdfArray destination) // Reference: 8.2 Destination syntax / Page 582 { // ReSharper disable HeuristicUnreachableCode

pragma warning disable 162

        **if (destination.Elements[0] is PdfNull)
            return;**
        // The destination page may not yet transformed to PdfPage.
        PdfDictionary destPage = (destination.Elements[0] is PdfInteger) ? 
            destination.Owner.Pages[((PdfInteger)destination.Elements[0]).Value] : 
            (PdfDictionary)((PdfReference)destination.Elements[0]).Value;