是否有一种简单的方法来计算Word文档的页数是.doc还是.docx?
谢谢
您可以尝试使用Apache API for word Docs:
http://poi.apache.org/
它作为获取页数的方法:
public int getPageCount()
返回:页面计数,如果SummaryInformation不包含页面计数,则返回 0.
使用Apache POI的SummaryInformation来获取MS Word文档的总页数
我发现了一个非常酷的课程,它可以计算Word,Excel和PowerPoint的页数.借助Apache POI.它适用于旧文档和新docx.
String lowerFilePath = filePath.toLowerCase(); if (lowerFilePath.endsWith(".xls")) { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(lowerFilePath)); Integer sheetNums = workbook.getNumberOfSheets(); if (sheetNums > 0) { return workbook.getSheetAt(0).getRowBreaks().length + 1; } } else if (lowerFilePath.endsWith(".xlsx")) { XSSFWorkbook xwb = new XSSFWorkbook(lowerFilePath); Integer sheetNums = xwb.getNumberOfSheets(); if (sheetNums > 0) { return xwb.getSheetAt(0).getRowBreaks().length + 1; } } else if (lowerFilePath.endsWith(".docx")) { XWPFDocument docx = new XWPFDocument(POIXMLDocument.openPackage(lowerFilePath)); return docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages(); } else if (lowerFilePath.endsWith(".doc")) { HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(lowerFilePath)); return wordDoc.getSummaryInformation().getPageCount(); } else if (lowerFilePath.endsWith(".ppt")) { HSLFSlideShow document = new HSLFSlideShow(new FileInputStream(lowerFilePath)); SlideShow slideShow = new SlideShow(document); return slideShow.getSlides().length; } else if (lowerFilePath.endsWith(".pptx")) { XSLFSlideShow xdocument = new XSLFSlideShow(lowerFilePath); XMLSlideShow xslideShow = new XMLSlideShow(xdocument); return xslideShow.getSlides().length; }
来源:OfficeTools.getPageCount()
//Library is aspose //package com.aspose.words.* /*Open the Word Document */ Document doc = new Document("C:\\Temp\\file.doc"); /*Get page count */ int pageCount = doc.getPageCount();