Page Number is not generating properly in footer after removing blank pages in word document

Hi Team,

After removing blank pages in word document, page number is not generating properly in footer. some page numbering was skipped. example 1, 2, 4, 5, etc.


Aspose.Words.Document doc = originalDocToGeneratePageNumber;
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Font.Name = "Arial";
builder.Font.Size = 8;
builder.PageSetup.PageStartingNumber = 1;
builder.InsertField(FieldType.FieldPage, false);
doc.Save(FullFileName);

can you help me to solve this issue.

@PrakashAon Could you please attach your input and output documents here for testing along with code that will allow us to reproduce the problem on our side? We will check the issue and provide you more information.

If you noticed the output folder, page no 4 got skipped in footer.
Aspose Support.zip (112.9 KB)

@PrakashAon This occurs because page starting number is set explicitly in the last section. You can rest this using the following code:

Document doc = new Document(@"C:\Temp\in.docx");
foreach (Section s in doc.Sections)
    s.PageSetup.RestartPageNumbering = false;
doc.Save(@"C:\Temp\out.docx");

post adding this lines in code, generated attached file as output file.
ST-360i_100030_EP(IS)_31-May-2024_184053.docx (34.7 KB)

 static void GeneratePageNumber(Aspose.Words.Document originalDocToGeneratePageNumber, string FullFileName)
 {
     Aspose.Words.Document doc = originalDocToGeneratePageNumber;
     DocumentBuilder builder = new DocumentBuilder(doc);
     builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
     builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
     builder.Font.Name = "Arial";
     builder.Font.Size = 8;
     builder.PageSetup.PageStartingNumber = 1;
     builder.InsertField(FieldType.FieldPage, false);

     foreach (Section s in doc.Sections)
         s.PageSetup.RestartPageNumbering = false;
     
     doc.Save(FullFileName);
 }

@PrakashAon In your code you insert additional PAGE field in the footer. So there are two PAGE fields in the footer that shows the same value: