java 实现word文件转pdf文件

这篇文章一共有两种方式实现word文件转pdf文件:

1、jacob    仅支持windows下转换,需要下载jacob-1.14.3-x64.dll 插件

2、OpenOffice   支持windows和linux,需要下载openffice

一、通过jacob方式

*只能在windows环境下使用

1.maven依赖

<dependency>
    <groupId>net.sf.jacob-project</groupId>
    <artifactId>jacob</artifactId>
    <version>1.14.3</version>
</dependency>

2.实现代码:

public void wordToPDF(String sfileName, 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));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", sfileName).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到 PDF..." + toFileName);
            File tofile = new File(toFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", toFileName, // FileName
                    17);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时:" + (end - start) + "ms.");

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

说明:

       需要下载  jacob-1.14.3-x64.dll文件,放到   jdk1.8.0_131\jre\bin

    

二、使用Openoffice转换

  *该方法可以自动开启openoffice,linux和windows都可以使用

 1.需要的maven依赖

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.1</version>
</dependency>

2.实现代码:

public static int office2PDF(String sourceFile, String destFile) {
        try {
            System.out.println(sourceFile);
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return -1;// 找不到源文件, 则返回-1
            }
            System.out.println("找不到源文件, 则返回-1");
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            System.out.println("文件创建结束");
            String OpenOffice_HOME = "C:\\Program Files (x86)\\OpenOffice 4\\program";//这里是OpenOffice的安装目录, 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是绝对没问题的
            // 如果从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {
                OpenOffice_HOME += "\\";
            }
            // 启动OpenOffice的服务
            String command = OpenOffice_HOME
                    + "soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
            Process pro = Runtime.getRuntime().exec(command);
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                    "127.0.0.1", 8100);
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(
                    connection);
            System.out.println("ing...");
            converter.convert(inputFile, outputFile);

            // close the connection
            connection.disconnect();
            // 关闭OpenOffice服务的进程
            pro.destroy();

            return 0;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return -1;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 1;
    }

说明:

        需要下载安装openoffic,替换方法中加粗的地址



附:xml格式的word文档转换为标准格式的文档,但是只支持在windows环境下

        soursePath:源文件路径    

        targetPath:转换的目标文件路径

public static void docToDocx(String sourcePath, String targetPath){
        //Word.Application代表COM OLE编程标识,可查询MSDN得到
        ActiveXComponent app = new ActiveXComponent("Word.Application");
        //设置Word不可见
        app.setProperty("Visible",false);
        //调用Application对象的Documents属性,获得Documents对象
        Dispatch docs = app.getProperty("Documents").toDispatch();
        Dispatch doc = Dispatch.call(docs,"Open",sourcePath).getDispatch();
        Dispatch.call(doc,"SaveAS",targetPath,12);
        //关闭打开的Word文件
        Dispatch.call(doc,"Close",false);
        //关闭Word应用程序
        app.invoke("Quit",0);
    }










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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值