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

Free Support Forum - aspose.com

Incorrect page numbering

After assembling a Word document I’m using:
UpdatePageLayout(); and UpdateFields();
to update all fields and table of contents.
However, the page numbering on the footer of each page is incorrect. It looks like this:
Page 1 of 2
Page 2 of 2
Page 3 of 2
Page 4 of 2
Page 5 of 1
Page 6 of 1
Page 7 of 2
Page 8 of 1
Page 9 of 1
Page 10 of 1
Page 11 of 1
Page 12 of 1
Page 13 of 1
Page 14 of 1
Page 15 of 1

Any ideas how to implement the correct page numbering to have Page x of 15?
Thanks,
thomas

@tpalmie

Could you please ZIP and attach your input and problematic output documents here for testing? We will investigate the issue and provide you more information on it.

SetContentControlTest.zip (9.1 MB)
Hi Tahir
Please find the attached example. What I’m trying to accomplish is having a continuous page numbering from the first to the last page. Please have a look at the assembled document “Summary.doc” in the “Docs” folder.
thanks,
thomas

@tpalmie

In your document, you are using SectionPages field. Please use NumPages field in the document for total number of pages

Document doc = new Document(MyDir + "input.docx");

DocumentBuilder builder = new DocumentBuilder(doc);
for (int i = 0; i < doc.Sections.Count; i++)
{
    builder.MoveToSection(i); 
    builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
    // Insert a PAGE field, which will display the number of the current page.
    builder.Write("Page ");
    builder.InsertField("PAGE", "");

    builder.Write(" of ");
    builder.InsertField("NUMPAGES", "");
}

doc.Save(MyDir + "20.12.docx");

Thanks Tahir.
This inserts a new page numbering.How can I update the “Page x of y” which is already defined in the file “Template.docx”?
thanks,
thomas

Also: how can I implement a consecutive page numbering?
Currently I have:
page 1-96, after page 5 it starts again page 1-96 and the table of contents is also updated (even after document.UpdateFields)
thanks,
thomas

@tpalmie

Your document contains multiple document sections. The PageSetup.RestartPageNumbering property is used to restart the page numbering for Section. Please iterate over all sections and set this property to false value as shown below.

Document doc = new Document(MyDir + "input.docx");

foreach (Section section in doc.Sections)
{
    section.PageSetup.RestartPageNumbering = false;
}

Your document contains the SectionPages field. Please move to the cursor to this field and insert NumPages field at its place as shown below. Hope this helps you.

Document doc = new Document(MyDir + "template.docx");
DocumentBuilder documentBuilder = new DocumentBuilder(doc);
foreach (Section section in doc.Sections)
{
    section.PageSetup.RestartPageNumbering = false;

    foreach (HeaderFooter hf in section.HeadersFooters)
    {
        foreach (var field in hf.Range.Fields.ToArray<Field>())
        {
            if (field.Type == FieldType.FieldSectionPages)
            {
                documentBuilder.MoveToField(field, false);
                documentBuilder.InsertField(FieldType.FieldNumPages, false);
                field.Remove();
            }
        }
    }
}

doc.UpdateFields();
doc.Save(MyDir + "20.12.docx");