PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

PDFSharp Outline Help
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4082
Page 1 of 1

Author:  J_Adams [ Tue Jan 14, 2020 8:55 pm ]
Post subject:  PDFSharp Outline Help

Hello! Thank you for such a wonderful tool!

I am making a small program that appends a page to a large PDF file and creates a new bookmark for it using a name provided by the user. It will work with my test files - no outline and single level outline - but will throw an exception when I try using a multi-level outline as my destination:

Code:
//Open the files
PdfDocument inputDestination = PdfReader.Open(pathDestination, PdfDocumentOpenMode.Import);
PdfDocument inputSource = PdfReader.Open(pathSource, PdfDocumentOpenMode.Import);

//Inserts the Source page in to the ScanDoc
PdfPage newPage = inputSource.Pages[0];
inputDestination.AddPage(newPage);

//Bookmarking
string bookmarkName = Interaction.InputBox("Please provide a name for the bookmark:", "New Bookmark");
int count = inputDestination.PageCount -1;
PdfPage lastPage = inputDestination.Pages[count];
PdfOutline outline = inputDestination.Outlines.Add(bookmarkName, lastPage);

//Saves the ScanDoc
inputDestination.Save(pathDestination);


Throws an error against that last line of my "Bookmarking" block:
Code:
System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.'

This exception was originally thrown at this call stack:
   PdfSharp.Pdf.PdfOutline.SplitDestinationPage(PdfSharp.Pdf.PdfArray)
   PdfSharp.Pdf.PdfOutline.Initialize()
   PdfSharp.Pdf.PdfOutline.InitializeChildren()
   PdfSharp.Pdf.PdfOutline.Initialize()
   PdfSharp.Pdf.PdfOutline.InitializeChildren()
   PdfSharp.Pdf.PdfOutline.Initialize()
   PdfSharp.Pdf.PdfOutline.InitializeChildren()
   PdfSharp.Pdf.PdfOutline.Initialize()
   PdfSharp.Pdf.PdfOutline.PdfOutline(PdfSharp.Pdf.PdfDictionary)


I know I am missing something with the levels, but I cannot find anything in the documentation. I just need the bookmark created in the tree, it can be manually moved where needed.

My thanks in advance for any advice or direction anyone can provide.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/