`
liss
  • 浏览: 825011 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

利用OpenOffice将word转换成PDF

    博客分类:
  • POI
阅读更多
之前找了一种方式是通过jacob调用adobe PDF虚拟打印机生成PDF,但多人同时调用时有同步问题;在同事的提醒下找了下面的实现方式:


 
   2、用以下命令启动OpenOffice服务
         cd C:\Program Files\OpenOffice.org 3\program
         soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
 3、附上我写的DEMO,第三方包: JODConverter v2.2.1
官方網站:
http://www.artofsolving.com/opensource/jodconverter

下載地點:
http://www.artofsolving.com/opensource/jodconverter
http://zh.openoffice.org/new/zh_tw/downloads.html

目前版本: 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: 執行以下程式

import java.io.File;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class JodDemo {
public static void main(String[] args) throws Exception{
File inputFile = new File("c:/document.doc");
File outputFile = new File("c:/document.pdf");
// connect to an OpenOffice.org instance running on port 8100
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
// close the connection
connection.disconnect();
}
}


程式說明:
程式的部份相當簡潔, 特別要注意的地方是第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 住
所以改成如下方式:

1234567891011121314
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){}
        }
    }




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

1234567891011121314151617
public void convertSTW(String input, String output){
        DocumentFormat stw = new DocumentFormat("OpenOffice.org 1.0 Template", DocumentFamily.TEXT, "application/vnd.sun.xml.writer", "stw");
        DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
        DocumentFormat pdf = formatReg.getFormatByFileExtension("pdf");
        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, stw, outputFile, pdf);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            try{ if(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}
        }
    }




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

123
DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw", "sxd");

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。

在此獻給所有需要作 File Convert 的人試試。
免錢的,最好用。還有 source code 可以自己改。
分享到:
评论
8 楼 fireinjava 2012-10-30  
呀,不错,转走了,谢谢啦~
7 楼 longgol 2011-11-01  
执行转换之后, 下一次执行就会报错了.
Exception: conversion failed: could not load input document
6 楼 yeahgoodboy 2011-05-29  
兄弟,看到com.chinacreator命名的java包很亲切
5 楼 ainoke 2010-12-16  
非常感谢...
4 楼 javatim 2010-11-04  
兄弟,谢谢啦,我也在研究这个东东……
3 楼 zhangrong_rongzhang2 2010-08-31  
不过我想知道怎么让openOffice的服务做到开机启动
2 楼 zhangrong_rongzhang2 2010-08-31  
按照你这种方式,我的可以实现转pdf了。谢谢
1 楼 guoxhvip 2010-05-06  
请问这位朋友 为什么我照你的步骤执行 程序却一直卡住不动了
2010-5-6 16:08:33 com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection connect
信息: connected

请问是哪个地方出现问题了?

相关推荐

Global site tag (gtag.js) - Google Analytics