利用openoffice转换ppt、doc转化pdf

openoffice下载地址:http://www.openoffice.org/

jodconverter jar包下载:http://sourceforge.net/projects/jodconverter/files/JODConverter/

ps:安装openoffice时,路径尽量不要带有中文或空格等信息、会导致调用时出错。


[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.kingt;  
  2.   
  3.   
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.IOException;  
  7. import java.net.ConnectException;  
  8. import java.util.Date;  
  9.   
  10. import com.artofsolving.jodconverter.DocumentConverter;  
  11. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;  
  12. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;  
  13. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;  
  14.   
  15. public class Office2PDF {  
  16.   
  17.     /**  
  18.      * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为  
  19.      * http://www.openoffice.org/  
  20.      * @param sourceFile  
  21.      *            源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc,  
  22.      *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc  
  23.      * @param destFile  
  24.      *            目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf  
  25.      * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0,  
  26.      *         则表示操作成功; 返回1, 则表示转换失败  
  27.      */    
  28.     public static int office2PDF(String sourceFile, String destFile) {  
  29.         Process pro = null;  
  30.         OpenOfficeConnection connection = null;  
  31.         try {  
  32.             File inputFile = new File(sourceFile);  
  33.             if (!inputFile.exists()) {  
  34.                 return -1;//文件不存在  
  35.             }  
  36.   
  37.             //文件夹不存在创建目录  
  38.             File outputFile = new File(destFile);  
  39.             if (!outputFile.getParentFile().exists()) {  
  40.                 outputFile.getParentFile().mkdirs();  
  41.             }  
  42.   
  43.             String OpenOffice_HOME = "F:\\tool\\OpenOffice4";  
  44.             if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '\\') {  
  45.                 OpenOffice_HOME += "\\";  
  46.             }  
  47.             // 启动OpenOffice的服务    
  48.             String command = OpenOffice_HOME  
  49.                     + "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";  
  50.             pro = Runtime.getRuntime().exec(command);  
  51.             // connect to an OpenOffice.org instance running on port 8100  
  52.             connection = new SocketOpenOfficeConnection("127.0.0.1"8100);  
  53.             connection.connect();  
  54.   
  55.             // convert  
  56.             DocumentConverter converter = new OpenOfficeDocumentConverter(  
  57.                     connection);  
  58.             converter.convert(inputFile, outputFile);  
  59.   
  60.             return 0;  
  61.         } catch (FileNotFoundException e) {  
  62.             e.printStackTrace();  
  63.             return -1;  
  64.         } catch (ConnectException e) {  
  65.             e.printStackTrace();  
  66.         } catch (IOException e) {  
  67.             e.printStackTrace();  
  68.         }finally{  
  69.             if(connection!=null){  
  70.                 // close the connection  
  71.                 connection.disconnect();  
  72.             }  
  73.              // 关闭OpenOffice服务的进程    
  74.             if(pro!=null){  
  75.                 pro.destroy();  
  76.             }  
  77.         }  
  78.   
  79.         return 1;  
  80.     }  
  81.       
  82.     public static void main(String[] args){  
  83.         Date startDate = new Date();  
  84.         String sourceFile = "d:\\code\\alipay.ppt";  
  85.         String destFile = "d:\\code\\change2.pdf";  
  86.         System.out.println(Office2PDF.office2PDF(sourceFile, destFile));  
  87.         Date endDate = new Date();  
  88.         System.out.println("the cost time is "+(endDate.getTime()-startDate.getTime()));  
  89.     }  
  90. }  

执行该服务的命令直接在JAVA代码里面调用,然后当转码完成的时候,直接干掉这个进程。

存在问题:当第一次执行时会报错、

java.net.ConnectException: connection failed: socket,host=localhost,port=8080,tcpNoDelay=1: java.net.ConnectException: Connection refused: connect
再次运行成功、至于这问题暂无找到解决办法。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值