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 t
Solution 1:
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.
Post a Comment for "How To Get Page/sheet Count Of Word/excel Documents?"