We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copied bookmarks not working when merging files

Hi,

I am merging a set of pdf’s, and create a top level bookmark to each file.
Existing bookmarks for the files should be inserted as child bookmarks to the new file bookmark.
I open the first file, create the new bookmark, and copy the existing outlines (bookmarks).
I then delete the existing outlines and add the new one.
The result is correct and working.

For the subsequent files, I create a new bookmark, append the pages of the new file, and copy the bookmarks to the new file level bookmark. I also adjust the target page number, because I need to offset with the number of pages of the previous file(s).
The file level bookmark is then appended to the main file, and it seems to contain the correct number of child bookmarks.
However, when the file is opened, the child bookmarks is just a single blank, “dead” bookmark!
See attached screenshot, with error on File 2 (should be same as File 1)

I have made a workaround, where I create new bookmarks and copy the properties.
But this will not cover all types of bookmarks and is a bit risky!

The full source code is included, with test pdf files (file 3 has no bookmarks)
Note that File 2 has the non-working code, File 3 & 4 is using the workaround.
Tested on version 9.3 and 10.6

Hi there,


Thanks for your inquiry. Please check following code snippet for the scenario, hopefully it will help you to accomplish the task in a generic way.

string fullPath1 = myDir + (“TestBM1.pdf”);//have bookmarks<o:p></o:p>

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.

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.
Hi there,

Thanks for your feedback.

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.

In both cases, only the bookmarks from the first file is remaining.
Which is not really a surprise, because all the other bookmarks must be transposed because the target page is now different, and must account the preceeding pages being inserted.

So I have figured out how to recalculate the new target location, and I now recreate the bookmarks using the new target page.

I don’t know if we should consider this as a bug, but it would be very nice if bookmarks were copied properly and realigned to new location.

Hi there,


Thanks for your feedback. It is good to know that you have managed to resolve your issue.

However, I am afraid still I couldn’t notice the issue you pointed, missing bookmarks of subsequent files to first document in combined.Pdf.

Best Regards,

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!!

Sorry I did not test it properly the first time I guess…

I still have to handle it manually when I have to insert the files based on chapter codes, but that also seems to work now, so thanks again!

Hi there,


Thanks for your feedback. You may use any solution as per your need.

Please keep using Aspose.Pdf and feel free to contact us for any further assistance, we will be more than happy to extend our support.

Best Regards,