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.
**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;
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