Inserting TOC while original document has TOC updates both TOC

Hi,

I am currently using the Aspose.Words Java version. One of the thing I am doing is merging multiple word documents into single word documents and once this is done, I insert a new TOC which reflect all multiple merged documents.

My problem is, when a document already has a TOC and i use document.update to update the final TOC, it updates both TOC (inserted one and the one that was original there in the document).

Is there a way, i can prevent this behavior and only update the TOC i am inserting programmatically and no the original one.

To reproduce:

Generate 2 Word document with TOC (insert multiple document with TOC).

Insert TOC programmatically using

builder.insertTableOfContents("\o "1-3" \h \z \u");

and finally at the end of the merge, do a doc.update();

All the TOC will be updated.

Please let me know if you need any more inforamtion from my side.

Thanks,
Kunal

Hi Kunal,

Thanks for your inquiry. Please use Field.update method to update the field. Please check following code snippet and let us know if you have any more queries.

// Create a document builder to insert content with into document.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents at the beginning of the document.
com.aspose.words.Field field = builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
field.update();