java 使用jacob将word和excel转换成pdf格式文档

1.word转pdf格式

import com.jacob.com.ComThread;
import com.jacob.com.Variant;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
     * word转成为PDF
     * @param fileName
     * @param toFileName
     * @return
     */
    public static int word2Pdf(String fileName,String toFileName){
        System.out.println("启动word...");
        Long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("word.Application");
            app.setProperty("Visible", new Variant(false));
            //打开word文件
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]{
                    fileName, new Variant(false),new Variant(true) },new int[1]).toDispatch();
            System.out.println("打开文档..."+ fileName);
            System.out.println("转换文档到PDF..."+ toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()){
                tofile.delete();
            }
            Dispatch.invoke(doc,"SaveAs", Dispatch.Method,new Object[]{
                    toFileName,new Variant(17)},new int[1]);
            Long end = System.currentTimeMillis();
            System.out.println("转换完成...用时:" + (end - start) + "ms.");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("=========Error:文档转换失败:" + e.getMessage());
        }catch (Throwable t){
            t.printStackTrace();
        }finally {
            //关闭word
            Dispatch.call(doc,"Close",false);
            System.out.println("关闭文档");
            if (app != null){
                app.invoke("Quit",new Variant[]{});
            }
            //如果没有这句话winword.exe进程将不会关闭
            ComThread.Release();
            return 1;
        }
    }

2.excel转pdf格式

/**
     * excel转PDF
     * @param inFilePath
     * @param outFilePath
     */
    public static void excelToPdf(String inFilePath, String outFilePath){
        ActiveXComponent ax = null;
        Dispatch excel = null;
        try {
            ComThread.InitSTA();
            ax = new ActiveXComponent("Excel.Application");
            ax.setProperty("Visible",new Variant(false));
            ax.setProperty("AutomationSecurity",new Variant(3));//禁用宏
            Dispatch excels = ax.getProperty("Workbooks").toDispatch();
            Object[] obj = new Object[]{
                    inFilePath,
                    new Variant(false),
                    new Variant(false)
            };
            excel = Dispatch.invoke(excels,"Open",Dispatch.Method,obj,new int[9]).toDispatch();
            File tofile = new File(outFilePath);
            if (tofile.exists()){
                tofile.delete();
            }
            //转换格式
            Object[] obj2 = new Object[]{
                    new Variant(0),//PDF格式=0
                    outFilePath,
                    new Variant(0) //0=标准 (生成的PDF图片不会变的模糊) ; 1=最小文件
            };
            Dispatch.invoke(excel, "ExportAsFixedFormat",Dispatch.Method,obj2,new int[1]);
            System.out.println("转换完毕!");
        }catch (Exception es){
            es.printStackTrace();
            throw es;
        }finally {
            if (excel != null){
                Dispatch.call(excel,"CLose",new Variant(false));
            }
            if (ax != null){
                ax.invoke("Quit", new Variant[]{});
                ax = null;
            }
            ComThread.Release();
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值