PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Oct 03, 2024 10:45 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Merge documents outlines
PostPosted: Fri Sep 15, 2023 7:17 pm 
Offline

Joined: Fri Sep 15, 2023 6:29 pm
Posts: 2
Can you please advise on how to merge the source document's outline into a PdfDocument instance when consolidating multiple documents into a single one?

This example works fine until I attempt to add the PdfOutline instance from the opened document to the outputPdf's:

Code:
var documentPaths = new { "./A.pdf", "./B.pdf" };
var outputPdf = new PdfDocument();
var index = 0;
foreach (var currentPdf in documentPaths.Select(path => PdfReader.Open(path, PdfDocumentOpenMode.Import))) {
  //  This works.
  outputPdf.Pages.InsertRange(index, currentPdf, AnnotationCopyingType.DeepCopy);
  index += currentPdf.PageCount;

  // This breaks because of an exception (see next block of code)
  currentPdf.Outlines.ForEach(outline => outputPdf.Outlines.Add(outline));
}


The version of PdfSharp (PdfSharpCore) in use is 1.3.60.
Looks like the library is attempting to update the document on the source outline while copying the object over to the output outline. The exception message is the following:

Code:
System.InvalidOperationException: Cannot change document.
   at PdfSharpCore.Pdf.PdfObject.set_Document(PdfDocument value)
   at PdfSharpCore.Pdf.PdfOutlineCollection.AddToOutlinesTree(PdfOutline outline)
   at PdfSharpCore.Pdf.PdfOutlineCollection.Add(PdfOutline outline)


The behavior is unexpected if not wrong, but I can see why the library code wants to set the a reference to the new document on the source outline.

As an alternative, I've tried creating and adding fresh PdfOutline instances to the output document's outline. This works, but I'm now having a bit of trouble correlating this floating outline instance with the page it's meant to point to.

I can't correlate the new outline to a source document's page because outline.DestinationPage is always NULL, so doing this doesn't work:

Code:
currentPdf.Outlines.ForEach(outline => outputPdf.Outlines.Add(outline.Title, outline.DestinationPage));


It seems to me that DestinationPage is NULL because the original outline is PdfPageDestinationType.Xyz, which is OK. I could work with that, but I need a little help.

Knowing that my PdfOutline is PdfPageDestinationType.Xyz, how can I get the outline's destination position and convert that into a reference to the page? Ideally in the outputPdf, but even finding the destination in the original PdfPage instance would work. I should be able to come up with that code myself.

Alternatively, are there any better, or more sensible approaches to merging a document's outline with another?


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 18, 2023 8:00 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 986
Location: CCAA
manthislibrarysucks wrote:
The version of PdfSharp (PdfSharpCore) in use is 1.3.60.
Not our library.

See also:
https://docs.pdfsharp.net/General/IssueReporting.html

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 18, 2023 9:33 am 
Offline

Joined: Fri Sep 15, 2023 6:29 pm
Posts: 2
Thanks for your time and providing a link, I'll have a look at their website. I figured the API I was interacting with was somehow still part of PdfSharp :?

So, just to clarify the distinction, PdfSharpCore forked your PdfSharp & MigraCore products at a fixed version, added .NET Core abstractions around it and essentially carried on with their own line of development without ever sending patches upstream?


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 18, 2023 10:05 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 986
Location: CCAA
manthislibrarysucks wrote:
So, just to clarify the distinction, PdfSharpCore forked your PdfSharp & MigraCore products at a fixed version, added .NET Core abstractions around it and essentially carried on with their own line of development without ever sending patches upstream?
That's more or less the situation.
The outline problem probably exists with both versions, but especially image handling and font handling are somewhat different.

From the PdfSharpCore readme file:
Quote:
PdfSharpCore is a partial port of PdfSharp.Xamarin for .NET Standard. Additionally MigraDoc has been ported as well (from version 1.32). Image support has been implemented with SixLabors.ImageSharp and Fonts support with SixLabors.Fonts.

So PdfSharpCore is a partial port of a partial port of PDFsharp.
MigraDoc had huge speed improvements with version 1.5 and with version 6.0.


BTW: With respect to the exception being thrown, maybe you have to create a "clone" of the object you are adding. Call the "Clone" method if available or create a deep copy.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 76 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