溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          近期項目中用到將用戶上傳的word文檔轉換為pdf文件。用java實現,于是乎在google上百度了一下。得出兩種解決方案。
          1、使用OpenOffice轉PDF;
          2、使用MS Office的OLE自動化功能;
          同樣也得到如下的帖子
          http://www.iteye.com/topic/1005741
          import java.io.File;

          import com.jacob.activeX.ActiveXComponent;
          import com.jacob.com.Dispatch;

          public class Word2Pdf {

          static final int wdDoNotSaveChanges = 0;// 不保存待定的更改。
          static final int wdFormatPDF = 17;// PDF 格式

          public static void main(String[] args) {

              String filename 
          = "D:\\workspace\\word2pdf\\src\\tests.doc";
              String toFilename 
          = filename + ".pdf";
              System.out.println(
          "啟動Word");
              
          long start = System.currentTimeMillis();
              ActiveXComponent app 
          = null;
              
          try {
                  app 
          = new ActiveXComponent("Word.Application");
                  app.setProperty(
          "Visible"false);

                  Dispatch docs 
          = app.getProperty("Documents").toDispatch();
                  System.out.println(
          "打開文檔" + filename);
                  Dispatch doc 
          = Dispatch.call(docs,//
                          "Open"//
                          filename,// FileName
                          false,// ConfirmConversions
                          true // ReadOnly
                          ).toDispatch();

                  System.out.println(
          "轉換文檔到PDF" + toFilename);
                  File tofile 
          = new File(toFilename);
                  
          if (tofile.exists()) {
                      tofile.delete();
                  }
                  Dispatch.call(doc,
          //
                          "SaveAs"//
                          toFilename, // FileName
                          wdFormatPDF);

                  Dispatch.call(doc, 
          "Close"false);
                  
          long end = System.currentTimeMillis();
                  System.out.println(
          "轉換完成..用時:" + (end - start) + "ms.");
              } 
          catch (Exception e) {
                  System.out.println(
          "========Error:文檔轉換失敗:" + e.getMessage());
              } 
          finally {
                  
          if (app != null)
                      app.invoke(
          "Quit", wdDoNotSaveChanges);
              }
          }
          }
          需要使用到的jar
          jacob.jar
          另外拷貝jacob.dll到 Windows System32目錄下
          posted on 2011-06-17 10:23 雪山飛鵠 閱讀(11080) 評論(3)  編輯  收藏 所屬分類: javase

          Feedback

          # re: java轉換word to pdf 2012-12-04 16:07
          我轉的pdf打開之后說文件錯誤不是pdf文檔。怎么辦  回復  更多評論
            

          # re: java轉換word to pdf 2013-05-10 10:59 于文峰
          openoffice轉換不了數學公式啊  回復  更多評論
            

          # re: java轉換word to pdf 2015-03-23 17:08 河北
          @邢
          解決了沒? 跟你遇到同一個問題了.....  回復  更多評論
            

          主站蜘蛛池模板: 防城港市| 阿鲁科尔沁旗| 嵊州市| 桃源县| 青州市| 苏尼特左旗| 临西县| 广南县| 佳木斯市| 新闻| 三亚市| 闵行区| 濉溪县| 双峰县| 金川县| 深州市| 朝阳县| 辉南县| 晋城| 富蕴县| 琼中| 灌阳县| 津市市| 柳河县| 二连浩特市| 应用必备| 浦北县| 富裕县| 兴业县| 林甸县| 射洪县| 嘉善县| 浦北县| 花莲县| 徐州市| 永川市| 洛南县| 新乐市| 乾安县| 尚志市| 贵定县|