List of usage examples for org.apache.poi.xwpf.extractor XWPFWordExtractor getExtendedProperties
public ExtendedProperties getExtendedProperties()
From source file:rocky.sizecounter.SizeCounterUtil.java
License:Apache License
/** * Count Word's number of page from input directory. * //from w w w.j a va 2 s . c om * @param filePath . * @return Number of A4 pages */ public static int countWordFile(String filePath) { FileInputStream fis = null; int page = 0; try { fis = new FileInputStream(filePath); if (CommonUtil.getExtension(filePath).equals("doc")) { // When file is .DOC HWPFDocument doc = new HWPFDocument(fis); page = doc.getDocProperties().getCPg(); } else if (CommonUtil.getExtension(filePath).equals("docx")) { // When file is .DOCX XWPFDocument doc = new XWPFDocument(fis); XWPFWordExtractor ex = new XWPFWordExtractor(doc); page = ex.getExtendedProperties().getUnderlyingProperties().getPages(); } } catch (FileNotFoundException ex) { LOG.warn("File " + filePath + " not found", ex); } catch (IOException ex) { LOG.warn("Invalid when reading file.", ex); } catch (Exception ex) { LOG.warn("Can not count file " + filePath, ex); } finally { if (fis != null) { try { fis.close(); } catch (IOException ex) { LOG.warn("Close the file input stream", ex); } } } return page; }