java中单词doc中的页数

时间:2010-05-17 11:16:02

标签: java ms-word

有一种简单的方法来计算Word文档的页数是.doc还是.docx?

由于

5 个答案:

答案 0 :(得分:25)

您可以尝试使用Apache API for word Docs:

http://poi.apache.org/

它是获取页数的方法:

public int getPageCount()

<强>返回: 页面计数,如果SummaryInformation不包含页数,则为0。

答案 1 :(得分:11)

使用Apache POI的SummaryInformation来获取MS Word文档的总页数

答案 2 :(得分:11)

我找到了一个非常酷的课程,计算 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()

答案 3 :(得分:5)

  //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();

答案 4 :(得分:2)

Document doc = new Document("C:\\Data\\abc.doc");     

//Get page count                                         
int pageCount = doc.getPageCount();

//Print Page Count            
System.out.println(pageCount);

如果您想使用Aspose.Words for Java,document.getPageCount()API将为您提供页数。请检查http://www.aspose.com/docs/display/wordsjava/com.aspose.words.Document.getPageCount+property

或者您也可以使用docx4j api,

http://www.docx4java.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/samples/DocProps.java