之前找了一种方式是通过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 可以自己改。
分享到:
相关推荐
NULL 博文链接:https://titanseason.iteye.com/blog/1471606
本例通过VB脚本使用openoffice将word转成PDF
asp.net利用OpenOffice转换WORD/EXCEL/PDF为PDF,加水印源码
java OpenOffice wordExcel转换PDF.zip 带jar包
网站有许多转换pdf的代码.本人也用过office和wps,上传服务器期后出现了许多bug,最后选用openoffice实现最方便。前提服务器或电脑必须下载openoffice.具体配置文件参考:...
openoffice word转pdf excel转pdf ppt转pdf
NULL 博文链接:https://bigbird2012.iteye.com/blog/2078213
利用OpenOffice技术将word、ppt、excel等文本文件转换成pdf。以实现在线预览的功能
Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用。Word、Excel等常用文档格式通过OpenOffice转换为pdf及图片,亲测,可用...
OpenOffice转换Office文档为PDF、HTML,将word,ppt转成html,Excel转为Html
Entry.java这个类的原理是先通过OfficeToPDF.java把文档转成pdf,然后再通过Pdf2Jpg.java这个类把PDF转成JPG或者PNG,想转成什么格式图片在Pdf2Jpg.java这个类当中设置一下,代码经过测试,完全没有问题,如果碰到...
NULL 博文链接:https://zhang-637.iteye.com/blog/1487556
是基于OpenOffice开发的Word转换PDF程序,程序是提供可录入Word文件地址转PDF文件地址方式,将Word转换为PDF文件
C# 将Word格式文件转换为Pdf格式文件源码
使用OpenOffice将文档转换为PDF,将PDF转换为Swf
支持将 .doc|.docx|.xls|.xlsx|.ppt|.pptx文件 转化为pdf文件 linux安装包: Apache_OpenOffice_4.1.2_Linux_x86_install-rpm_zh-CN.tar.gz Apache_OpenOffice_4.1.7_Linux_x86-64_install-rpm_zh-CN.tar.gz ...
基于openoffice实现的将word文件转换为PDF文件。 DocConverter.java是代码实现 controller.txt中写了简单的使用方法 所需jar包中提供了一些必须导入的jar包。 仅提供了核心部分代码,因为是从项目中提取出来的,测试...
网络上有很多种Word转PDF的方式(openoffice,jacob,POI),但都不是很完美,要不依赖三防包还要下载dll文件,要不转换不完美,还不支持中文,我的转换方式使用eclipse自带的swt包即可,程序引用swt的jar后直接运行
1、需要用到组建在转PDF,安装OpenOffice 3:下载路径:http://www.openoffice.org/download/...我实现的是读取access数据库中对应WORD文件名然后到指定文件夹查找并转换,你也可以到指定文件夹中去遍历所有WORD文件转换
Java通过OpenOffice将word、ppt、excel、图片、txt文档转换成PDF-附件资源