栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

统计word页数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

统计word页数

统计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



    
        
            Aspose.Total for Java
            Aspose.Words for Java
        
        Enterprise
        20991231
        20991231
        8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7
    
    sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=

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);
  }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/837302.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号