Java跨平台将word转为pdf(结合Jodconverter 和OpenOffice.org)

Java跨平台将word转为pdf

(结合Jodconverter开源框架 和OpenOffice.org办公软件)

Jodconverter的版本:jodconverter 2.2.1

maven:

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

网上的流传的方法:

1. 安装OpenOffice 3     下载路径:http://zh.openoffice.org/new/zh_cn/downloads.html

 

2. 启动OpenOffice服务

 cd C:\Program Files\OpenOffice.org 3\program

         soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

3. 利用Jodconverter编写转换类

 

Java代码  收藏代码
public void convert(String input, String output){   
        File inputFile = new File(input);   
        File outputFile = new File(output);   
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);   
        try {   
            connection.connect();   
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
            converter.convert(inputFile, outputFile);   
        } catch(Exception e) {   
            e.printStackTrace();   
        } finally {   
            try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}   
        }   
    }

 

以上方法需要启动OpenOffice的服务,麻烦且占内存。

后来,有人又整理了一个方法:直接在转换类中调用启动服务。

 

 

Java代码  收藏代码
public static int office2PDF(String sourceFile, String destFile) {  
  
        String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 这里是OpenOffice的安装目录,  
                                                                        // 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是尽对没题目的  
        // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'  
        if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {  
            OpenOffice_HOME += "/";  
        }  
        Process pro = null;  
        try {  
            // 启动OpenOffice的服务  
            String command = OpenOffice_HOME  
                    + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";  
            pro = Runtime.getRuntime().exec(command);  
            // connect to an OpenOffice.org instance running on port 8100  
            OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);  
            connection.connect();  
  
            // convert  
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
            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();  
        } finally {  
            pro.destroy();  
        }  
  
        return 1;  
    }  

google code无意中发现jodconverter 的新版本jodconverter-core 3.0-beta-4。

但是不知道为什么用不了maven,只能下载包 。地址:https://code.google.com/p/jodconverter/downloads/list

并结合其他开发者的整理如下:

 

Java代码  收藏代码
      /** 
 * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice 
 *  
 * @param sourceFile 
 *            源文件,绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, .docx, .xls, .xlsx, .ppt, .pptx等. 
 *  
 * @param destFile 
 *            目标文件.绝对路径. 
 */  
public static void word2pdf(String inputFilePath) {  
    DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();  
  
    String officeHome = getOfficeHome();  
    config.setOfficeHome(officeHome);  
  
    OfficeManager officeManager = config.buildOfficeManager();  
    officeManager.start();  
  
    OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
    String outputFilePath = getOutputFilePath(inputFilePath);  
    File inputFile = new File(inputFilePath);  
    if (inputFile.exists()) {// 找不到源文件, 则返回  
        File outputFile = new File(outputFilePath);  
        if (!outputFile.getParentFile().exists()) { // 假如目标路径不存在, 则新建该路径  
            outputFile.getParentFile().mkdirs();  
        }  
        converter.convert(inputFile, outputFile);  
    }  
  
    officeManager.stop();  
}  
  
public static String getOutputFilePath(String inputFilePath) {  
    String outputFilePath = inputFilePath.replaceAll(".doc", ".pdf");  
    return outputFilePath;  
}  
  
public static String getOfficeHome() {  
    String osName = System.getProperty("os.name");  
    if (Pattern.matches("Linux.*", osName)) {  
        return "/opt/openoffice.org3";  
    } else if (Pattern.matches("Windows.*", osName)) {  
        return "D:/Program Files/OpenOffice.org 3";  
    } else if (Pattern.matches("Mac.*", osName)) {  
        return "/Application/OpenOffice.org.app/Contents";  
    }  
    return null;  
}  
posted @ 2018-07-18 11:01  蚂蚁总督  阅读(1174)  评论(0)    收藏  举报