使用JODConverter转换文档为PDF

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的版本

[java]  view plain copy
  1. OfficeManager officeManager = new DefaultOfficeManagerConfiguration().buildOfficeManager();  
  2. officeManager.start();  
  3.   
  4. OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
  5. converter.convert(new File("test.odt"), new File("test.pdf");  
  6.           
  7. officeManager.stop();  
如果你直接运行这些代码的话,是不会成功的,报一个错误

[java]  view plain copy
  1. Exception in thread "main" java.lang.IllegalStateException: officeHome not set and could not be auto-detected  
  2.     at org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration.buildOfficeManager(DefaultOfficeManagerConfiguration.java:163)  
  3.     at org.ygy.util.PDFUtil.toPDF(PDFUtil.java:11)  
  4.     at org.ygy.util.PDFUtil.main(PDFUtil.java:21)  
要使用JODconverter需要安装OpenOffice或者LibreOffice,我安装了OpenOffice。

然后设置一下officeHome:

[java]  view plain copy
  1. public static void toPDF() {  
  2.         OfficeManager officeManager = new DefaultOfficeManagerConfiguration()  
  3.                          .setOfficeHome("D:\\program files\\openoffice.org 3")  
  4.                          .buildOfficeManager();  
  5.         officeManager.start();  
  6.   
  7.         OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
  8.         converter.convert(new File("E:\\demo.doc"), new File("E:\\demo_1.pdf"));  
  9.               
  10.         officeManager.stop();  
  11.     }  
这下就可以了,之前用2.2的版本,需要手动的开一个服务:

[java]  view plain copy
  1. public static void startServer() {  
  2.         String cmd = "cmd /k soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";  
  3.         try {  
  4.             Runtime.getRuntime().exec(cmd , null , new File("D:\\program files\\openoffice.org 3\\program"));  
  5.         } catch (IOException e) {  
  6.             e.printStackTrace();  
  7.         }  
  8.     }  

3.TXT转换为PDF

在将.txt文件转换为PDF的时候发现,如果TXT文件的编码是ANSI的话,中文会产生乱码,解决办法是先将.txt保存为.odt文件,再将.odt文件转换为PDF就可以了。

如果TXT文件的编码是UTF-8的话,中文也可以正常转换。

[java]  view plain copy
  1. package org.ygy.util;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.artofsolving.jodconverter.OfficeDocumentConverter;  
  6. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;  
  7. import org.artofsolving.jodconverter.office.OfficeManager;  
  8.   
  9. public class PDFUtil {  
  10.     public static void toPDF(String sourcePath , String targetpath) {  
  11.         if(sourcePath.endsWith(".txt")) {  
  12.             //先保存为.odt  
  13.             StringBuffer odtPath = new StringBuffer(sourcePath.substring(0 , sourcePath.lastIndexOf(".")));  
  14.             odtPath.append(".odt");  
  15.   
  16.             FileUtil.write(sourcePath , odtPath.toString());  
  17.             sourcePath = odtPath.toString();  
  18.         }  
  19.         OfficeManager officeManager = new DefaultOfficeManagerConfiguration()  
  20.                                         .setOfficeHome("D:\\program files\\openoffice.org 3")  
  21.                                         .buildOfficeManager();  
  22.         officeManager.start();  
  23.   
  24.         OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);  
  25.         converter.convert(new File(sourcePath), new File(targetpath));  
  26.               
  27.         officeManager.stop();  
  28.     }  
  29.       
  30.     public static void main(String[] args) {  
  31.         PDFUtil.toPDF("E:\\type.txt" , "E:\\type_2.pdf");  
  32.     }  
  33. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值