Document.UpdateFields does not update the TOC correctly using .NET


I’m programmatically creating a word document. I’d like the TOC to show section titles and total pages in that section (rather than the start page number).

My word document ends up with this TOC field -
{ TOC \f \h \n \w}

It correctly pulls in all the entries from the TC fields distributed around the document. The TC fields are effectively -

{ TC “Example section title \t {SECTIONPAGES}” }

The only thing that doesn’t work correctly is that when i open the resulting word document, I need to refresh the TOC before it seems to correctly style the tab character and align the number of pages in the section to the right.

This is how it looks when i first open the word doc
image.png (4.2 KB)

Then after i refresh the ToC
image.png (4.1 KB)

In code, I already call UpdateFields() before saving (twice in fact) and it doesn’t seem to trigger it in the same way that updating in Word does.

Any ideas?

It looks like when the TOC generates there isn’t a tab stop - but after refreshing the table, Word seems to add a right aligned one and sets it’s Leader


Please attach the following resources here for testing:

  • Input Word document.
  • Output Word file that shows the undesired behavior.
  • Expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

We will investigate the issue and provide you more information on it. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

I’ve put together this basic console app which is doing the kind of thing I’m trying to achieve.

Running the app takes the templates.docx (input document) and produces an output.docx (undesirable output doc) in the run location.

In terms of seeing the desirable output, after opening the generated output.docx file, if you right click and update the TOC, it shifts the numbers over to the right and adds in a tabstop - that’s the desired behaviour i’d like when the user first opens their document. (17.5 KB)

It’s worth noting that i’ve sort of been able to work around this problem in docx by re-inserting TabStop’s into the document once the fields have been updated. However when i export the same thing to PDF, the numbers still aren’t right aligned.


We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20691 . You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-20691) have been fixed in this Aspose.Words for .NET 20.8 update and this Aspose.Words for Java 20.8 update.