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