jacob操作ppt报错

发布于 2021-12-02 13:44:21 字数 3985 浏览 797 评论 1

private boolean ppt2PDF(String inputFile, String pdfFile) {
        ComThread.InitSTA();

        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch ppt = null;
        try {
            app = new ActiveXComponent("PowerPoint.Application");// 创建一个PPT对象
            //app.setProperty("Visible", new Variant(true)); // 不可见打开(PPT转换不运行隐藏,所以这里要注释掉)
            //app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch ppts = app.getProperty("Presentations").toDispatch();// 获取文挡属性

            System.out.println("打开文档 >>> " + inputFile);
            // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
            ppt = Dispatch.call(ppts, "Open", inputFile, 
                    true,// ReadOnly
                    true,// Untitled指定文件是否有标题
                    false// WithWindow指定文件是否可见
                    ).toDispatch();
            
            System.out.println("转换文档 [" + inputFile + "] >>> [" + pdfFile + "]");
            Dispatch.call(ppt, "SaveCopyAs", pdfFile, ppFormatPDF);
            //Dispatch.call(ppt, "ExportAsFixedFormat", pdfFile, ppFormatPDF);
            long end = System.currentTimeMillis();

            System.out.println("用时:" + (end - start) + "ms.");

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("========Error:文档转换失败:" + e.getMessage());
        } finally {
            Dispatch.call(ppt, "Close");
            System.out.println("关闭文档");
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        ComThread.Release();
        ComThread.quitMainSTA();
        return false;
    }

就是网上到处流传的这段代码,PPT转pdf就会报错

com.jacob.com.ComFailException: Invoke of: SaveAs
Source: Microsoft PowerPoint 2013
Description: Presentation.SaveAs : PowerPoint 无法将 ^0 保存到 ^1。

PPT转jpg也是这样的错误,但是word和excel就不会,求解啊,,,

或者有什么其它的windows下office转pdf的方法么(⊙v⊙)?

跪谢。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2021-12-04 08:34:16

openoffice调用服务转换,不用ms office ,具体可百度。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文