官方网站: 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启动, 才可以使用.
OpenOffice.org具有一个鲜为人知的特性就是其能够作为一个服务来运行,而这种能力具有一定的妙用。举例来说,你可以把openoffice.og变成一个转换引擎,利用这种转换引擎你可以通过网络接口或命令行工具对文件的格式进行转换,JODConverter可以帮助你实现OpenOffice.org的这种文件转换功能。
为了将OpenOffice.org作为一个转换引擎,你必须以服务的方式将它启动,使它在某个特定的端口监听连接,在Linux平台你可以用如下的命令启动openoffice.org:
soffice -headless -accept=”socket,port=8100;urp;”(我在linux下使用soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;”,open office server是开启来了,但是文件转换不成功,异常是连接失败,这个很可以是你用jodconverter来转换时使用的是localhost,而当你的机有host配置文件里没有将localhost与127.0.0.1对应起来时,就无法解析了,这里可以修改host文件或去掉host=127.0.0.1,这样我试过可以成功)
在Windows平台, 使用如下命令:
“C:\Program Files\OpenOffice.org 2.2\program\soffice” -accept=”socket,port=8100;urp;”
使用教学:
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: 执行以下程式
- <span style="font-size: medium;">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;
- publicclass JodDemo {
- publicstaticvoid 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();
- }
- } </span>
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在使用的时候必须要注意.
那我也來補充一些好了
之前也在試這個檔案轉換的程式
程式最好加上 try-catch
因為之前发現有些檔案 format 不能转,发生 Exception 后,connection 不會自动切断,程序会hand 住
所以改成如下方式:
- <span style="font-size: medium;">publicvoid 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){}
- }
- } </span>
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,程式如下:
- <span style="font-size: medium;">publicvoid 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){}
- }
- } </span>
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 的內容即可。
- <span style="font-size: medium;">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"); </span>
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,至于它里面的参数可以从jodconverter-2.2.2.jar包的com.artofsolving.jodconverter包下的document-formats.xml文件里面得到,这样就可以完成多种格式的转换,如open office,ms office , wps office及所有的纯文本文件。
相关推荐
利用jodconverter-core-3.0-beta-4.jar包解决office转pdf,同时解决excel转pdf,excel列数太多的时候,分成很多pdf页的问题
word、excel、ppt转pdf小demo,毫无水印,全套可用包含三个jar,三个测试文档,还有可用license.xml,spring boot写的,建议idea打开
jodconverter-2.2.2.zip 利用openoffice word转pdf所需jar 使用说明:https://blog.csdn.net/weixin_40845192/article/details/104660387
支持将Word2003,word2007等以上版本,将office如Word,excel,ppt等资源转换为pdf
jodconverter-2.2.2 在2.2.1的基础上新增了对docx/xlsx转pdf,配合openoffcie使用,可以通过java实现对word和excel转pdf的操作
jodconverter word文档转换pdf格式工具架包,亲测使用
PDF转Word软件
openoffice--word转PDF,利用openoffice将word文档转为PDF
而作为PDF转换软件厂商,免费的 AnyBizSoft PDF to Text 也是必须的了,若只需要PDF转Word功能,则可以试用 AnyBizSoft PDF to Word Converter。 感觉 AnyBizSoft 是最近"声名鹊起"的一个公司,前段时间全世界首发...
开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用openoffice+jodconverter 实现转换文档。
jodconverter-2.2.2.jar架包 jodconverter word文档转换pdf格式工具架包,亲测使用
java实现Office转PDF格式需要jodconverter相关jar包以及下载并安装OpenOffice。 本资源包含了所有的jodconverter及相关jar包,供大家下载使用。 OpenOffice的下载地址:http://www.openoffice.org/
Java跨平台将word转为pdf(结合Jodconverter 和OpenOffice.org)
利用jodconverter--doc2pdf
linux和windows安装openOffice java通过jodconverter 将excel、doc文件转成pdf或html,比2.2.1版本相比 提供office 2007版本支持 maven是只有2.2.1版本的maven依赖; maven 安装jar包到本地命令 mvn install:install-...
word转pdf工具,内含linux版本的jodconverter 2.2.2,其他链接有openoffice4.0.0、中文字体包,亲测好用
当转换多列EXCEL为PDF时,如EXCEL列过多,则会换行显示,上述java将EXCEL缩小转换,类百度文库做法。EXCEL经上述处理后在进行转换就可以转换成为一行对应一行的pdf
1、需要用到组建在转PDF,安装OpenOffice 3:下载路径:http://www.openoffice.org/download/ 2、用以下命令启动OpenOffice服务 cd C:\Program Files (x86)\OpenOffice.org 3\program soffice -headless -accept=...
JODConverter,是一个Java的OpenDocument文件转换器,依赖于OpenOffice.org或者LibreOffice提供的服务来进行转换,它能将Microsoft Office文档(Word,Excel,PowerPoint)转换为PDF格式。
java实现Office转PDF格式需要jodconverter相关jar包以及下载并安装OpenOffice。 本资源包含了所有的jodconverter及相关jar包以及java代码和openoffice服务器文档,供大家下载使用。 OpenOffice的下载地址:...