Java word转pdf 精确获取文件页数(jacob)

注意: 该项目需在windows下进行, 如果需要商用需准备Windows服务器

这里我们用到的工具是jacob 需要创建一个maven项目添加以下依赖

        <dependency>
            <groupId>net.sf.jacob-project</groupId>
            <artifactId>jacob</artifactId>
            <version>1.14.3</version>
            <scope>compile</scope>
        </dependency>

下一步就是配置我们的jacob

1.首先找到我们的jre安装目录, 我这里的目录是 C:\Program Files\Java\jdk1.8.0_241\jre\bin
在这里插入图片描述

然后将这两个文件复制到目录中

  1. jacob-1.14.3-x64.dll
  2. jacob-1.14.3-x86.dll
jacob配置文件下载地址
链接:https://pan.baidu.com/s/1PLvmjzRu8nbQAOohqcd0uw 
提取码:pd1o

在这里插入图片描述

完成以上的这些操作就可以直接开始编写代码进行页数获取

package com.yysd.pdfutil.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpUtil;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.apache.pdfbox.pdmodel.PDDocument;

import java.io.*;

/**
 * @author dell
 */
public class Word2PdfUtil {

    // 不保存待定的更改。
    static final int wdDoNotSaveChanges = 0;
    // word转PDF 格式
    static final int wdFormatPDF = 17;


    /**
     * 测试main方法
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // 需要传入的 word 路径可以是本机 也可以是网络路径
        Word2PdfUtil.word2pdf("http://8e2a-60-216-158-158.ngrok.io/aaaa.docx","C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");
//      Word2PdfUtil.word2pdf("C:\\Users\\dell\\Desktop\\wordpage\\web.docx","C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");

        String fileUrl  = "http://6820-58-56-64-2.ngrok.io/aaaa.docx";
        //截取发送过来的文件名
        System.out.println(fileUrl.substring(fileUrl.lastIndexOf("/") + 1, fileUrl.length() - 1));

        //将文件下载后保存在D盘,返回结果为下载文件大小
        long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("D:\\file"));

        System.out.println("Download size: " + size);

        //进行Md5加密
        System.out.println("加密后: "+DigestUtil.md5Hex(new FileInputStream("D:\\file\\aaaa.docx")));

//      创建需要解析的PDF文件
        File file = new File("C:\\Users\\dell\\Desktop\\wordpage\\web.pdf");
//      加载文件
        PDDocument doc = PDDocument.load(file);
//      通过工具类获取页数,打印
        System.out.println("页数为 : "+doc.getNumberOfPages());

    }


    /**
     *   工具类
     * inputStream 转 File
     */
    public static File inputStreamToFile(InputStream ins, String name) throws Exception{
        File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
        if (file.exists()) {
            return file;
        }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 1024;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }


    public static boolean word2pdf(String source, String target) {
        System.out.println("Word转PDF开始启动...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);
            Dispatch docs = app.getProperty("Documents").toDispatch();
            System.out.println("打开文档:" + source);
            Dispatch doc = Dispatch.call(docs, "Open", source, false, true).toDispatch();
            System.out.println("转换文档到PDF:" + target);
            File tofile = new File(target);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", target, wdFormatPDF);
            Dispatch.call(doc, "Close", false);
            long end = System.currentTimeMillis();
            System.out.println("转换完成,用时:" + (end - start)/1000 + "秒");
            return true;
        } catch (Exception e) {
            System.out.println("Word转PDF出错:" + e.getMessage());
            return false;
        } finally {
            if (app != null) {
                app.invoke("Quit", wdDoNotSaveChanges);
            }
        }
    }
}

demo项目上传到gitee 地址:
https://gitee.com/programmer-k/pdfutil.git

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Max恒

为了开源加油 ! !

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值