2010-05-17 93 views

回答

25

你可以尝试在Apache API为Word文档:

http://poi.apache.org/

它作为获取页计数的方法:

public int getPageCount() 

返回: 如果SummaryInformation不包含页面co,则页面计数为0或0 UNT。

10

的总页数我发现了一个非常酷的班,那算页,Excel和PowerPoint。借助Apache POI。它适用于旧的doc和新的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()

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(); 
+0

你能在图书馆说,这个文档类属于??? – 2018-02-26 14:18:12

+0

请检查代码中的评论 – 2018-03-01 15:44:24

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