openoffice转pdf

来源:互联网 发布:淘宝u站特价乐购 编辑:程序博客网 时间:2024/06/06 15:51

文章来自:http://blog.csdn.net/youthon/article/details/32717471

1. 需要用的软件

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

    JodConverter 下载地址http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接从附件里面下载

  

2.启动OpenOffice的服务

安装完openoffice,安装服务

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

执行

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

查看是否安装成功,查看端口对应的pid

    netstat -ano|findstr "8100"

 查看pid对应的服务程序名

    tasklist|findstr "pid值"

 

3.将JodConverter相关的jar包添加到项目中   

  

4. 下面是实现代码

附件里面有现成的可以用的项目示例,直接导入eclipse就可以运行

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * 将Office文档转换为PDF. 运行该函数需要用到OpenOffice, OpenOffice下载地址为 
  3.      * http://www.openoffice.org/ 
  4.      *  
  5.      * <pre> 
  6.      * 方法示例: 
  7.      * String sourcePath = "F:\\office\\source.doc"; 
  8.      * String destFile = "F:\\pdf\\dest.pdf"; 
  9.      * Converter.office2PDF(sourcePath, destFile); 
  10.      * </pre> 
  11.      *  
  12.      * @param sourceFile 
  13.      *            源文件, 绝对路径. 可以是Office2003-2007全部格式的文档, Office2010的没测试. 包括.doc, 
  14.      *            .docx, .xls, .xlsx, .ppt, .pptx等. 示例: F:\\office\\source.doc 
  15.      * @param destFile 
  16.      *            目标文件. 绝对路径. 示例: F:\\pdf\\dest.pdf 
  17.      * @return 操作成功与否的提示信息. 如果返回 -1, 表示找不到源文件, 或url.properties配置错误; 如果返回 0, 
  18.      *         则表示操作成功; 返回1, 则表示转换失败 
  19.      */  
  20.     public static int office2PDF(String sourceFile, String destFile) {  
  21.         try {  
  22.             File inputFile = new File(sourceFile);  
  23.             if (!inputFile.exists()) {  
  24.                 return -1;// 找不到源文件, 则返回-1  
  25.             }  
  26.   
  27.             // 如果目标路径不存在, 则新建该路径  
  28.             File outputFile = new File(destFile);  
  29.             if (!outputFile.getParentFile().exists()) {  
  30.                 outputFile.getParentFile().mkdirs();  
  31.             }  
  32.               
  33.             // connect to an OpenOffice.org instance running on port 8100  
  34.             OpenOfficeConnection connection = new SocketOpenOfficeConnection(  
  35.                     "127.0.0.1"8100);  
  36.             connection.connect();  
  37.   
  38.             // convert  
  39.             DocumentConverter converter = new OpenOfficeDocumentConverter(  
  40.                     connection);  
  41.             converter.convert(inputFile, outputFile);  
  42.   
  43.             // close the connection  
  44.             connection.disconnect();  
  45.   
  46.             return 0;  
  47.         } catch (FileNotFoundException e) {  
  48.             e.printStackTrace();  
  49.             return -1;  
  50.         } catch (ConnectException e) {  
  51.             e.printStackTrace();  
  52.         } catch (IOException e) {  
  53.             e.printStackTrace();  
  54.         }  
  55.   
  56.         return 1;  
  57.     }  

0 0