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

Free Support Forum - aspose.com

Aspose.Words Page Numbering Issue

I am generating a document in Aspose Words and have a different header for the first page. That works fine. I do not need a different footer for the first page, so I do a movetoheaderfooter to footer.first, then write my footer, then do a movetoheaderfooter to footer.primary, and write the same footer. The formatting is fine but the page numbering is off. The first footer has 1 of 3 and the second has 2 of 5 which is corrent. How do I get the first footer to have the correct total pages?

Hi Curtis,


Thanks for your inquiry. I think, you can achieve what you need by using the following code snippet:

DocumentBuilder builder = new DocumentBuilder();<o:p></o:p>

PageSetup ps = builder.PageSetup;

ps.DifferentFirstPageHeaderFooter = true;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);

builder.Write("Different first page header content");

builder.MoveToHeaderFooter(HeaderFooterType.FooterFirst);

builder.Write("Same footer content\t");

builder.InsertField("PAGE");

builder.Write(" of ");

builder.InsertField("NUMPAGES");

builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);

builder.Write("Primary header content");

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.Write("Same footer content\t");

builder.InsertField("PAGE");

builder.Write(" of ");

builder.InsertField("NUMPAGES");

// Move back to the main story of the first section.

builder.MoveToSection(0);

builder.Writeln("Text page 1.");

builder.InsertBreak(BreakType.PageBreak);

builder.Writeln("Text page 2.");

builder.InsertBreak(BreakType.PageBreak);

builder.Writeln("Text page 3.");

builder.InsertBreak(BreakType.PageBreak);

builder.Writeln("Text page 4.");

builder.InsertBreak(BreakType.PageBreak);

builder.Writeln("Text page 5.");

builder.Document.Save(@"c:\test\PageSetup.DifferentHeaders Out.docx");


I hope, this will help.


Best Regards,

I am doing essentially the same thing. It is organized only slightly diffrent, but I tried rearranging it to see if it would help but it didn’t. This is what I am doing… I am moving to the header.first writing the header, moving to header.primary, and writing the header. Then I move to document.start and write the body of the document. Then I move to footer.first and write the footer, then footer primary and write the footer. The first page has the wrong total number of pages.

I also noticed that when I open the saved document and click in the first page footer it corrects itself and shows 1 of 5. I tried linktoprevious.headerfooter(true) in various ways, but it didn’t work either.

Hi Curtis,

Thanks for your request. Yes, we are aware about this strange behavior. However, I am not sure this is a bug in Aspose.Words. MS Word updates PAGE and NUMPAGES fields in headers/footers automatically, but for some reason it updates NUMPAGES field improperly if it is in the first pages header/footer. The problem does not occur with primary headers/footers at all.

Anyways, this issue is logged in our defect database. We will investigate it and provide you more information.

Best regards,

OK, thanks Alexey. I hope you can find a solution for me.