# Updating TOC Page Numberings in Word Documents Before Exporting to DOCX Format

## Environment

| Version    | Product            | Author                                                                          |
| ---------- | ------------------ | ------------------------------------------------------------------------------- |
| 2024.2.426 | RadWordsProcessing | [Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova) |

## Description

When working with Word documents that contain a [Table of Contents (TOC)](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/toc-field), it is often necessary to update the TOC to reflect the correct page numbers before exporting the document to DOCX format. This KB article shows what steps are needed to refresh the page numbers in a document's TOC before exporting the flow document content to DOCX format.

| Before                                                                                                                                    | After                                                                                                                                   |
| ----------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| ![TOC Before](https://www.telerik.com/document-processing-libraries/documentation/assets/862f1170677822876018796d63c215df/toc-before.png) | ![TOC After](https://www.telerik.com/document-processing-libraries/documentation/assets/876d1e5343216df346ec0db59569bf96/toc-after.png) |

## Solution

To update the TOC in a [RadFlowDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/radflowdocument) before exporting it to DOCX format, follow these steps:

1. Ensure that your document contains a [TOC field](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/toc-field). RadWordsProcessing supports the TOC field and allows for its update before exporting the document.

2. Before exporting the document to PDF, use the `UpdateFields()` method to update all fields, including the TOC, in the `RadFlowDocument`. This ensures that the TOC reflects the correct page numbers.

3. To accurately update the TOC, including the correct page numbering, you need to calculate the layout of the document. RadWordsProcessing provides the [NumberingFieldsProvider](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/numbering-fields-provider) for this purpose.

```csharp
FlowExtensibilityManager.NumberingFieldsProvider = new NumberingFieldsProvider();
document.UpdateFields();
```

By setting the `NumberingFieldsProvider` and then calling `RadFlowDocument.UpdateFields()`, the document updates the TOC to reflect the correct page numbering.

It is possible to [update a single field](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/fields#updating-fields), not all of them:

```csharp
FieldCharacter fieldCharacter = document.EnumerateChildrenOfType<FieldCharacter>().First(x=> x.FieldInfo.Field is TocField);
FieldInfo fieldInfo = fieldCharacter.FieldInfo; 
fieldInfo.UpdateField(); 
```

4. With the TOC updated, you can now proceed to export the document to DOCX format, confident that the TOC displays the correct page references.

## Notes

* The `UpdateFields()` method updates all fields in the document, not only the TOC. Ensure that this behavior is acceptable for your document before you proceed.
* The `NumberingFieldsProvider` is essential for correct page number calculation in the TOC. Set it before you update the fields.

## See Also

* [RadFlowDocument](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/model/radflowdocument)
* [Table of Contents Field](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/toc-field)
* [NumberingFieldsProvider](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/concepts/fields/numbering-fields-provider)
