Free Support Forum -

Merge Word Documents with Table of Content TOC Fields & Update Hyperlinks so that they point to Correct Heading Locations using Java

Hi Team,

I am trying to merge multiple docx documents to generate a single docx document.
Each document containing Table of content .When all the documents are getting merged using aspose.word ,in the final generated document the first table of content hyperlinks referring correct header in the final document but for the second table of content the hyperlink are referring incorrect headers as you can see in the below attached sample document.When you will click the second table of content hyperlink it is referring incorrect header location .

Your solution on this will be really helpful for (39.9 KB)

Uddeshya Pratik


To ensure a timely and accurate response, please ZIP and attach the following resources here for testing:

  • Your simplified input Word documents that you are merging into one
  • Aspose.Words 20.3 generated output DOCX file showing the undesired behavior
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.
  • Please also create a standalone simple runnable console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL/JAR files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you more information. Thanks for your cooperation.


Please find all the requested document and the description and issue details inside the attached folder.

Uddeshya Pratik (203.9 KB)


To fix “Actual_generated_document.docx”, you can just define the scope of second TOC field by enclosing the entire content of last section inside a Bookmark. Please try the following code:

Document doc = new Document("E:\\Temp\\Sample_documents\\Actual_generated_document.docx");

com.aspose.words.DocumentBuilder builder = new DocumentBuilder(doc);

BookmarkStart bookmarkStart = builder.startBookmark("bookmarked_Scope");

doc.getLastSection().getBody().getFirstParagraph().insertBefore(bookmarkStart, doc.getLastSection().getBody().getFirstParagraph().getFirstChild());

for (Field field : doc.getLastSection().getBody().getRange().getFields()) {
    if (field.getType() == FieldType.FIELD_TOC) {
        FieldToc tocField = (FieldToc) field;


Thanks, Hafeez for the above solution.

Now I am able to see in the newly generated document the TOC 2 hyperlinks are referring to correct header locations.

However, I came across a couple of observations after using the above code snippet given by you.

Formatting to table of content 2 is getting change as well as in the updated generated document few headers are also missing.

I am attaching a detailed issue description document along with the sample code and other documents for your reference.

Please help me to sort out these issues. (220.3 KB)

Uddeshya Pratik


We are checking these scenarios and will get back to you soon.


Did you get a chance to look into the above issue.



We tested the scenario and have managed to reproduce the same problems on our end. For the sake of correction, we have logged a ticket in our issue tracking system. The ID of this ticket is WORDSNET-20187. We will further look into the details of these problems and will keep you updated on the status of linked issue. We apologize for your inconvenience.


Please let me know once you will get any solution on this.

Uddeshya Pratik


Your issue (WORDSNET-20187) is currently ‘pending for analysis’ and is in the queue. We will inform you via this thread as soon as this issue will be resolved. We apologize for your inconvenience.