调用jacob服务时注意事项

需求有一个将office文件转成PDF并添加水印的功能,office转PDF需要用到jacob功能

转化代码

   private static final int wdFormatPDF = 17;
    private static final int xlTypePDF = 0;
    private static final int ppSaveAsPDF = 32;
    private static final int msoTrue = -1;
    private static final int msofalse = 0;
    public static boolean word2PDF(String inputFile,String pdfFile){
        try{
            ComThread.InitSTA();
        //打开word应用程序
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        //设置word不可见
        app.setProperty("Visible", false);
        //获得word中所有打开的文档,返回Documents对象
        Dispatch docs = app.getProperty("Documents").toDispatch();
        //调用Documents对象中Open方法打开文档,并返回打开的文档对象Document
        Dispatch doc = Dispatch.call(docs,
                                    "Open",
                                    inputFile,
                                    false,
                                    true
                                    ).toDispatch();
        //调用Document对象的SaveAs方法,将文档保存为pdf格式
        /*
        Dispatch.call(doc,
                    "SaveAs",
                    pdfFile,
                    wdFormatPDF     //word保存为pdf格式宏,值为17
                    );
                    */
        Dispatch.call(doc,
                "ExportAsFixedFormat",
                pdfFile,
                wdFormatPDF     //word保存为pdf格式宏,值为17
                );
        //关闭文档
        Dispatch.call(doc, "Close",false);
        //关闭word应用程序
        app.invoke("Quit", 0);
        return true;
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }finally{
        ComThread.Release();
    }
    }
    public static boolean excel2PDF(String inputFile,String pdfFile){
        try{
            ComThread.InitSTA();
            ActiveXComponent app = new ActiveXComponent("Excel.Application");
            app.setProperty("DisplayAlerts", "False");
        app.setProperty("Visible", false);
        Dispatch excels = app.getProperty("Workbooks").toDispatch();
        Dispatch excel = Dispatch.call(excels,
                                    "Open",
                                    inputFile,
                                    false,
                                    true
                                    ).toDispatch();
        Dispatch.call(excel,
                    "ExportAsFixedFormat",
                    xlTypePDF,      
                    pdfFile
                    );
        Dispatch.call(excel, "Close",false);
        app.invoke("Quit");
        return true;
    }catch(Exception e){
        e.printStackTrace();
        return false;
    }finally{
        
        ComThread.Release();
    }
         
    }
    public static boolean ppt2PDF(String inputFile,String pdfFile){
        try{
            ComThread.InitSTA();
        ActiveXComponent app = new ActiveXComponent("PowerPoint.Application");
        //app.setProperty("Visible", msofalse);
        Dispatch ppts = app.getProperty("Presentations").toDispatch();
         
        Dispatch ppt = Dispatch.call(ppts,
                                    "Open",
                                    inputFile,
                                    true,//ReadOnly
                                    true,//Untitled指定文件是否有标题
                                    false//WithWindow指定文件是否可见
                                    ).toDispatch();
         
        Dispatch.call(ppt,
                    "SaveAs",
                    pdfFile,
                    ppSaveAsPDF 
                    );
                 
        Dispatch.call(ppt, "Close");
         
        app.invoke("Quit");
        return true;
        }catch(Exception e){
            return false;
        }finally{
            ComThread.Release();
        }
        
    }

这个是网上找到的功能,的确可以实现需求,但是有很明显的缺陷。

代码转化完后会打开一个进程,但是结束时却不会主动开闭,导致内存占用最终崩溃。

所以要在 每个方法前   加上 

 ComThread.InitSTA();
结束加上
 ComThread.Release(); //释放进程

1.         注意事项

2.1 注意使用高版本的ofiice,建议office2010.

2.2启动office的打印服务

office打印设置为系统默认

2.3 由于高本版的office经常将excel自动设置为受保护的视图,需要解除文件状态

  Excel文件  文件--选项信任中心信任中心设置受信任的位置-添加新位置(可能涉及到需要用到的文件的地方,例如tomcat文件夹和文件上传目录)

2.4 服务启动的tomcat使用jacob组件时要设置 DOCM权限,否则用不了。Tomcat catalina方式启动或者main方式启动不会有这个问题。

 

 运行输入   comexp.msc -32

 

找到 office Excel word ,右键属性

标识---交互式用户

 

 

安全-配置权限编辑

 

将所有权限勾上

 
 

 

转载于:https://www.cnblogs.com/qiuyuedong/p/5138398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值