PdfSharpCore icon indicating copy to clipboard operation
PdfSharpCore copied to clipboard

SplitDestinationPage in PdfOutine.cs blows up on this PDF

Open gevorgter opened this issue 2 years ago • 0 comments

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;

gevorgter avatar Feb 12 '23 18:02 gevorgter