统计word页数

统计word页数,第1张

统计docx页数
    • 方式一:Spire.Doc
    • 方式二:jacob
    • 方式三:先将docx转成pdf,然后统计PDF页数

前段时间接手一个项目要求导出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:

       e-iceblue 
      spire.doc.free
      5.2.0

配置镜像

    
      com.e-iceblue
      http://repo.e-iceblue.cn/repository/maven-public/
    

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

依赖:

  net.sf.jacob-project
  jacob
  1.14.3

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页数
依赖:

  org.apache.pdfbox
  pdfbox
  2.0.24


  com.aspose
  aspose-words
  18.10

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


<License>
    <Data>
        <Products>
            <Product>Aspose.Total for JavaProduct>
            <Product>Aspose.Words for JavaProduct>
        Products>
        <EditionType>EnterpriseEditionType>
        <SubscriptionExpiry>20991231SubscriptionExpiry>
        <LicenseExpiry>20991231LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7SerialNumber>
    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);
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/737719.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存