java java中word文档中的页数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2848514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Number of pages in a word doc in java
提问by DD.
Is there an easy way to count the number of pages is a Word document either .doc or .docx?
有没有一种简单的方法来计算 .doc 或 .docx 的 Word 文档的页数?
Thanks
谢谢
回答by Robben_Ford_Fan_boy
You could try the Apache API for word Docs:
你可以试试 Apache API for word Docs:
It as a method for getting the page count:
它作为一种获取页数的方法:
public int getPageCount()
Returns: The page count or 0 if the SummaryInformation does not contain a page count.
返回: 页数,如果 SummaryInformation 不包含页数,则返回0。
回答by Diaes
I found a really cool class, that count Pages for Word, Excel and PowerPoint. With help of Apache POI. And it is for old doc and new docx.
我发现了一个非常酷的课程,可以计算Word、Excel 和 PowerPoint 的页面数。在 Apache POI 的帮助下。它适用于旧文档和新文档。
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;
}
source: OfficeTools.getPageCount()
回答by Snehal
Use Apache POI's SummaryInformationto fetch the Total page count of a MS word document
使用 Apache POI 的SummaryInformation获取 MS Word 文档的总页数
回答by Khalid Habib
//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();
回答by Zia
Document doc = new Document("C:\Data\abc.doc");
//Get page count
int pageCount = doc.getPageCount();
//Print Page Count
System.out.println(pageCount);
If you want to use Aspose.Words for Java, document.getPageCount() API will give you number of pages. Please check http://www.aspose.com/docs/display/wordsjava/com.aspose.words.Document.getPageCount+property
如果您想使用 Aspose.Words for Java,document.getPageCount() API 将为您提供页数。请检查http://www.aspose.com/docs/display/wordsjava/com.aspose.words.Document.getPageCount+property
or you may also use the docx4j api,
或者你也可以使用 docx4j api,

