使用openoffice任意格式的office转pdf

实现任意格式的office转pdf:

首先需要安装openoffice
地址:http://download.openoffice.org/other.html
百度很多 这个自己找找

一、配置maven依赖

<!--office转换pdf -->
        <dependency>
            <groupId>com.artofsolving</groupId>
            <artifactId>jodconverter</artifactId>
            <version>2.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>jurt</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>juh</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.openoffice</groupId>
            <artifactId>unoil</artifactId>
            <version>3.0.1</version>
        </dependency>

使用maven只能下载到jodconverter-2.2.1.jar

二、代码实现

  public static void Office2Pdf(String srcPath, String desPath) throws IOException {
        // 源文件目录
        File inputFile = new File(srcPath);
        if (!inputFile.exists()) {
            System.out.println("源文件不存在!");
            return;
        }
        // 输出文件目录
        File outputFile = new File(desPath);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        // 调用openoffice服务线程
        String command = "C:/Program Files (x86)/OpenOffice 4/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
        Process p = Runtime.getRuntime().exec(command);

        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
                "127.0.0.1", 8100);
        connection.connect();

        // 转换word到pdf
        DocumentConverter converter = new OpenOfficeDocumentConverter(
                connection);
        converter.convert(inputFile, outputFile);

        // 关闭连接
        connection.disconnect();

        // 关闭进程
        p.destroy();
        System.out.println("转换完成!");
    }

测试代码

public static void main(String[] args) throws IOException {
        String sourceFile = "D:/1.doc";
        String destFile = "D://1.pdf";
        Office2Pdf.Office2Pdf(sourceFile, destFile);
    }

这个只能实现xls、doc、ppt转pdf 看个人需求
我这边是需要任意格式的office转pdf。 所以,很显然这个效果达不到要求

这边只需要把jodconverter-2.2.1.jar包 替换成jodconverter-2.2.2.jar就可以实现转换效果,但是maven是下在不到这个jar包的

jar地址:jodconverter/jodconverter-2.2.2.jar.zip( 27 k)

这个地址呢没有自己测试过,如果不行就自己百度一个,很多的

这个是maven项目,导入jar包可能不太方便,我这边也是导入不了这个jar包,只能放在lib项目中引用
其实很简单 新建一个lib文件夹 存放jar包
这里写图片描述

这里写图片描述

然后 biudpath 引入jar包
这里写图片描述
这里写图片描述

即可实现任意格式的openoffice转换pdf

有什么不懂的问题可以留言咨询,也可以加我微信:18601979899

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值