文档在线预览(二)-使用JODConverter转换文档为PDF
2013-02-20 12:19
1016 查看
1.JODConverter介绍:
JODConverter automates conversions between office document formats using OpenOffice.org or LibreOffice.Supported formats include OpenDocument, PDF, RTF, HTML, Word, Excel, PowerPoint, and Flash.
It can be used as a Java library, a command line tool, or a web application.
JODConverter可以将一般格式的文档转换为PDF格式。
官网地址:http://code.google.com/p/jodconverter/
下载地址:http://download.csdn.net/detail/jolingogo/5074520
2.示例
这里用的是3.0的版本OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(new File("test.odt"), new File("test.pdf"); officeManager.stop();如果你直接运行这些代码的话,是不会成功的,报一个错误
Exception in thread "main" java.lang.IllegalStateException: officeHome not set and could not be auto-detected at org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration.buildOfficeManager(DefaultOfficeManagerConfiguration.java:163) at org.ygy.util.PDFUtil.toPDF(PDFUtil.java:11) at org.ygy.util.PDFUtil.main(PDFUtil.java:21)要使用JODconverter需要安装OpenOffice或者LibreOffice,我安装了OpenOffice。
然后设置一下officeHome:
public static void toPDF() { OfficeManager officeManager = new DefaultOfficeManagerConfiguration() .setOfficeHome("D:\\program files\\openoffice.org 3") .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(new File("E:\\demo.doc"), new File("E:\\demo_1.pdf")); officeManager.stop(); }这下就可以了,之前用2.2的版本,需要手动的开一个服务:
public static void startServer() { String cmd = "cmd /k soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard"; try { Runtime.getRuntime().exec(cmd , null , new File("D:\\program files\\openoffice.org 3\\program")); } catch (IOException e) { e.printStackTrace(); } }
3.TXT转换为PDF
在将.txt文件转换为PDF的时候发现,如果TXT文件的编码是ANSI的话,中文会产生乱码,解决办法是先将.txt保存为.odt文件,再将.odt文件转换为PDF就可以了。如果TXT文件的编码是UTF-8的话,中文也可以正常转换。
package org.ygy.util; import java.io.File; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; public class PDFUtil { public static void toPDF(String sourcePath , String targetpath) { if(sourcePath.endsWith(".txt")) { //先保存为.odt StringBuffer odtPath = new StringBuffer(sourcePath.substring(0 , sourcePath.lastIndexOf("."))); odtPath.append(".odt"); FileUtil.write(sourcePath , odtPath.toString()); sourcePath = odtPath.toString(); } OfficeManager officeManager = new DefaultOfficeManagerConfiguration() .setOfficeHome("D:\\program files\\openoffice.org 3") .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(new File(sourcePath), new File(targetpath)); officeManager.stop(); } public static void main(String[] args) { PDFUtil.toPDF("E:\\type.txt" , "E:\\type_2.pdf"); } }
这篇博客还介绍了其他的转换PDF的方法,可以学习一下
http://www.cnblogs.com/luckyxiaoxuan/archive/2012/06/13/2548355.html
相关文章推荐
- (三)java实现文档的在线浏览-使用JODConverter将office文档转换为pdf
- 使用jodconverter转换文档为PDF
- 使用jodconverter-3.0-beta-2.jar 转换Office 文档为 PDF
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- openOffice+jodConverter+jquery.media.js在线预览office、pdf、txt等文档
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper] 使用JODConverter将office文档转换为pdf
- 仿百度文库方案[openoffice.org 3+swftools+flexpaper](三) 之 使用JODConverter将office文档转换为pdf
- 使用JODConverter转换文档为PDF
- java将office文档,word,ppt,pdf文档转换成swf文件在线预览
- vue插件开发 使用pdf.js实现手机端在线预览pdf文档
- java将office文档pdf文档转换成swf文件在线预览
- [置顶] 使用PageOffice实现文档(word,excel,pdf)在线预览编辑
- java将office文档pdf文档转换成swf文件在线预览
- 利用 jodconverter 和openoffice 结合 把doc、ppt 等一系列文档转换成pdf
- java将office文档pdf文档转换成swf文件在线预览
- java将office文档pdf文档转换成swf文件在线预览
- centos6.5下安装openoffice+jodconverter+swftool+flexpaper工具实现在线预览文档功能
- java将office文档pdf文档转换成swf文件在线预览