PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Sep 17, 2021 1:54 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Sun Jun 20, 2021 6:49 pm 
Offline

Joined: Sun Jun 20, 2021 6:35 pm
Posts: 4
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?


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 23, 2021 6:22 am 
Offline

Joined: Sat Jan 09, 2021 7:09 am
Posts: 18
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();


Top
 Profile  
Reply with quote  
PostPosted: Wed Jun 23, 2021 6:34 am 
Offline

Joined: Sun Jun 20, 2021 6:35 pm
Posts: 4
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 24, 2021 7:31 am 
Offline

Joined: Sat Jan 09, 2021 7:09 am
Posts: 18
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 24, 2021 7:34 am 
Offline

Joined: Sun Jun 20, 2021 6:35 pm
Posts: 4
What dlls from PDFsharp are you referring in the project?


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 28, 2021 6:16 am 
Offline

Joined: Sat Jan 09, 2021 7:09 am
Posts: 18
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 28 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group