aspose.words读取文档页数(不准确?)_aspose 获取页数_爱琴孩的博客-CSDN博客

前言

在对word操作中,经常需要获取文档页数,这里采用aspose.words来解析文档,获取文档页数。下面几种方式都是基于aspose.words的19.10版本,其他版本可能略有不同。

        <dependency>            <groupId>com.aspose</groupId>            <artifactId>aspose-words</artifactId>            <version>19.10</version>            <type>pom</type>        </dependency>

方式一

Document doc = new Document(path);int pages = doc.getPageCount();

这种通过doc.getPageCount()来获取word页数在windows环境还可以,但是部署到Linux服务器上获取的页码差别就很大了。

方式二

  Document doc = new Document(path);  LayoutCollector layoutCollector = new LayoutCollector(doc);  int numPage = 0;  NodeCollection runs = doc.getChildNodes(NodeType.PARAGRAPH, true);  for (int i = 0; i < runs.getCount(); i++) {      Node r = runs.get(i);      numPage = layoutCollector.getEndPageIndex(r);  }  return numPage;

这种方式是通过解析段落,然后通过最后一段的所属页码来得到文档的总页数。这种方式同第一种方式,windows环境还可以,但是部署到Linux服务器上获取的页码差别就很大了。

方式三

Document doc = new Document(path);int pages = doc.getBuiltInDocumentProperties().getPages();

这种方式在windows和Linux环境下测试,页数都是准确的。上面的三种方式,大家都可以试试。能获取到最准确页数即可。


原网址: 访问
创建于: 2023-05-09 20:24:53
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论