Changed ToC UpdateFields() behavior

Hi,

I’ve recently updated Aspose.Words from 24.2 to 24.11, but I’ve checked and this also happens on 25.1. Using the attached docx file, if you load it as Document, run UpdateFields on it, and save it to docx, an extra item appears in the ToC. This item does not appear when the ToC is updated in Word, and back in version 24.2 Aspose.Words did behave in line with Word. The item seems to be due to the field in the last paragraph with outline level 2, but since word ignores it when updating fields I think Aspose should do the same?

It can be replicated with code like:

var doc = new Document(path); 
doc.UpdateFields();
doc.Save(outputpath);

I’ve also had the extra entries in ToC appear when running MailMerge on a document, but I’mguessing it must be due to mailMerge updating fields in the background.

ToC.docx (12.3 KB)

@acturisaspose

It seems that you are experiencing an issue with the behavior of the UpdateFields method in Aspose.Words after updating from version 24.2 to 24.11 (and also in 25.1). Specifically, you are noticing that an extra item appears in the Table of Contents (ToC) when you run UpdateFields on your document, which does not occur when updating the ToC in Microsoft Word.

This behavior can be attributed to how Aspose.Words handles field updates. When you load a document and call UpdateFields, Aspose.Words does not automatically update fields unless explicitly instructed to do so. This means that if there are fields in the document that are not relevant for the ToC, they may still be included in the output if they are not filtered out during the update process.

To address this issue, you might want to consider the following approaches:

  1. Check Field Types: Ensure that the fields being included in the ToC are appropriate. Aspose.Words does not update all field types, and some fields may be included unintentionally. You can refer to the documentation for a detailed list of supported field types.

  2. Use NormalizeFieldTypes: Before calling UpdateFields, you can use the NormalizeFieldTypes method to ensure that any changes in the document that affect field types are accounted for. This might help in preventing unwanted fields from appearing in the ToC.

  3. Manual Filtering: If the extra item is consistently related to a specific field (like the one in the last paragraph with outline level 2), you may need to manually filter out such fields before updating the ToC.

Here is a sample code snippet that demonstrates how to update fields in a document:

var doc = new Document(path); 
doc.UpdateFields();
doc.Save(outputpath);

If the issue persists, you may want to consider reaching out to Aspose support for further assistance, as they can provide more specific guidance based on the document you are working with.

For more information on updating fields in Aspose.Words, you can refer to the official documentation on the UpdateFields method here.

If you have any further questions or need additional assistance, feel free to ask!

@acturisaspose
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27759

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.