Free Support Forum -

Facing problems with Page Numbering logic


I am using Aspose.Words to generate WORD document. The word document is a concat of many word docs.

Also i have a requirement which should print page number on each doc file like "Current Page Num of Total".

I am using the following code to achieve the above requirement.

I am able to achieve what is required, but the word doc is showing wrong number of pages at the bottom.

Suppose total number of pages are 89. Word doc is showing 87 of 87 when i am in last page.

Please hav a look @ the following code and let me know where am i going wrong.

Document ldocSrcDoc = new Document(_strTemplatePath + @"\OutPut_0.doc");

for (int i = 1; i <= lintTotalOutputPagesCnt; i++)


Document ldocSrcDoc1 = new Document(_strTemplatePath + @"\OutPut_" + i + ".doc");

DocumentBuilder lFinalDocBuilder1 = new DocumentBuilder(ldocSrcDoc1);



lFinalDocBuilder1 = null;

ldocSrcDoc.AppendDocument(ldocSrcDoc1, ImportFormatMode.KeepSourceFormatting);

ldocSrcDoc1 = null;


ldocSrcDoc.Save(_strTemplatePath + @"\Temp.doc");

ldocSrcDoc = null;

Document lFinalDoc = new Document(_strTemplatePath + @"\Temp.doc");

DocumentBuilder lFinalDocBuilder = new DocumentBuilder(lFinalDoc);



lFinalDoc.Save(_strTemplatePath + @"\Final.doc");



<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Why do not you use PAGE and NUMPAGEs fields?

// Insert page numbering text here.

// It uses PAGE and NUMPAGES fields to autocalculate current page number and total number of pages.

builder.Write("Page ");

builder.InsertField("PAGE", "");

builder.Write(" of ");

builder.InsertField("NUMPAGES", "");

Also, please see the following link:

MS Word automatically updates these fields during opening a document.

Best regards.

Hi Thanks for the reply.

My main problem was appending multiple documents. Page numbering is not a problem.

I tried your solution of using Fields but still WORD is showing 87/87 where as the actual created document contains 89.

If we calculate the number of pages manually the final appended document has 89 pages.

Please look at the code for appending documents which i hav sent and let me know if the appending logic of multiple documents is corect.

If it is not correct then please let me know how to append multiple documents. Here multiple documents can be any number so i was using for loop.

Please find the attached image to get a clear idea of issue.

I am facing the same problem in 2003 as well as 2007.

One more point to note is that we are facing this problem if the number of pages in the final doc is more than 70.



<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Your code is correct. Could you please also attach your input and output document here for testing or create a simple application, which will allow me to reproduce the problem? I will check the issue and provide you more information.

Best regards.


I am attaching the folder which contains all the individual docs.

Also i am attaching the template with which the individual docs are generated.

Let me know in case you require anything else.



Problem is solved a bit.

Now i hav used Fields "PAGE" AND "NUMPAGES" in my template it self. So when i am concatinating all the individual documents, the final document page numbers are getting updated automatically.

Now the problem is first page page number shows "1 of 1" until i do some action on the word like moving to last page or clicking on footer. Once i do one of these actions the first page numbering also gets updated.

Please let me know do i have to write any code to get this update automatically?

The problem still exists even after trying Documnets UpdatePageLayout(), UpdateTableLayout(), UpdateFields() methods just before saving the document.



<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information. It is perfect that you partially resolved the problem. I know that sometimes such problem occurs with the first page header/footer. For some reason MS Word does not update NUMPAGES fields there. This is not Aspose.Words issue; I think this is MS Word issue.

In case of using primary header/footer this problem never occurs.

Best regards.

Thanks for the response.