目前版本: JODConverter v2.2.1, OpenOffice v3.0.0 
使用需求: JDK1.4以上, 安裝OpenOffice v2.0.3以上 
基本簡介: 
JODConverter 主要的功能是用來做各種檔案的轉換. 目前測試過, Word,Excel,PowerPoint轉PDF都是沒問題的. 
因為JODConverter 是透過OpenOffice來做轉換, 所以使用前需要先安裝OpenOffice, 並且將OpenOffice的Service啟動, 才可以使用. 
使用教學: 
Step1: 安裝OpenOffice 
Step2: 啟動OpenOffice Service 
1 cd C:\Program Files\OpenOffice.org 3\program 
2 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 
Step3:將JODConverter 的Jar檔放進專案中的Library, 請檢查你的專案是否包含以下的Jar檔: jodconverter -2.2.1.jar 
jurt-2.3.0.jar 
xstream-1.2.2.jar 
ridl-2.3.0.jar 
commons-io-1.3.1.jar 
juh-2.3.0.jar 
slf4j-api-1.4.3.jar 
unoil-2.3.0.jar 
slf4j-jdk14-1.4.3.jar

Step4: 準備一個word檔放在c:/document.doc 
Step5: 執行以下程式

  1. import java.io.File;
  2. import com.artofsolving.jodconverter .DocumentConverter;
  3. import com.artofsolving.jodconverter .openoffice.connection.OpenOfficeConnection;
  4. import com.artofsolving.jodconverter .openoffice.connection.SocketOpenOfficeConnection;
  5. import com.artofsolving.jodconverter .openoffice.converter.OpenOfficeDocumentConverter;
  6. public class JodDemo {
  7. public static void main(String[] args) throws Exception{
  8. File inputFile = new File("c:/document.doc");
  9. File outputFile = new File("c:/document.pdf");
  10. // connect to an OpenOffice.org instance running on port 8100
  11. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
  12. connection.connect();
  13. // convert
  14. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  15. converter.convert(inputFile, outputFile);
  16. // close the connection
  17. connection.disconnect();
  18. }
  19. }

程式說明: 
程式的部份相當簡潔, 特別要注意的地方是第12行連線的port必須與你啟動OpenOffice的Port相同, 
另外JODConverter 預設是用副檔名作文件種類的判斷, 所以副檔名必須要正確才行. 
如果副檔名比較特別的話, 就必須在convert()的時候強制指定Document Type.

心得: 
JODConverter 使用起來相當方便, 官網也提供War檔讓JODConverter 變成Web Service提供給不同的語言來呼叫.
特別要注意的是, OpenOffice Service並不是ThreadSafe的, 多個Web AP在使用的時候必須要注意.

參考資料: 
http://www.artofsolving.com/opensource/jodconverter

那我也來補充一些好了 
之前也在試這個檔案轉換的程式 
程式最好加上 try-catch 
因為之前發現有些檔案 format 不能轉,發生 Exception 後,connection 不會自動切斷,程式會 hand 住 
所以改成如下方式:

  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. try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
  13. }
  14. }

再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下:

  1. public void convertSTW(String input, String output){
  2. DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
  3. DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
  4. DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
  5. File inputFile = new File(input);
  6. File outputFile = new File(output);
  7. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
  8. try {
  9. connection.connect();
  10. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  11. converter.convert(inputFile, stw, outputFile, pdf);
  12. } catch(Exception e) {
  13. e.printStackTrace();
  14. } finally {
  15. try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
  16. }
  17. }

上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。

  1. DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");
  2. DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula", "odf");

所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter .DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定義 DocumentFormat。

转载于:https://blog.51cto.com/3475174/1078971

java环境用openoffice转pdf相关推荐

  1. openoffice java awt_使用openoffice转pdf,详细

    期由于项目的需求,需要word文档转pdf在线预览,由于一直没有接触这块,所以花了将近四天时间才弄明白. 写这篇文章的主要目的是加深自己的记忆,同时方便以后在用. (最近有使用了这个功能,发现这篇文章 ...

  2. Java使用openOffice转PDF以及PDF文件预览乱码问题

    Java使用openOffice转PDF以及PDF文件预览乱码问题 使用openOffice,支持doc, docx, .xls, .xlsx, .ppt, .pptx转pdf 一:依赖 <de ...

  3. java swf转pdf_doc转pdf和pdf转swf

    [java]代码库/** *OpenOffice可以到官网下载最新版的SWFTools也是直接搜索名字即可 *OpenOffice转2007版本一下的支持比较好2010以上对插入的艺术字表格等会丢失但 ...

  4. 卸载CentOS 5.4自带的OpenJDK,配置新的Java环境

    本文CentOS版本为5.4 final,使用图形界面与命令结合的操作方式 由于CentOS 5.4在默认情况下,会安装OpenOffice之类的软件,而这些软件需要Java支持,因此系统会默认安装一 ...

  5. java在linux生成pdf文件,从 Java 应用程序动态生成 PDF 文件

    简介: 如果您的应用程序需要动态生成 PDF 文档,那么您需要 iText 库.开源的 iText 库使得 PDF 的创建变得轻松易行.本文介绍了 iText 并提供了一个使用它从 Java 技术应用 ...

  6. java程序设计实用教程高飞pdf_普通高等教育“计算机类专业”规划教材:Java程序设计实用教程习题集 pdf epub mobi txt 下载...

    普通高等教育"计算机类专业"规划教材:Java程序设计实用教程习题集 pdf epub mobi txt 下载 图书介绍 ☆☆☆☆☆ 高飞,赵小敏,陆佳炜 等 著 下载链接在页面底 ...

  7. 阿里巴巴项目P8技术咖总结的Java心得,完整版PDF可下载

    自学Java,如果觉得看<Java编程思想>或者<Core Java>等之类的"圣经"觉得内容太多,一下子吃不透的话,不妨看看这本<Java基础核心& ...

  8. OpenOffice转PDF遇到的问题

    原文:https://blog.csdn.net/FANKYIXUN/article/details/80627339  问题:Java 操作  OpenOffice 将excel 转换为 pdf , ...

  9. java 调用linux openoffice,java 调用linux openoffice

    如题,Java后台调用OpenOffice将文件转换为PDF,在调用时报错,异常信息如下: Mar 20, 2014 1:16:13 AM org.artofsolving.jodconverter. ...

最新文章

  1. matlab 基于Topsis的熵权法2
  2. Python-统计svn变更代码行数
  3. presentViewController和pushViewController
  4. eclipse安装jsp
  5. linux中进程pts 1和pts 3,termial创建时ptmx与pts的关系
  6. 服务器证书安装配置指南(IIS7)
  7. java中初始化块、静态初始化块和构造方法
  8. ASP.NET在类中引用Application和Server.MapPath
  9. 静态路由的不通配置方式
  10. PHP中的异常和错误(转载)
  11. 蜂鸣器c51汇编语言,51单片机蜂鸣器奏乐实验汇编代码.doc
  12. 用tinypng压缩图片
  13. 计算机机房岗位管理制度,机房管理规定-机房管理制度.doc
  14. godaddy无法修改域名服务器,GoDaddy域名修改DNS教程 - 修改DNSPOD及CLOUDXNS免费DNS
  15. ORBSLAM2 理论部分_高斯金字塔(二)
  16. RuntimeError:The size of tensor a (100) must match the size of tensor b (12800) at non-singleton di
  17. 2020年书法落款_书法落款的基本常识最新版
  18. 程序员女朋友都是在哪找的?
  19. HTML简单语法总结
  20. Mac系统安装/升级 Git

热门文章

  1. freertos之任务
  2. Android插件化(使用Small框架)
  3. HTML的input类型为hidden导致无法reset改字段的value问题
  4. 51 单片机 跑马灯2
  5. 【BZOJ-2599】Race 点分治
  6. 并发模型之——基本概念
  7. android 界面切换【转】
  8. u-boot分析——struct gd_t与struct bd_t
  9. Unity3D正常行走和飞行的实现
  10. 熬了一个通宵,终于解决了所有的事情,下线,睡觉