• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java获取word文件页数的3种方式

武飞扬头像
林北北
帮助1

doc、docx页码跟系统字体,渲染方式有关,需要预览,或则转成pdf才能知道最终页数。

pdf页码是个属性,可以直接读取,获取比较容易。

1:poi获取

  1.  
    import java.io.File;
  2.  
    import java.io.FileInputStream;
  3.  
    import java.io.IOException;
  4.  
    import org.apache.poi.xwpf.usermodel.XWPFDocument;
  5.  
     
  6.  
    public class WordPageCount {
  7.  
    public static void main(String[] args) {
  8.  
    File file = new File("example.docx");
  9.  
     
  10.  
    try (FileInputStream fis = new FileInputStream(file);
  11.  
    XWPFDocument document = new XWPFDocument(fis)) {
  12.  
     
  13.  
    int pageCount = document.getProperties().getExtendedProperties().getPages();
  14.  
    System.out.println("Page count: " pageCount);
  15.  
     
  16.  
    } catch (IOException e) {
  17.  
    e.printStackTrace();
  18.  
    }
  19.  
    }
  20.  
    }
学新通

缺点:几乎不准,可以忽略。

2: spire.doc获取(相对准确,页数越多速度越慢,因为需要渲染,其它功能需要收费)

spire.doc是国内开发的一款软件,感觉也是基于poi
在windows和mac获取页码相对比较

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbkccc
系列文章
更多 icon
同类精品
更多 icon
继续加载