We're sorry Aspose doesn't work properply without JavaScript enabled.

# Page numbers of TOC are updated incorrectly using C#

Hi Team,

I have tried to update TOC of a document with the code snippet mentioned below

Document doc = new Document("D:\\Temp\\in.docx");

foreach (Field field in doc.Range.Fields)
{
if (field.Type == FieldType.FieldTOC)
{
FieldToc toc = (FieldToc)field;
toc.UpdatePageNumbers();
}
}

doc.Save("D:\\Temp\\18.7.docx");


But in the final generated document still the TOC page numbers not getting updated.
I am attaching the link for the word document for which i have tried this code.

https://easyupload.io/0w2m5c

In the document you can see the mismatch of page numbers i.e.:

6.2 Toxicity Management is on page number 28 but in TOC it is showing as page number 27.
Similarly the problem with the following topic numbers and the page references for others.

I have also try to update it with below method:

Document doc = new Document("C:/TOC_Project/Test1.docx");
doc.updateFields();


But still the page number is not getting updated.

Regards,
Uddeshya Pratik

@upratik

@tahir.manzoor

Hi Tahir,

Please find the attached zip file.

Regards,
Uddeshya PratikTOC_ASPOSE_SAMPLE.zip (53.2 KB)

@upratik

We have tested the scenario using the latest version of Aspose.Words for .NET 20.3 and have not found the shared issue. So, please use Aspose.Words for .NET 20.3. We have attached the output DOCX with this post for your kind reference. 20.3.zip (49.7 KB)

@tahir.manzoor

In the attached document you have shared ,i am still able to see the same issue.
I am highlighting the page number mismatch in the document and table of content and attaching the screenshot for your reference:

Regards,
Uddeshya Pratikpage1.PNG (91.0 KB)
Doc2.PNG (40.7 KB)