java 使用 jacob 转换 pdf

原料 jacob 、office 2010 、 windows Server 2008R2 及以上(当前测试环境 2008 windows Server 2019)

1、jacob 版本 1.20

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

2、java 版本 1.8

推荐国产 jdk

下载地址 https://github.com/alibaba/dragonwell8/releases

3、将 jacob-1.20.xx.dll 放入 JAVA_HOME/bin 目录

image

4、安装 office 2010 或以上版本 最好是2010版本 其他版本未测试过

5、常见错误

image

com.jacob.com.ComFailException: VariantChangeType failed

尝试方法1

win2008 以下 需要在

C:\Windows\System32\config\systemprofile\Desktop

建立文件夹

win2008 及以上 需要在

C:\Windows\SysWOW64\config\systemprofile\Desktop

建立文件夹

尝试方法2(尝试方法1 还是失败的情况下)

解决计算机的com交互组件没有启用.

解决方案:

1) 运行命令: mmc comexp.msc -32

2) 找到:组建服务>计算器>我的计算机>DCOM组件>Microsoft Word 97-2003 文檔;

3) 右键点击,选择属性,修改标识为“交互式用户”,点击“确定”;

6、资料

源码参考 https://gitee.com/xdoc/xoffice

相关服务参考 https://view.xdocin.com

7、主要代码示例


 public static void toPdf(String src, String tar) {
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            app.setProperty("AutomationSecurity", new Variant(3));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open",
                    new Object[] { src, Boolean.FALSE, Boolean.TRUE })
                    .toDispatch();
            Dispatch.call(doc, "SaveAs",
                    new Object[] { tar, Integer.valueOf(17) });
        } catch (Throwable t) {
          
        } finally {
            if (doc != null) {
                try {
                    Dispatch.call(doc, "Close", new Object[] { Boolean.FALSE });
                } catch (Throwable t) {
                    l 
                }
            }
            if (app != null) {
                try {
                    app.invoke("Quit", new Variant[] {new Variant(false)});
                    app.safeRelease();
                } catch (Throwable t) {
                 
                }
            }

        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容