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

PDFsharp: Exception with bookmark creation
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4263
Page 1 of 1

Author:  luispolaris [ Sun Jun 20, 2021 6:49 pm ]
Post subject:  PDFsharp: Exception with bookmark creation

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?

Author:  rsoeung [ Wed Jun 23, 2021 6:22 am ]
Post subject:  Re: PDFsharp: Exception with bookmark creation

I'm able to execute this just fine in dotnetfiddle.net. Try running your code in Release mode.
Code:
     // Create a new PDF document
      PdfDocument doc = new PdfDocument();
      doc.AddPage(new PdfPage());
      MemoryStream str = new MemoryStream();
      doc.Save(str, false);
      
      PdfDocument document = PdfReader.Open(str, PdfDocumentOpenMode.Modify);
      PdfPage pagepdf;
      pagepdf = document.Pages[0];
      PdfOutline outline = document.Outlines.Add("Sheets", pagepdf, true, PdfOutlineStyle.Bold);
      
      Console.WriteLine(document.Outlines.Count);
      
      document.Save(str);
      document.Close();

Author:  luispolaris [ Wed Jun 23, 2021 6:34 am ]
Post subject:  Re: PDFsharp: Exception with bookmark creation

Perhaps it the issue is the PDF that is being changed. I believe that is PDF1.7. But I also tried with 1.5 without success.

Please see the PDF in the link under. I did not manage to upload it to this post.
https://we.tl/t-yVUeVKIG1h

Do you manage to add outline to this one?

I am also able to run the sample code from PDFsharp to create 5 pages and add bookmarks.

Author:  rsoeung [ Thu Jun 24, 2021 7:31 am ]
Post subject:  Re: PDFsharp: Exception with bookmark creation

I'm able to run your code using the pdf you'd provided and didn't encounter any exceptions. I'm using PdfSharp v1.50.5147.

Author:  luispolaris [ Thu Jun 24, 2021 7:34 am ]
Post subject:  Re: PDFsharp: Exception with bookmark creation

What dlls from PDFsharp are you referring in the project?

Author:  rsoeung [ Mon Jun 28, 2021 6:16 am ]
Post subject:  Re: PDFsharp: Exception with bookmark creation

luispolaris wrote:
What dlls from PDFsharp are you referring in the project?


I'm using the latest from Nuget. https://www.nuget.org/packages/PdfSharp/1.50.5147

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