UpdatePageNumbers updates page numbers incorrectly using C#

@tahir.manzoor

Hi Tahir,

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.

Your solution on this will be really helpful for me.

Regards,
Uddeshya Pratik

@upratik

We have already answered your query here in this post. Please follow that thread for further proceedings.