PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Merge documents outlines
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4475
Page 1 of 1

Author:  manthislibrarysucks [ Fri Sep 15, 2023 7:17 pm ]
Post subject:  Merge documents outlines

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?

Author:  TH-Soft [ Mon Sep 18, 2023 8:00 am ]
Post subject:  Re: Merge documents outlines

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

Author:  manthislibrarysucks [ Mon Sep 18, 2023 9:33 am ]
Post subject:  Re: Merge documents outlines

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?

Author:  TH-Soft [ Mon Sep 18, 2023 10:05 am ]
Post subject:  Re: Merge documents outlines

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.

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