Apache Poi:获取 DOC 文档中的页数 [英] Apache Poi: get page count in DOC document

查看:112
本文介绍了Apache Poi:获取 DOC 文档中的页数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Apache Poi 获取 DOC 文档中的页数?

How to get page count in DOC document using Apache Poi?

我尝试使用以下代码:

HWPFDocument wordDoc = new HWPFDocument(new FileInputStream(lowerFilePath));
Integer pageCount = wordDoc.getSummaryInformation().getPageCount();

但出现异常(Apache Poi 版本:3.13)

But got exception (version of Apache Poi: 3.13)

    java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray(Ljava/io/InputStream;I)[B
at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:95)
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174)

推荐答案

您的代码应该可以正常工作.这个非常常见的 POI 错误的原因是您的类路径上的库的旧版本,其中该方法尚不存在.而且库中的某些版本的零件也不兼容.

Your code should work properly. The reason of this very common POI error is that an older version of the library on your classpath in which the method didn't exist yet. And also some versions of parts from the library are incompatible.

如果您使用 maven,则这段代码只需要这些依赖项:

If you use maven you need only these dependencies for this piece of code:

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>${poi.version}</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-scratchpad</artifactId>
  <version>${poi.version}</version>
</dependency>

确保您没有额外版本的 jar.

Make sure you do not have extra versions of jars.

这篇关于Apache Poi:获取 DOC 文档中的页数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆