Getting total page count of a word document


#1

I wanted to count the total number of pages in a word document. What is the best way to do so?



thanks



Chintan Parikh


#2

Hi,

Thank you for considering Aspose.

The only way to do this at the moment is using BuiltInDocumentProperties.Pages:

int pageCount = doc.BuiltInDocumentProperties.Pages;

However, it might not return the proper value as it just returns the value written by Word which is not updated by Aspose.Word. We will improve this in the future.


#3

I tried this, and I'm getting the exception:

'Aspose.Word.DocumentProperties' does not contain a definition for 'Pages'

However I tried:

foreach(DocumentProperty dp in WordDoc.BuiltInDocumentProperties)
{
if(dp.Name == "Number of Pages")
{
NumPages = (int)dp.Value;
return;
}
}

But, it returns 1 every time on this document (even though it is 4 pages).


#4

Please check if you are using the latest version of Aspose.Word.

I've tried this kind of code:

string filename = Application.StartupPath + "\\Doc1.doc";

Document doc = new Document(filename);

MessageBox.Show(String.Format("Document has {0} pages", doc.BuiltInDocumentProperties.Pages));

and it does not produce any exceptions.

The number of pages returned by BuiltInDocumentProperties.Pages is indeed always 1. We shall check this issue. Thanks for reporting.


#5

Number of pages as well as other data returned by BuiltInDocumentProperties are taken from the document as stored by MS Word. Aspose.Word cannot calculate the number of pages.