user1805885
user1805885

Reputation:

How to get Page/Sheet Count of Word/Excel documents?

In my project I have one requirement to show the number of pages in Word documents (.doc, .docx) files and number of sheets in Excel documents (.xls, .xlsx). I have tried to read the .docx file using Docx4j but the performance is very poor but I need just the word count and tried using Apache POI. I am getting an error, something like:

"trouble writing output: Too many methods: 94086; max is 65536. By package:" 

I want to know whether there is any paid/open source library available for android.

Upvotes: 3

Views: 1832

Answers (1)

vlsergey
vlsergey

Reputation: 264

There is just no way to show exact number of pages in MS Word file, because it will be different for different users. The exact number depends on printer settings, paper settings, fonts, available images, etc.

Still, you can do the following for binary files:

  • open file use POIFSFileSystem or NPOIFSFileSystem
  • extract only FileInformationBlock as it is done in the constructor HWPFDocumentCore
  • create DocumentProperties using information from FileInformationBlock as it is done in constuctor of HWPFDocument
  • get value of property cPg of DOP: DocumentProperties::getCPg()

The description of this field is: "A signed integer value that specifies the last calculated or estimated count of pages in the main document, depending on the values of fExactCWords and fIncludeSubdocsInStats."

For DOCX/XLSX documents you will need to access the same (I assume) property but using SAX or StAX methods.

Upvotes: 2

Related Questions