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

Jodconverter的版本:jodconverter 2.2.1

maven:

 

Java代码  
  1.                      <dependency>  
  2.     <groupId>com.artofsolving</groupId>  
  3.     <artifactId>jodconverter</artifactId>  
  4.     <version>2.2.1</version>  
  5. </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代码  
  1. public void convert(String input, String output){   
  2.         File inputFile = new File(input);   
  3.         File outputFile = new File(output);   
  4.         OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);   
  5.         try {   
  6.             connection.connect();   
  7.             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
  8.             converter.convert(inputFile, outputFile);   
  9.         } catch(Exception e) {   
  10.             e.printStackTrace();   
  11.         } finally {   
  12.             tryif(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}   
  13.         }   
  14.     }   

 

 

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

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

 

 

Java代码   收藏代码
  1. public static int office2PDF(String sourceFile, String destFile) {  
  2.   
  3.         String OpenOffice_HOME = "D:/Program Files/OpenOffice.org 3";// 这里是OpenOffice的安装目录,  
  4.                                                                         // 在我的项目中,为了便于拓展接口,没有直接写成这个样子,但是这样是尽对没题目的  
  5.         // 假如从文件中读取的URL地址最后一个字符不是 '\',则添加'\'  
  6.         if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {  
  7.             OpenOffice_HOME += "/";  
  8.         }  
  9.         Process pro = null;  
  10.         try {  
  11.             // 启动OpenOffice的服务  
  12.             String command = OpenOffice_HOME  
  13.                     + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";  
  14.             pro = Runtime.getRuntime().exec(command);  
  15.             // connect to an OpenOffice.org instance running on port 8100  
  16.             OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1"8100);  
  17.             connection.connect();  
  18.   
  19.             // convert  
  20.             DocumentConverter converter = new OpenOfficeDocumentConverter(connection);  
  21.             converter.convert(inputFile, outputFile);  
  22.   
  23.             // close the connection  
  24.             connection.disconnect();  
  25.             // 封闭OpenOffice服务的进程  
  26.             pro.destroy();  
  27.   
  28.             return 0;  
  29.         } catch (FileNotFoundException e) {  
  30.             e.printStackTrace();  
  31.             return -1;  
  32.         } catch (IOException e) {  
  33.             e.printStackTrace();  
  34.         } finally {  
  35.             pro.destroy();  
  36.         }  
  37.   
  38.         return 1;  
  39.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值