Need to merge two documents and keep original bookmarks

How can I merge Document1 and Document2 to produce the Merged document? Concatenating the files leaves all of the bookmarks on the same level. I tried adapting the code in this post but I’m still doing something wrong (https://forum.aspose.com/t/insert-a-bookmark-as-a-child-in-an-existing-bookmark-parent-using-pdf-outlines-in-c/19974).

Document1.pdf (505.5 KB)
Document2.pdf (727.3 KB)
Merged.pdf (1.3 MB)

@joe_lopez

Can you please share the code snippet that you are using to merge the PDF files at your end? Also, please share how you have generated the expected output PDF that you have shared with us? Is it using some other API or you have created it manually?

I figured it out, just needed a couple of tweaks. Originally, the output doc was created using activePDF. We’re replacing it everywhere with Aspose.

var firstPdf = new Document("Document1.pdf");
var secondPdf = new Document("Document2.pdf");
var originalPageCount = firstPdf.Pages.Count;

firstPdf.Pages.Add(secondPdf.Pages);

var parentOutline = new OutlineItemCollection(firstPdf.Outlines)
{
	Title = "Title",
	Action = new GoToAction(firstPdf.Pages[originalPageCount + 1])
};

var bookmarkEditor = new PdfBookmarkEditor(secondPdf);
foreach (var bookmark in bookmarkEditor.ExtractBookmarks())
{
	var childOutline = new OutlineItemCollection(firstPdf.Outlines)
	{
		Title = bookmark.Title,
		Action = new GoToAction(firstPdf.Pages[originalPageCount + bookmark.PageNumber])
	};

	parentOutline.Add(childOutline);
}

firstPdf.Outlines.Add(parentOutline);
firstPdf.Save("Merged.pdf");

@joe_lopez

It is nice to know that your issue has been resolved. Please keep using our API and feel free to create a new topic in case you face any issues.