使用jacob进行word操作

由于项目需要,需要在后台对word文档中的书签进行重新填值,并进行打印。一开始已经使用Spire.Doc for Java实现了这个效果,但是这个插件是收费的,公司不想买,于是就在网上找了一段时间开源的插件,最终用了JACOB这个插件。下面记录一下实现的方法
1、使用准备

jacob下载地址:https://github.com/freemansoft/jacob-project/releases

2、使用方法

添加到项目:将jacob-1.18.jar复制到你的项目的类路径下,或是通过构建工具如Maven或Gradle的依赖管理添加。

bash 复制代码
     <dependency>
            <groupId>com.jacob</groupId>
            <artifactId>jacob</artifactId>
            <version>1.18</version>
     </dependency>

DLL放置:确保对应的.dll文件位于可访问的系统路径中,或者直接放在Java执行目录下,以便于JVM加载。

示例代码:在你的Java程序中,你可以开始使用Jacob提供的API来创建和操作COM对象,例如自动化Word文档处理等。

java 复制代码
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import lombok.extern.slf4j.Slf4j;

import java.io.File;

@Slf4j
public class WordConvertPdf {
    private static final int wdFormatPDF = 17; // PDF 格式

    public static void wordToPDF(String sourceFilePath, String targetFilePath) {

        log.info("启动 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", sourceFilePath).toDispatch();
            log.info("打开文档..." + sourceFilePath);
            log.info("转换文档到 PDF..." + targetFilePath);
            File tofile = new File(targetFilePath);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", targetFilePath, // FileName
                    wdFormatPDF);
            long end = System.currentTimeMillis();
            log.info("转换完成..用时:" + (end - start) + "ms.");

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

    public static void main(String[] args) {
        WordConvertPdf d = new WordConvertPdf();
        d.wordToPDF("/Users/xdai/项目/一体化/开发文档/技术文件/部署文件/template/外网/任务书封面封底-京.docx", "/Users/xdai/项目/一体化/开发文档/技术文件/部署文件/template/外网/上海骄成机电设备有限公司企业发展分析报告.pdf");
    }

}

注意事项

在生产环境中使用时,建议详细测试以保证兼容性和稳定性。

若遇到 UnsatisfiedLinkError错误,请确认.dll文件的位置是否正确,且系统架构与DLL匹配。

版权与许可:请遵循Jacob项目的相关许可协议使用此资源。

相关推荐
漫游者Nova1 天前
微软markitdown PDF/WORD/HTML文档转Markdown格式软件整合包下载
pdf·html·word·markdown·ppt
一只特立独行的兔先森1 天前
Go 语言 + Word 文档模板:WordZero 引擎如何让企业文档处理效率提升 300%?
word·办公自动化·开源项目·go语言·模板引擎
学软件的小铃铛2 天前
Microsoft Word使用技巧分享(本科毕业论文版)
word·毕业论文
Chasing Aurora2 天前
Word双栏英文论文排版攻略
论文阅读·word·sci·论文笔记
铭....4 天前
word批量导出visio图
开发语言·c#·word
CodeCraft Studio5 天前
国产化Word处理控件Spire.Doc教程:通过Java简单快速的将 HTML 转换为 PDF
java·html·word
碧海蓝天go星辰大海5 天前
word为跨页表格新加表头和表名
word
进阶的小木桩5 天前
C# 导出word 插入公式问题
开发语言·c#·word
碧海蓝天go星辰大海5 天前
word添加页眉
word
Dontla5 天前
word文档格式规范(论文格式规范、word格式、论文格式、文章格式、格式prompt)
prompt·word