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

相关推荐
寒山李白1 天前
解决 python-docx 生成的 Word 文档打开时弹出“无法读取内容“警告
python·word·wps·文档·docx·qoder
AI进化营-智能译站2 天前
ROS2 C++开发系列01:在ROS2上编写第一个C++ hello word
开发语言·c++·ai·word
gc_22993 天前
学习C#调用OpenXml操作word文档的基本用法(30:学习日期相关类)
word·openxml·日期
gc_22993 天前
学习C#调用OpenXml操作word文档的基本用法(28:学习文本类)
word·文本·text·openxml
2501_907136823 天前
HandyWrite Pro - word/excel转手写工具
word·软件需求
Eiceblue3 天前
C# 如何实现 Word 转 Excel ?分享两种实用方法
c#·word·excel
天才少女爱迪生3 天前
word格式规范检测+自动修改【python】
python·c#·word
gc_22993 天前
学习C#调用OpenXml操作word文档的基本用法(29:学习中断类)
word·中断·openxml·break
梅孔立3 天前
Aspose.Words Java 表格动态删列、合并列、表头重建、全局字体统一解决方案
java·开发语言·word·aspose·在线编辑
爱叨叨的小嘟4 天前
Latex公式 转 word可编辑公式
word·typora·latex