使用openOffice將office文件轉成pdf


    1. 下載安裝openoffice,下載地址:http://www.openoffice.org/download/
    2. 我安裝的目錄:
    3. 輸入cmd回車

      在命令窗口輸入
      soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard
      此時服務就開啟了
    4. 將以下代碼放到工具類里面,直接調用即可
       1 import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
       2 import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
       3 import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
       4 ...
       5 ...
       6     public File office2Pdf(String srcPath, String pdfPath) throws Exception {
       7         // 源文件目錄
       8         File inputFile = new File(srcPath);
       9         if (!inputFile.exists()) {
      10             throw new Exception("程序出現問題,文件不存在");
      11         }
      12         // 輸出文件目錄
      13         File outputFile = new File(pdfPath);
      14         if (!outputFile.exists()) {
      15             outputFile.createNewFile();
      16         }
      17         // 調用openoffice服務線程
      18         String command = openOfficeCommand;
      19         Process process = Runtime.getRuntime().exec(command);
      20 
      21         // 連接openoffice服務
      22         OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeUrl, openOfficePort);
      23         connection.connect();
      24 
      25         // 轉換word到pdf
      26         DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
      27         converter.convert(inputFile, outputFile);
      28         // 關閉連接
      29         connection.disconnect();
      30         // 關閉進程
      31         process.destroy();
      32         return outputFile;
      33     }
      34 ...

       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM