Merging many word documents and creating table of contents

I am using aspose word for java, and I have many word documents that i merged in to one document. Using

Document doc = documents.get(0);

for(int i=1;i<documents.size();i++){

where documents is a List

I want to create a table of contents entry for each document I merged. Is that possible?


Yes, you can achieve your requirement using Aspose.Words. In your case, we suggest you following solution.

  1. Move the cursor to the start of document and and insert table of content.
  2. Insert the document using DocumentBuilder.InsertDocument method.
  3. Insert the desired heading before inserting the document using DocumentBuilder.Write method. Set the paragraph’s style to Heading 1 or Heading 2.

We suggest you please read the following articles.
Inserting a Table of Contents
Moving the Cursor

Hi Tahir,
Thank you for your kind response. Is there way to do this with out modifying documents? Because some documents already have titles but their style are different, and i don’t want to do changes on them. I already have document meta data on my database like title, I want to use that information generate toc if it’s possible?


You do not need to add heading text in the document. You can use the TOC field codes as shown below. The table of content will be generated for Heading and Title styles.

{ TOC \o “1-3” \h \z \t “Title,1” }

Moreover, we suggest you please read the following article.
How-to Insert and Work with the Table of Contents Field