统计word页数


前段时间接手一个项目要求导出word打印双面,由于导出的word是由多个word拼接而成,客户又要求每个模块的第一页必须在上面,所以必须将每一个word的页数统计出来,当word页数为奇数时则新增一个空白页,所以简单的吧统计word的页数错误方法和可使用方法罗列一些
错误方式:
1、

	Document document=new Document("C:\\Users\\73909\\Desktop\\temporary.docx");
    int pageCount = document.getPageCount();

2、

	FileInputStream in = new FileInputStream("C:\\Users\\73909\\Desktop\\temporary.docx");
    OPCPackage open = OPCPackage.open(in);
    XWPFDocument document = new XWPFDocument(open);
    int pages = document.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();

3、

 	Document document=new Document("C:\\Users\\73909\\Desktop\\temporary.docx");
    int pages = document.getBuiltInDocumentProperties().getPages();

这三种方式有时候把我两页的word统计成一页有时候统计成三页不建议使用,可以尝试使用下面三种方式

方式一:Spire.Doc

Maven:
<dependency>
      <groupId> e-iceblue </groupId>
      <artifactId>spire.doc.free</artifactId>
      <version>5.2.0</version>
</dependency>
配置镜像
<repositories>
    <repository>
      <id>com.e-iceblue</id>
      <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
Gradle:implementation "e-iceblue:spire.office.free:5.2.0"
配置镜像
maven {
        url "https://repo.e-iceblue.cn/repository/maven-public/"
    }
	import com.spire.doc.Document;

	public class DemoAction {
	  public static void main(String[] args)  {
	    Document d=new Document("C:\\Users\\73909\\Desktop\\temporary.docx");
	    int pageCount = d.getPageCount();
	    System.err.println("docx页数:"+pageCount);
	  }
	}

方式二:jacob

此方式需要用到office组件,需要在jdk的bin中放入适合电脑系统的
jacob-1.14.3-x64.dll

依赖:
<dependency>
  <groupId>net.sf.jacob-project</groupId>
  <artifactId>jacob</artifactId>
  <version>1.14.3</version>
</dependency>
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class DemoAction {
  public static void main(String[] args){
    // 建立ActiveX部件
    ActiveXComponent wordCom = new ActiveXComponent("Word.Application");
    //word应用程序不可见
    wordCom.setProperty("Visible", false);
    // 返回wrdCom.Documents的Dispatch
    Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
    // 调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc
    Dispatch wordDoc = Dispatch.call(wrdDocs, "Open", "C:\\Users\\73909\\Desktop\\temporary.docx", false, true, false).toDispatch();
    Dispatch selection = Dispatch.get(wordCom, "Selection").toDispatch();
    int pages = Integer.parseInt(Dispatch.call(selection,"information",4).toString());
    System.err.println("docx页数:"+pages);
    //关闭文档且不保存
    Dispatch.call(wordDoc, "Close", new Variant(false));
    //退出进程对象
    wordCom.invoke("Quit", new Variant[] {});
  }
}

方式三:先将docx转成pdf,然后统计PDF页数

依赖:
<dependency>
  <groupId>org.apache.pdfbox</groupId>
  <artifactId>pdfbox</artifactId>
  <version>2.0.24</version>
</dependency>
<dependency>
  <groupId>com.aspose</groupId>
  <artifactId>aspose-words</artifactId>
  <version>18.10</version>
</dependency>

aspose-words-15.8.0-jdk16.jar生成PDF会生成水印所以我们需要先把水印给它去掉
license.xml

<?xml version="1.0" encoding="UTF-8" ?>
<License>
    <Data>
        <Products>
            <Product>Aspose.Total for Java</Product>
            <Product>Aspose.Words for Java</Product>
        </Products>
        <EditionType>Enterprise</EditionType>
        <SubscriptionExpiry>20991231</SubscriptionExpiry>
        <LicenseExpiry>20991231</LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
    </Data>
    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
</License>
import com.aspose.words.License;
import java.io.IOException;
import java.io.InputStream;

public class Word2PdfAsposeUtil {
    public static boolean getLicense(InputStream inputStream) {
        boolean result = false;
        InputStream is = null;
        try {
            is = inputStream;
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.pdfbox.pdmodel.PDDocument;

public class DemoAction {

  public static void main(String[] args) throws Exception {
    //指定license.xml
    InputStream xmlIn = new FileInputStream("*****\\license.xml");
    // 破解spiredoc水印
    Word2PdfAsposeUtil.getLicense(xmlIn);
    Document d=new Document("C:\\Users\\73909\\Desktop\\temporary.docx");
    //创建空白pdf
    FileOutputStream out=new FileOutputStream("C:\\Users\\73909\\Desktop\\temporary.pdf");
    //将word内容添加到pdf
    d.save(out,SaveFormat.PDF);
    //加载PDF得到PDF对象
    PDDocument pdDocument = PDDocument.load(new File("C:\\Users\\73909\\Desktop\\temporary.pdf"));
    int pageNum = pdDocument.getNumberOfPages();
    System.err.println("docx页数:"+pageNum);
  }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值