Free Support Forum - aspose.com

Missing bookmarks when combining pdfs in aspose pdf (java)

Hi,
I have 2 pdfs - a cover page pdf and a notes page pdf. The cover page pdf does not contain any bookmarks but the notes page contains 1 bookmark.

When I combine the pdfs using the following code, the resulting combined pdf does not contain any bookmarks. Am I missing something?

com.aspose.pdf.Document coverPagePdf = new com.aspose.pdf.Document(“cover_page.pdf”)
com.aspose.pdf.Document notesPagePdf = new com.aspose.pdf.Document(“notes_page.pdf”)

coverPagePdf.getPages().add(notesPagePdf.getPages())

coverPagePdf.save(“combined”.pdf")

Thanks!

@andrhahn1,

Thanks for contacting support.

When merging PDF files containing bookmarks, you need to exclusively copy the bookmarks. Please take a look over following code snippet. In case you still face any issue, please share the input PDF files, so that we can test the scenario in our environment.

[C#]

foreach (string s in Files)
{
// string book = s.Substring(s.LastIndexOf("_"));
Document pdfDocument = new Document(s);
// Create a bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "3854-report-disclaimer-guidance-for-site - Copy.pdf";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Set the destination page number
pdfOutline.Action = new Aspose.Pdf.Annotations.GoToAction(pdfDocument.Pages[1]);
// Add bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);
// Save output
pdfDocument.Save(s);

}
foreach (string s in Files)
{
Document pdfDocument1 = new Document(s);
res.Pages.Add(pdfDocument1.Pages);
foreach (OutlineItemCollection item in pdfDocument1.Outlines)
{
res.Outlines.Add(item);
}
}
res.Save("result.pdf");

Please pay attention to following code lines.

[C#]
foreach (OutlineItemCollection item in pdfDocument1.Outlines)
{
res.Outlines.Add(item);
}

Thanks for the reply. Using your example code I am able to combine 2 pdfs and the bookmarks work great. However this only works when BOTH pdf documents contain bookmarks.

Bookmarks DO NOT work if I combine 2 pdfs and only the second page contains bookmarks. Any ideas?

@andrhahn1,

Thanks for sharing the updates.

Can you please share the sample PDF files, so that we can test the scenario in our environment. We are sorry for this inconvenience.

Here are some test cases. Please let me know what you find. Thanks-

bookmarks1.pdf (11.4 KB)
bookmarks2.pdf (11.4 KB)
no_bookmarks.pdf (80.3 KB)

def "combining two pdfs with bookmarks should preserve bookmarks - works"() {
    expect:
    com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document("C:\\temp\\bookmarks1.pdf")
    com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document("C:\\temp\\bookmarks2.pdf")
    // combine pdfs
    pdf1.getPages().add(pdf2.getPages())
    // combine bookmarks
    List<com.aspose.pdf.OutlineItemCollection> pdf2Bookmarks = IteratorUtils.toList(pdf2.outlines.iterator())
    pdf2Bookmarks.each { com.aspose.pdf.OutlineItemCollection bookmark ->
        pdf1.outlines.add(bookmark)
    }
    pdf1.save("C:\\temp\\combined.pdf")
}
def "combining two pdfs with bookmarks should preserve bookmarks - does not work"() {
    expect:
    com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document("C:\\temp\\no_bookmarks.pdf")
    com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document("C:\\temp\\bookmarks2.pdf")
    // combine pdfs
    pdf1.getPages().add(pdf2.getPages())
    // combine bookmarks
    List<com.aspose.pdf.OutlineItemCollection> pdf2Bookmarks = IteratorUtils.toList(pdf2.outlines.iterator())
    pdf2Bookmarks.each { com.aspose.pdf.OutlineItemCollection bookmark ->
        pdf1.outlines.add(bookmark)
    }
    pdf1.save("C:\\temp\\combined.pdf")
}

@andrhahn1,

Thanks for sharing the details and sample files.

I have tried executing the code snippet but there were some errors during the code compile i.e. IteratorUtils, pdf2Bookmarks.each { -> etc.

In order to test the scenario, I have used following simplified code where I have first copied pages of second PDF document to first PDF file and then have copied the bookmarks to Bookmarks collection of first document and I am unable to notice any issue. For your reference, I have also attached the output generated over my end. combined.pdf (22.5 KB)

PS, as per my observations, both bookmarks1.pdf and bookmarks2.pdf contain bookmarks.

[Java]

com.aspose.pdf.Document pdf1 = new com.aspose.pdf.Document("C:\\pdftest\\bookmarks1.pdf");
com.aspose.pdf.Document pdf2 = new com.aspose.pdf.Document("C:\\pdftest\\bookmarks2.pdf");

// combine pdfs
pdf1.getPages().add(pdf2.getPages());

// combine bookmarks
//    java.util.List<com.aspose.pdf.OutlineItemCollection> pdf2Bookmarks = (java.util.List<OutlineItemCollection>)pdf2.getOutlines())
    for (OutlineItemCollection outlineItem : (Iterable<OutlineItemCollection>) pdf2.getOutlines()) {
   // pdf2Bookmarks.each { com.aspose.pdf.OutlineItemCollection bookmark ->
    pdf1.getOutlines().add(outlineItem);
}

pdf1.save("C:\\pdftest\\combined.pdf");

Yes I already knew that combining bookmarks1.pdf and bookmarks2.pdf resulted in a combined.pdf which contained working bookmarks. This was the first test scenario I had posted above.

Please try the second scenario I had pasted above which is where I am combining no_bookmarks.pdf and bookmarks2.pdf. That is where the combined.pdf does not contain any bookmarks. Thanks

@tilal.ahmad any ideas?

@andrhahn1,

Thanks for sharing the details.

I have tested the scenario and have managed to reproduce the same problem that bookmarks are missing in resultant document. For the sake of correction, I have logged it as PDFJAVA-36883 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Thanks @codewarior

Hi @codewarior, I would like to know if the issue PDFJAVA-36883 has been solved. Thanks

Hello, we need to know if this problem has been fixed and in what version of Aspose PDF for Java. Thanks.

@fabio.parise,

I have observed your comments. I regret to inform that issue is still unresolved owing to missing support in Aspose.PDF. The issue is blocked owing to missing implementation in Aspose.Slides for .NET. This issue will be resolved first in >NET than in Aspose.PDF Java. We will share good news with you regarding ETA for this issue soon.