Getting Outlines.Count throws TargetInvocationException and Inner Exception: Destination Array expected.
If you think there is a bug in PDFsharp then please use the IssueSubmissionTemplate to make the issue replicable.
https://docs.pdfsharp.net/General/IssueReporting.html
Thanks.
Resources
The official project web site:
https://docs.pdfsharp.net/
The official peer-to-peer support forum:
http://forum.pdfsharp.net/
Reporting an Issue Here
Expected Behavior
When trying to get the bookmark count I expect to get some kind of PdfOutlineCollection object (maybe nested?). Instead I get an exception. See the attached picture.
Actual Behavior
System.Reflection.TargetInvocationException
Inner Exception 1: Exception: Destination Array expected

Call stack:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at PdfSharp.Pdf.PdfDictionary.DictionaryElements.CreateDictionary(Type type, PdfDictionary oldDictionary)
at PdfSharp.Pdf.PdfDictionary.DictionaryElements.GetValue(String key, VCF options)
at PdfSharp.Pdf.Advanced.PdfCatalog.get_Outlines()
at PdfSharp.Pdf.PdfDocument.get_Outlines()
at PdfTools.Models.PdfSharpHelper.MergePDFsWithBookmarks(String targetPath, String[] pdfs) in E:\GitHub\PdfTools\Models\PdfSharpHelper.cs:line 52
at PdfTools.ViewModels.MainViewModel.MergePdfs() in E:\GitHub\PdfTools\ViewModels\MainViewModel.cs:line 40
at PdfTools.ViewModels.MainViewModel.<get_MergePdfsCommand>b__14_0() in E:\GitHub\PdfTools\ViewModels\MainViewModel.cs:line 32
at Utilities.CommandHandler.Execute(Object parameter) in E:\GitHub\Utilities\Utils\Commands.cs:line 31
at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at PdfTools.App.Main()
This exception was originally thrown at this call stack:
PdfSharp.Pdf.PdfOutline.Initialize()
PdfSharp.Pdf.PdfOutline.InitializeChildren()
PdfSharp.Pdf.PdfOutline.Initialize()
PdfSharp.Pdf.PdfOutline.PdfOutline(PdfSharp.Pdf.PdfDictionary)
Inner Exception 1:
Exception: Destination Array expected.
Steps to Reproduce the Behavior
The pdf is something I've received externally and can't share it. It is produced with Python and TeX somehow. Maybe LaTeX or some other engine.
We strongly recommend using the IssueSubmissionTemplate to make sure we can replicate the issue.
https://docs.pdfsharp.net/General/IssueReporting.html
I will try to use the template to provide better test oppurtunities.
Code to reproduce.
var pdfPath = "document.pdf";
PdfDocument inputDocument = PdfReader.Open(pdfPath, PdfDocumentOpenMode.Import);
var outlinesCount = inputDocument.Outlines.Count;
I'm also seeing this issue when I try to do doc.Outlines.Clear() on some similar PDFs.
Some more info for the person that works on this. The issue in my case presents when attempting to open then immediately clear the outline when the pdf being opened has a very large number of oultine entries. If I await Task.Delay(500); before clearing outlines, the issue goes away.