JAVA OpenOffice+jodconverter转换WORD为PDF

之前有需求要实现一个将word文档转为PDF文档的功能,刚开始也是无厘头的从网上收集到一些资料,自己也做了下实践。
OpenOffice是一个跨平台的office办公套件,这点也是最符合功能的需要,因为项目也是部署在Linux。
OpenOffice可以在http://www.openoffice.org/下载安装各版本
jodconverter 可以添加maven依赖

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

java代码实现


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class WordToPdf {
    public static int office2PDF(String sourceFile, String destFile) {
        //String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 这里是OpenOffice的安装目录,C:\Program Files (x86)\OpenOffice 4
          String OpenOffice_HOME = "C:/Program Files (x86)/OpenOffice 4/";
        // 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是尽对没题目的
        // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'
        if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
            OpenOffice_HOME += "/";
        }
        Process pro = null;

        try {
            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                return -1;// 找不到源文件, 则返回-1
            }
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            // 启动OpenOffice的服务
            String command = OpenOffice_HOME  
                    + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;StarOffice.ServiceManager\" -nofirststartwizard"; 
            pro = Runtime.getRuntime().exec(command);
            // connect to an OpenOffice.org instance running on port 8100
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
            //OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            connection.connect();

            // convert
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(inputFile, outputFile);
            //converter.convert(inputFile,xml,outputFile,pdf);

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

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

        return 1;
    }


    public static void main(String[] args){
        WordToPdf wordToPdf = new WordToPdf();
        String sourceFile = "E:\\aa.doc";
        String destFile = "E:\\pdfdest.pdf";
        int i =  wordToPdf.office2PDF(sourceFile, destFile);
        System.out.println(i);
    }

}

windows下启动OpenOffice服务

C:\Program Files (x86)>cd “OpenOffice 4”

C:\Program Files (x86)\OpenOffice 4>cd program

C:\Program Files (x86)\OpenOffice 4\program>soffic -headless -accept=”socket,ho
st=127.0.0.1,port=8100;urp;” -nofirststartwizard

linux下相关启动服务参考:

http://blog.csdn.net/kerryzb/article/details/51490701

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值