[JODConverter]word转pdf心得分享

官方网站:  http://www.artofsolving.com/opensource/jodconverter 

下载地点: 
  http://www.artofsolving.com/opensource/jodconverter 
  http://zh.openoffice.org/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启动, 才可以使用. 

OpenOffice.org具有一个鲜为人知的特性就是其能够作为一个服务来运行,而这种能力具有一定的妙用。举例来说,你可以把openoffice.og变成一个转换引擎,利用这种转换引擎你可以通过网络接口或命令行工具对文件的格式进行转换,JODConverter可以帮助你实现OpenOffice.org的这种文件转换功能。

为了将OpenOffice.org作为一个转换引擎,你必须以服务的方式将它启动,使它在某个特定的端口监听连接,在Linux平台你可以用如下的命令启动openoffice.org:
soffice -headless -accept=”socket,port=8100;urp;”(我在linux下使用
soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;”,open office server是开启来了,但是文件转换不成功,异常是连接失败,这个很可以是你用jodconverter来转换时使用的是localhost,而当你的机有host配置文件里没有将localhost与127.0.0.1对应起来时,就无法解析了,这里可以修改host文件或去掉host=127.0.0.1,这样我试过可以成功)

在Windows平台, 使用如下命令:
“C:\Program Files\OpenOffice.org 2.2\program\soffice” -accept=”socket,port=8100;urp;”

  使用教学: 
  Step1: 安装OpenOffice 
  Step2: 启动OpenOffice Service 


    

     1 cd C:\Program Files\OpenOffice.org 3\program 

   2 soffice -headless -accept="socket,host=127.0.0.1,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. <span style="font-size: medium;">import java.io.File;   
  2.   
  3.   import com.artofsolving.jodconverter.DocumentConverter;   
  4.   
  5.   import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;   
  6.   
  7.   import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;   
  8.   
  9.   import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;   
  10.   
  11.   public class JodDemo {   
  12.   
  13.   public static void main(String[] args) throws Exception{   
  14.   
  15.   File inputFile = new File("c:/document.doc");   
  16.   
  17.   File outputFile = new File("c:/document.pdf");   
  18.   
  19.   // connect to an OpenOffice.org instance running on port 8100   
  20.   
  21.   OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);   
  22.   
  23.   connection.connect();   
  24.   
  25.   // convert   
  26.   
  27.   DocumentConverter converter = new OpenOfficeDocumentConverter(connection);   
  28.   
  29.   converter.convert(inputFile, outputFile);   
  30.   
  31.   // close the connection   
  32.   
  33.   connection.disconnect();   
  34.   
  35.   }   
  36.   
  37.   } </span>  

 程式说明: 

  程式的部份相当简洁, 特别要注意的地方是第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. <span style="font-size: medium;">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.             tryif(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}   
  13.         }   
  14.     } </span>  


再來,明明就是 open office 的檔案,卻生不能轉換的問題。例如:*.STW, *.SXD, *.ODF 等,後來才知道可以自行指定來源檔和輸出檔的 mime-type,程式如下: 


Java代码   收藏代码
  1. <span style="font-size: medium;">public void convertSTW(String input, String output){   
  2.         DocumentFormat stw = new DocumentFormat("OpenOffice.org 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.             tryif(connection != null){connection.disconnect(); connection = null;}}catch(Exception e){}   
  16.         }   
  17.     } </span>  

上面的程式是轉換 STW 到 PDF,如果是 SXD / ODF 則只需要變更 DocumentFormat 的內容即可。 


Java代码   收藏代码
  1. <span style="font-size: medium;">DocumentFormat sxd = new DocumentFormat("OpenOffice.org 1.0 Drawing", DocumentFamily.DRAWING, "application/vnd.sun.xml.sraw""sxd");   
  2.   
  3. DocumentFormat odf = new DocumentFormat("OpenDocument Math", DocumentFamily.TEXT, "application/vnd.oasis.opendocument.formula""odf"); </span>  

 所有 default support 的 DocumentFormat 都在 com.artofsolving.jodconverter.DefaultDocumentFormatRegistry 裡,但並非所有 open office 支援的 file format 都有,所以要像上面的方法自行去定义 DocumentFormat,至于它里面的参数可以从jodconverter-2.2.2.jar包的com.artofsolving.jodconverter包下的document-formats.xml文件里面得到,这样就可以完成多种格式的转换,如open office,ms office , wps office及所有的纯文本文件。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Linux环境中使用Java JODConverter批量换文件为PDF,你可以按照以下步骤进行操作: 1. 安装OpenOffice或LibreOffice:JODConverter依赖于OpenOffice或LibreOffice,因此需要先在Linux环境中安装其中一个软件。 2. 安装JODConverter:下载JODConverter的压缩包并解压缩,然后将JAR文件添加到你的Java类路径中。 3. 编写Java代码:编写Java代码来实现批量换文件为PDF。以下是一个示例代码: ```java import java.io.File; import java.util.HashMap; import java.util.Map; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; public class ConvertFilesToPDF { public static void main(String[] args) { // Set up the OfficeManager configuration DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); config.setOfficeHome("/usr/lib/libreoffice"); config.setPortNumber(8100); // Start the OfficeManager OfficeManager manager = config.buildOfficeManager(); manager.start(); // Set up the converter OfficeDocumentConverter converter = new OfficeDocumentConverter(manager); // Set up the input and output directories File inputDir = new File("/path/to/input/dir"); File outputDir = new File("/path/to/output/dir"); // Get a list of all files in the input directory File[] files = inputDir.listFiles(); // Loop through all files and convert them to PDF for (File file : files) { String inputFilePath = file.getAbsolutePath(); String outputFilePath = outputDir.getAbsolutePath() + "/" + file.getName() + ".pdf"; Map<String, Boolean> filterData = new HashMap<String, Boolean>(); filterData.put("Hidden", true); converter.convert(new File(inputFilePath), new File(outputFilePath), filterData); } // Stop the OfficeManager manager.stop(); } } ``` 4. 运行代码:使用Java命令运行代码,即可批量换文件为PDF,输出文件将保存在指定的输出目录中。 以上是在Linux环境中使用Java JODConverter批量换文件为PDF的步骤,希望对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值