Hi,
Hi there,
Thanks for your inquiry. Please check the following code snippet for the scenario; hopefully, it will help you accomplish the task in a generic way.
string fullPath1 = myDir + ("TestBM1.pdf"); //have bookmarks
string fullPath2 = myDir + ("TestBM2.pdf"); //have bookmarks
string fullPath3 = myDir + ("TestBM3.pdf"); //no bookmarks inside this on
string fullPath4 = myDir + ("TestBM4.pdf"); //no bookmarks inside this on
Aspose.Pdf.Document first = new Aspose.Pdf.Document(fullPath1);
Aspose.Pdf.Document second = new Aspose.Pdf.Document(fullPath2);
Aspose.Pdf.Document third = new Aspose.Pdf.Document(fullPath3);
Aspose.Pdf.Document fourth = new Aspose.Pdf.Document(fullPath4);
Aspose.Pdf.Document[] input = new Aspose.Pdf.Document[4]
{ first, second, third, fourth };
//I tried this way, without luck
for (Int32 i = 0; i < input.Length; i++)
{
Aspose.Pdf.Document oneDoc = input[i];
OutlineCollection rootBookmark = oneDoc.Outlines;
OutlineItemCollection pdfOutline = new OutlineItemCollection(oneDoc.Outlines);
pdfOutline.Title = "File " + i;
pdfOutline.Action = new Aspose.Pdf.InteractiveFeatures.GoToAction(oneDoc.Pages[1]);
foreach (OutlineItemCollection bookmarks in rootBookmark)
{
pdfOutline.Add(bookmarks);
}
oneDoc.Outlines.Delete();
oneDoc.Outlines.Add(pdfOutline);
}
Aspose.Pdf.Document combinedDoc = new Aspose.Pdf.Document();
//concatenate all documents to single document
Aspose.Pdf.Facades.PdfFileEditor pdfEditor = new Aspose.Pdf.Facades.PdfFileEditor();
pdfEditor.Concatenate(input, combinedDoc);
combinedDoc.Save(myDir + "combined.pdf");
Please feel free to contact us for any further assistance.
Best Regards,
Hi, thanks for the reply. I was not aware of the PdfFileEditor and the Concatenate method. But it seems to have the same problem, it only keeps the bookmarks of the first file, and discards the bookmarks in the following files.
ole.helgesen:
I was not aware of the PdfFileEditor and the Concatenate method. But it seems to have the same problem, it only keeps the bookmarks of the first file, and discards the bookmarks in the following files.I am afraid I am unable to observe this behavior. Please check shared document in my above post, bookmarks of second and onward files preserved the bookmarks. Can you please share some more information here to get this point?ole.helgesen:
Besides, I have two scenarios: a simple merge where all files are just concatenated, and a more complex one where each file is inserted into a pdf document at different locations, decided by a chapter code.By using my workaround, I have now achieved both to work beautifully, with new top level bookmarks to each file inserted, and with existing bookmarks inserted as child bookmarks.Does it mean you have managed to resolve the bookmark issue in merged files? We should consider this issue resolved right?Best Regards,
On the first issue: If you want to join two pdf files with bookmarks, it does not matter if you open the first file and insert the second file at the end (as I do) or if you use the concatenate-method (as you proposed) to merge all in a single call.
Hi there,
Hi, I tested the concatenate method again, and it preserves all bookmarks as it should in all files, including handling the relocation to new page as they are joined together. This will simplify the first merge operation, thank you!!
Hi there,