Page 1 of 1

Update Table of Content of docx document in word using docx4

PostPosted: Wed Sep 29, 2021 5:15 pm
by DenyKush
I want to update TOC in my docx document. One way is generate the table entries, and the corresponding page numbers. Now I can generate the table entries and show it in TOC. But the problem is there aren't right corresponding page numbers. So I need to write proper pageRef for every headings. There are some ways:
1) Use export document to PDF and define number of pages. You can use docx4j TOC Helper. But it need to use special converter, because plutext is died now. Maybe docx4j-export-FO now work fine. Disadvantages - spend time for converting and there is a probability of unproper page numbers.
2) Update TOC during opening the document. There are two ways to do so:
2.1) Use macros. Disadvantages - need to change security level.
2.2) Write <w:updateFields w:val="true" /> in setting.xml file of WordProcessingML. Disadvantages - special warning boxes for updating.

Is there other way to achieve my goal?

Re: Update Table of Content of docx document in word using d

PostPosted: Thu Sep 30, 2021 7:58 am
by jason
See https://www.docx4java.org/blog/2020/09/ ... x4j-8-2-3/

You can use documents4j: see local converter example https://github.com/plutext/docx4j/blob/ ... tions.java

Remote should also work: https://github.com/plutext/docx4j/blob/ ... .java#L314

When I last checked, Microsoft Graph couldn't update docx table of contents; this may have changed now. Please let us know if you try it.

Re: Update Table of Content of docx document in word using d

PostPosted: Sat Oct 09, 2021 7:32 am
by DenyKush
Hello! Thank you for your answer!
Yes, I tried docx4j-fo service, but in my case it was bad quality in pagination. (you can see the topic, where I described that more presicely docx-java-f6/converting-wordmlpackage-to-fo-using-docx4j-tofo-t3027.html)

Unfortunately, documents4j is a Microsoft product and it may be the problem with the license.

I had another way to update the TOC in the document. You may use libreoffice API. Libreoffice has the opportunity to update the TOC just with help of it’s API. You may use UNO development tools. The accurate example of that you may find at https://github.com/sgavmp/automataRepor ... eport.java

Of course there are some differences between word and libre, but anyway.

Hope that information will be useful for someone :)