2010-05-17 135 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