Java用OpenOffice將word轉換為PDF

來源:互聯網
上載者:User

標籤:target   sea   ack   tput   lin   啟動   color   class   url   

本文在原文的基礎上有所修改,原文請參考:

http://titanseason.iteye.com/blog/1471606 由於此blog不支援附件附件請到此處下載

http://my.oschina.NET/bigyuan/blog/165464

1. 需要用的軟體

    OpenOffice http://www.openoffice.org/

    JodConverter http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接從附件裡面下載

  

2.啟動OpenOffice的服務

 

安裝完openoffice,安裝服務

cd C:\Program Files (x86)\OpenOffice 4\program

執行

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

查看是否安裝成功,查看連接埠對應的pid

    netstat -ano|findstr "8100"

 查看pid對應的服務程式名

    tasklist|findstr "pid值"

 

 

3.將JodConverter相關的jar包添加到項目中   

  

4. 下面是實現代碼

附件裡面有現成的可以用的項目樣本,直接匯入eclipse就可以運行

 

 1 本文在原文的基礎上有所修改,原文請參考: 2  3 http://titanseason.iteye.com/blog/1471606 由於此blog不支援附件附件請到此處下載 4 http://my.oschina.NET/bigyuan/blog/165464 5 1. 需要用的軟體 6  7     OpenOffice http://www.openoffice.org/ 8  9     JodConverter http://sourceforge.net/projects/jodconverter/files/JODConverter/,也可以直接從附件裡面下載10 11   12 2.啟動OpenOffice的服務13 14 15 安裝完openoffice,安裝服務16 17 cd C:\Program Files (x86)\OpenOffice 4\program18 19 執行20 21 soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard22 23 查看是否安裝成功,查看連接埠對應的pid24 25     netstat -ano|findstr "8100"26 27  查看pid對應的服務程式名28 29     tasklist|findstr "pid值"30 31  32 3.將JodConverter相關的jar包添加到項目中   33   34 4. 下面是實現代碼35 附件裡面有現成的可以用的項目樣本,直接匯入eclipse就可以運行36 37 [java] view plain copy38 /** 39      * 將Office文檔轉換為PDF. 運行該函數需要用到OpenOffice, OpenOffice為 40      * http://www.openoffice.org/ 41      *  42      * <pre> 43      * 方法樣本: 44      * String sourcePath = "F:\\office\\source.doc"; 45      * String destFile = "F:\\pdf\\dest.pdf"; 46      * Converter.office2PDF(sourcePath, destFile); 47      * </pre> 48      *  49      * @param sourceFile 50      *            源檔案, 絕對路徑. 可以是Office2003-2007全部格式的文檔, Office2010的沒測試. 包括.doc, 51      *            .docx, .xls, .xlsx, .ppt, .pptx等. 樣本: F:\\office\\source.doc 52      * @param destFile 53      *            目標檔案. 絕對路徑. 樣本: F:\\pdf\\dest.pdf 54      * @return 操作成功與否的提示資訊. 如果返回 -1, 表示找不到源檔案, 或url.properties配置錯誤; 如果返回 0, 55      *         則表示操作成功; 返回1, 則表示轉換失敗 56      */  57     public static int office2PDF(String sourceFile, String destFile) {  58         try {  59             File inputFile = new File(sourceFile);  60             if (!inputFile.exists()) {  61                 return -1;// 找不到源檔案, 則返回-1  62             }  63   64             // 如果目標路徑不存在, 則建立該路徑  65             File outputFile = new File(destFile);  66             if (!outputFile.getParentFile().exists()) {  67                 outputFile.getParentFile().mkdirs();  68             }  69               70             // connect to an OpenOffice.org instance running on port 8100  71             OpenOfficeConnection connection = new SocketOpenOfficeConnection(  72                     "127.0.0.1", 8100);  73             connection.connect();  74   75             // convert  76             DocumentConverter converter = new OpenOfficeDocumentConverter(  77                     connection);  78             converter.convert(inputFile, outputFile);  79   80             // close the connection  81             connection.disconnect();  82   83             return 0;  84         } catch (FileNotFoundException e) {  85             e.printStackTrace();  86             return -1;  87         } catch (ConnectException e) {  88             e.printStackTrace();  89         } catch (IOException e) {  90             e.printStackTrace();  91         }  92   93         return 1;  94     }  

 

Java用OpenOffice將word轉換為PDF

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.