使用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项目的相关许可协议使用此资源。

相关推荐
一叶龙洲13 小时前
Java中使用模板引擎(FreeMarker / Velocity) + Word XML导出复杂Word
xml·java·word
伟贤AI之路1 天前
为什么AI里的公式复制到Word格式会乱?
人工智能·word·latex
琪伦的工具库2 天前
批量DOCXPDFPPTX文档页拆分工具使用说明:每页拆分/每N页拆分/指定页码范围,支持导出日志
word
weixin_416660073 天前
从标记语言到 Word 文档:AI 生成的 Mermaid 与 LaTeX 自动化转换的技术方案解析
word·latex·数学公式·deepseek
昵称暂无13 天前
通过 C# 复制 Word 文档、指定段落、指定节
开发语言·c#·word
STRUGGLE_xlf5 天前
AI大模型生成表格粘贴到 Word 后出现双线边框的原因与解决方案
word
weixin_416660075 天前
2026 年 AI 对话转 Word 工具分析:Pandoc、Typora、aitoword 怎么选
人工智能·word
F_D_Z5 天前
Word Embedding :从分布式假设到神经网络语言模型
分布式·word·embedding
asdzx676 天前
C#:通过模板快速生成 Word 文档
开发语言·c#·word
xinixini6 天前
2026年马年日历模板大全 可编辑Excel/Word/PSD/PDF素材合集
pdf·word·excel·日历