高性能实现WORD转PDF(jacob1.19+SaveAsPDFandXPS)内附资源链接

高性能实现WORD转PDF(jacob1.19+jacob-1.19-x64.dll+SaveAsPDFandXPS)

1. 引入jacob依赖

		<dependency>
			<groupId>net.sf.jacob-project</groupId>
			<artifactId>jacob</artifactId>
			<version>1.19</version>
		</dependency>

2. 安装SaveAsPDFandXPS.exe

下载地址: 链接:https://pan.baidu.com/s/1MaTFCueyGBvIZfRd88wn7Q

提取码:8a6s
在这里插入图片描述

3.下载jacob-1.19-x64.dll 放到jdk/jre bin目录下

下载地址: https://download.youkuaiyun.com/download/helloWorld_ma/12254776

4. 网上抄来抄去的demo代码

package com.pxkj.util;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

public class WordToPdf {

    private static final int wdFormatPDF = 17; // PDF 格式

    public void wordToPDF(String sfileName, String toFileName) {

        System.out.println("启动 Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到 PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", toFileName, // FileName
                    wdFormatPDF);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时:" + (end - start) + "ms.");

        } catch (Exception e) {
            System.out.println("========Error:文档转换失败:" + e.getMessage());
        } finally {
            Dispatch.call(doc, "Close", false);
            System.out.println("关闭文档");
            if (app != null)
                app.invoke("Quit", new Variant[]{});
        }
        // 如果没有这句话,winword.exe进程将不会关闭
        ComThread.Release();
    }

    public static void main(String[] args) {
        WordToPdf d = new WordToPdf();
        d.wordToPDF("F:\\test.docx", "F:\\test.pdf");
    }
}

200k的work,大约需要3000ms,包含图片,效果还ok
在这里插入图片描述

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值