How do you create a dynamic TOC inside a word template
You can replace a mergefield with a TOC field by using the following code:
Document doc = new Document("D:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToMergeField("mf");
builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
doc.updateFields();
doc.save("D:\\temp\\awjava-18.2.docx");