Incorrect page numbers in Table of Contents. Not correct even after updating fields

Hello Aspose team,

We are having issues with a customer document where the Table of Content’s page numbers do not match the actual page numbers of the chapters.

This issue persists even after we call the UpdateFields() method on the document.

I have tried other routes as well, such as explicitly calling the UpdatePageNumbers() method on the Table of Content field. Unfortunately, none of them helped.

For your inspection, I have attached a zip folder containing the problem document and a test console app which demonstrates the UpdateFields() method not updating the table of contents page numbers.

Check the page number of the “General Notes” chapter, it is indicated to be on page 17 in the ToC, but its actually in page 13. The console app calls the UpdateFields() method on the document, but this does nothing.

Please let me know if any more information is needed from our end, I look forward to your reply.

Best regards.

https://1drv.ms/u/s!bairqzs5koirzyh1qqb2sqjtc4a5?e=wzl1sempvkyr7p52vfghxw&at=9

@manikya.rao

I just run the following code the page field were updated correctly:

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

The input and output files:
IncorrectPageNumber_input.docx (945.9 KB)
IncorrectPageNumber_output.docx (945.9 KB)

I am using Aspose Words 23.2.

Which version are you using?

Maybe if you upgrade your version the problem will be solved.

Hi Carlos,

Unfortunately, even after updating to 23.2 it still did not work. Please see the attached screenshot that shows the code and the Aspose.Words version number

image.png (78.2 KB)

I have uploaded the updated console App (with v23.2 and the new code) and linked it below

[AsposeIssue-TocUpdateNotWorking-Compressed-2.zip ]https://1drv.ms:443/u/s!bairqzs5koirz04wklyb6gg_jh_v?e=s47tgyeg8eglqfv37f3ipq&at=9

Can you please try with the console app and see if it changes the result?

I just did and the code works.

static void Main(string[] args)
{
    var asposeWordsLicense = new Aspose.Words.License();
    asposeWordsLicense.SetLicense("Aspose.Total.lic");

    // Document One: UpdateFields does not update the table of content page numbers.
    // For reference, "General Notes" page number is shown 17 in ToC but its actually in page 13

    string documentOneFile = @"../../../Toc-Does-Not-Update - Copy.docx";

    var document = new Document(documentOneFile);

    //document.UpdateFields();

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

Toc-Does-Not-Update - Copy.docx (945.9 KB)

You can see the time when was edited:

Pages updated correctly:

Hello Carlos, thank you for trying.

It’s strange that its working there but not in my machine.
I have attached the recording of my attempt below:

[AsposeIssue-Recording.mp4 ]https://1drv.ms:443/v/s!bairqzs5koirz1cj2avjk3ocg-db?e=fx2ladvpwuuwq8gatzrrag&at=9

Is there a configuration, language or environment setting that we need to be aware of for this to work?

FYI the microsoft word version I have is: Microsoft® Word for Microsoft 365 MSO (Version 2208 Build 16.0.15601.20446) 64-bit

@manikya.rao The problem might occur because the fonts used in your document are not available on the machine where document is processed. Please note Aspose.Words needs the fonts used in the document to build document layout upon updating TOC. If Aspose.Words cannot find the font used in the document, the font is substituted. This might lead into fonts mismatch and document layout differences due to the different fonts metrics, this in turn can cause incorrect page numbers in the TOC. You can implement IWarningCallback to get notifications when font substitution is performed.

FYI: @carlos.molina

1 Like

Hi @alexey.noskov and @carlos.molina ,

Using the IWarningCallback I found that my machine had the Hebrew fonts ‘David’ and ‘Narkisim’ missing. After installing them and then using UpdatePageNumbers(), the ToC successfully updated. I also tried with UpdateFields() and it worked there as well.

Thank you for your assistance

1 Like