Move table of content

I want to move table of contents node from one page to another. Need assistance to accomplish this

Shubhangi Gupta
Hi Shubhangi,

Thanks for your inquiry. In your case, you need to move the cursor to the start of TOC field and insert page break.

Please check the following code example. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder docBuilder = new DocumentBuilder(doc);

for (Field field : doc.getRange().getFields())
{
if(field.getType() == FieldType.FIELD_TOC)
{
docBuilder.moveToField(field, false);
docBuilder.insertBreak(BreakType.PAGE_BREAK);
break;
}
}

doc.save(MyDir + "Out v16.12.0.docx");

Hi Tahir,

Sorry if the issue was not explained properly. I want to move a TOC from one document to another. Other document may already have a TOC and I wish to replace it with this copied one.
Thanks
Hi Shubhangi,

Thanks for your inquiry. There is no need to move the TOC field from one document into another. Please move the cursor to the desired location and use DocumentBuilder.InsertTableOfContents method to insert a TOC (table of contents) field into the document.

Note that InsertTableOfContents will only insert a TOC field, but will not actually build the table of contents. The table of contents is built by Microsoft Word when the field is updated. In Microsoft Word, fields are not automatically updated when a document is opened, but you can update fields in a document at any time by pressing F9.

If document has already TOC field, please use Document.updatefields to update it.

If you still face problem please share your input and expected output documents, we will then provide you more information on this.