基于openoffice的office类型转换_转载.doc_第1页
基于openoffice的office类型转换_转载.doc_第2页
基于openoffice的office类型转换_转载.doc_第3页
基于openoffice的office类型转换_转载.doc_第4页
基于openoffice的office类型转换_转载.doc_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

官方网站: /opensource/jodconverter 下载地点: /opensource/jodconverter /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启动, 才可以使用. OpenO具有一个鲜为人知的特性就是其能够作为一个服务来运行,而这种能力具有一定的妙用。举例来说,你可以把openoffice.og变成一个转换引擎,利用这种转换引擎你可以通过网络接口或命令行工具对文件的格式进行转换,JODConverter可以帮助你实现OpenO的这种文件转换功能。为了将OpenO作为一个转换引擎,你必须以服务的方式将它启动,使它在某个特定的端口监听连接,在Linux平台你可以用如下的命令启动:soffice -headless -accept=”socket,port=8100;urp;”(我在linux下使用soffice -headless -accept=”socket,host=,port=8100;urp;”,open office server是开启来了,但是文件转换不成功,异常是连接失败,这个很可以是你用jodconverter来转换时使用的是localhost,而当你的机有host配置文件里没有将localhost与对应起来时,就无法解析了,这里可以修改host文件或去掉host=,这样我试过可以成功)在Windows平台, 使用如下命令:“C:Program FilesOpenO 2.2programsoffice” -accept=”socket,port=8100;urp;”使用教学: Step1: 安装OpenOffice Step2: 启动OpenOffice Service 1 cd C:Program FilesOpenO 3program 2 soffice -headless -accept=socket,host=,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: 执行以下程式 Java代码 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 OpenO 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在使用的时候必须要注意. 那我也來補充一些好了 之前也在試這個檔案轉換的程式 程式最好加上 try-catch 因為之前发現有些檔案 format 不能转,发生 Exception 后,connection 不會自动切断,程序会hand 住 所以改成如下方式: Java代码 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,程式如下: Java代码 1. public void convertSTW(String input, String output) 2. DocumentFormat stw = new DocumentFormat(OpenO 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 的內容即可。 Java代码 1. DocumentFormat sxd = new DocumentFormat(OpenO 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 都有

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论