I am trying to implement bookmarks for a single page PDF using PDFsharp on a C# project using Visusal Studio 2019. I get it to work if I place a breakpoint in the last line of the code under. But without the breakpoint it is throwing an exception.
The code I am using:
Code:
PdfDocument document = PdfReader.Open(FilePath, PdfDocumentOpenMode.Modify);
PdfPage pagepdf;
pagepdf = document.Pages[0];
PdfOutline outline = document.Outlines.Add("Sheets", pagepdf, true, PdfOutlineStyle.Bold); Gives exception at this line
document.Save(FilePath);
document.Close();
The exception is:
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'
InvalidCastException: Unable to cast object of type 'PdfSharp.Pdf.PdfNull' to type 'PdfSharp.Pdf.Advanced.PdfReference'.What am I doing wrong? Why does it work when the breakpoint is on the line that the give exception?