java获取word页数_java中word doc中的页数

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提取Word文档的页数,可以通过使用Apache POI库来实现。Apache POI是一种开源的Java API,用于读取和写入Microsoft Office格式的文件,包括Word。 首先,需要通过创建一个Word文档对象来打开要提取页数Word文件。可以使用`XWPFDocument`类来实现这个目的。以下是一个示例代码: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class WordPageExtractor { public static void main(String[] args) { try { File file = new File("path/to/word/document.docx"); FileInputStream fis = new FileInputStream(file); XWPFDocument document = new XWPFDocument(fis); int numberOfPages = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages(); System.out.println("Word文档的页数:" + numberOfPages); document.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上述代码,需要将`"path/to/word/document.docx"`替换为要提取页数Word文档的文件路径。程序将读取文件,创建`XWPFDocument`对象,并使用该对象的`getProperties() -> getExtendedProperties() -> getUnderlyingProperties().getPages()`方法来获取页数,最后输出结果。 需要注意的是,这段代码仅适用于DOCX格式的Word文档。对于旧的DOC格式的Word文档,可以使用`HWPFDocument`类和其相应的方法来提取页数。 以上是用Java提取Word文档的页数的简单示例,请根据具体的需求和文档类型进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值