使用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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章