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
这里用的是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(); } }
在将.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