使用SaveAsPDFandXPS + jacob实现Java word转pdf

简介

SaveAsPDFandXPS,微软官方提供的插件。必须是windows系统并且安装了office。
在这里插入图片描述

下载和配置

SaveAsPDFandXPS 下载地址 : http://www.microsoft.com/zh-cn/download/details.aspx?id=7
Jacob 的jar包 下载地址 :https://sourceforge.net/projects/jacob-project/
先安装SaveAsPDFandXPS ,安装成功后 , 打开jacob的jar包 ,里面的结构应该是这样的
在这里插入图片描述
把jacob.jar 放到项目下 , 然后 这里注意 把 两个后缀为dll的文件 放到 jre目录bin的里面 必须放进去 不然会报错(java.lang.NoClassDefFoundError)

开发路径

在这里插入图片描述
或者
在这里插入图片描述

Word转PDF代码

package office;

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

import java.io.File;

/**
 * @author liuzonghua
 * @Package office
 * @Description:
 * @date 2019/1/31 10:09
 */
public class WordToPDF {


    private static final int wdFormatPDF = 17;// 指定17为转成pdf格式

    public static void main(String[] args) {
        wordToPDF("C:\\Users\\Admin\\Desktop\\2" + ".doc","C:\\Users\\Admin\\Desktop\\2"  + ".pdf");
    }
    

    public static void wordToPDF(String inFilePath, String outFilePath){

        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" , inFilePath).toDispatch();
            File tofile = new File(outFilePath);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc,"SaveAs", outFilePath, wdFormatPDF);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            Dispatch.call(doc,"Close",false);
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        //结束后关闭进程
        ComThread.Release();
    }
}

Excel转PDF代码

package office;

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

/**
 * @author liuzonghua
 * @Package office
 * @Description:
 * @date 2019/1/31 14:18
 */
public class ExcelToPDF {

    public static void main(String[] args) {
       xlsToPdf("C:\\Users\\Admin\\Desktop\\2017年学生体质健康测试成绩.xls","C:\\Users\\Admin\\Desktop\\3.pdf");
    }

    public static boolean xlsToPdf(String inFilePath, String outFilePath) {
        ComThread.InitSTA(true);
        ActiveXComponent ax = new ActiveXComponent("Excel.Application");
        try {
            ax.setProperty("Visible", new Variant(false));
            ax.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
            Dispatch excels = ax.getProperty("Workbooks").toDispatch();


            Dispatch excel = Dispatch.invoke(
                    excels,
                    "Open",
                    Dispatch.Method,
                    new Object[] { inFilePath, new Variant(false),
                            new Variant(false) }, new int[9]).toDispatch();
            // 转换格式
            Dispatch.invoke(excel, "ExportAsFixedFormat", Dispatch.Method,
                    new Object[] { new Variant(0), // PDF格式=0
                            outFilePath, new Variant(0) // 0=标准 (生成的PDF图片不会变模糊)
            // 1=最小文件
            // (生成的PDF图片糊的一塌糊涂)
                    }, new int[1]);


            Dispatch.call(excel, "Close", new Variant(false));


            if (ax != null) {
                ax.invoke("Quit", new Variant[] {});
                ax = null;
            }
            ComThread.Release();
            return true;
        } catch (Exception es) {
            return false;
        }
    }
}

注意

如果出现类似:com.jacob.com.ComFailException: Can’t map name to dispid: Close错误
一般情况下是本机设置问题,doc被admin锁定
解决doc被admin锁定方法有多种,可以使用文件另存为解除锁定。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值