Show the exact page number calculated

Good morning,

I am working on a C# program to analyze the page number displayed on a Word document to check its validity. To do so, I analyze the FieldPage contained in each page, and I obtain their result with DisplayResult. My code works well when there is only one section in the document:

Document doc = new Document(@"....docx");
doc.UpdateFields();

List<FieldPage> fieldPageList = new List<FieldPage>();
for (int i = 0; i < doc.PageCount; i++)
{
    Document pageDoc = doc.ExtractPages(i, 1);
    pageDoc.UpdateFields();
    foreach (Paragraph paragraph in pageDoc.GetChildNodes(NodeType.Paragraph, true))
    {
        foreach (Field field in paragraph.Range.Fields)
        {
            if (field.Type == FieldType.FieldPage)
            {
                FieldPage fieldPage = (FieldPage)field;
                fieldPageList.Add(fieldPage);
                Console.WriteLine(
                    $"page {i + 1} : {fieldPage.GetFieldCode()}, {fieldPage.Result}, {fieldPage.DisplayResult}, {fieldPage.IsDirty}");
            }
        }
    }
}

However, when there are several sections, this method does not return the correct page numbers at all. What I would like is to be able to extract the page number as it is displayed on screen in the document for the user.

CompletForForum.docx (34,9 Ko)

In the document I am attaching, the DisplayResult shows me the following information for each page:

Page 1 → displays 3, when nothing should be displayed.
Page 2 → displays 3, when nothing should be displayed.
Page 3 → displays 3, correct.
Page 4 → displays 4, correct.
Pages 5, 6, 7 → display nothing, correct.

The page 1 is in Section 1.
The pages 2, 3 are in Section 2.
The page 4 is in Section 3.
The page 5 is in Section 4.
The pages 6, 7 are in Section 5.

I also tried to display the plain text content of the paragraphs, via paragraph.ToString(SaveFormat.Text), but it gives me the same results as DisplayResult.

Do you have any ideas on how I can get my C# program to display exactly what the user sees in his Word document, please?

Thank you very much !
Best regards

@BlackSea You can use the following code:

Document doc = new Document(@"C:\Temp\in.docx");
for (int i = 0; i < doc.PageCount; i++)
{
    Document pageDoc = doc.ExtractPages(i, 1);
    Console.WriteLine(pageDoc.FirstSection.PageSetup.PageStartingNumber);
}

@alexey.noskov
Thank you very much for your reply!

However, when I use this code, I get the following result:
1
2
3
4
5
6
7
So it sends me the numbers that should normally be displayed on the screen. However, in the document I sent, the page numbers for pages 1, 2, 5, 6 and 7 are not displayed. The aim of my program is to detect that the original document does not have the page numbers written in it.
Do you have any ideas on how I can find out exactly what is being displayed?

Thank you again,
Best regards

@BlackSea You can modify the code like this:

Document doc = new Document(@"C:\Temp\in.docx");
for (int i = 0; i < doc.PageCount; i++)
{
    Document pageDoc = doc.ExtractPages(i, 1);
    pageDoc.Save($@"C:\Temp\page{i + 1}.docx");
    // Page field can be in the main document body or in the header/footer
    // The section can contain several headers/footers
    // Check whether main body contains PAGE field
    bool hasPageFieldInBody = pageDoc.GetChildNodes(NodeType.Body, true).Cast<Body>()
        .Any(b => b.Range.Fields.Any(f => f.Type == FieldType.FieldPage));

    // Check whether header/footer displaid on the first page has PAGE field
    // This might be either primary or field page header/footer.
    HeaderFooterType[] firstPageDisplayedHeaderFooterTypes = pageDoc.FirstSection.PageSetup.DifferentFirstPageHeaderFooter ?
        new HeaderFooterType[] { HeaderFooterType.HeaderFirst, HeaderFooterType.FooterFirst } :
        new HeaderFooterType[] { HeaderFooterType.HeaderPrimary, HeaderFooterType.FooterPrimary };
    bool hasPageFieldInHeaderFooter = pageDoc.FirstSection.HeadersFooters.Cast<HeaderFooter>()
        .Where(hf => firstPageDisplayedHeaderFooterTypes.Contains(hf.HeaderFooterType))
        .Any(hf => hf.Range.Fields.Any(f => f.Type == FieldType.FieldPage));


    if (hasPageFieldInBody || hasPageFieldInHeaderFooter)
    {
        Console.WriteLine(pageDoc.FirstSection.PageSetup.PageStartingNumber);
    }
    else
    {
        Console.WriteLine($"There is no PAGE field in the page {i + 1}");
    }
}

But in the extracted second page there is footer with PAGE field.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27351

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@alexey.noskov
Thank you very much !

1 Like

The issues you have found earlier (filed as WORDSNET-27351) have been fixed in this Aspose.Words for .NET 24.10 update also available on NuGet.