隨筆 - 303  文章 - 883  trackbacks - 0
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

          • n維空間
          • Email : java3d@126.com 群 : 12999758

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網站

          搜索

          •  

          最新評論

          大家晚上好??! 我是尋覓!

                 
                   前幾天,網友遇到一個用Java實現 word 轉換成 pdf 的問題。悲哀,本人解決不了!
          后來網友解決了問題,找到本人,本人在這里謝謝那個網友!下面會介紹他的blog希望
          大家沒事去看看,支持支持!謝謝!

          匿名網友的blog:http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx

            好了,進入正題 :

          其實,本人之所以失敗,和代碼沒關系;問題出在下面的設置上,本人因為沒有
          仔細閱讀方法中的設置部分,導致失??!希望大家看完這個文,能避免我的錯誤;

          本人把問題的解決分成多步,用最詳細的內容讓大家一步成功?。。∽4蠹液眠\?。。。?!

          1》要用到的軟件
                        (1)Adobe Acrobat 8 Professional (最低版本7.03)
                                  (個人非商業使用)8.0破解版下載地址:    
          http://green.crsky.com/soft/2205.html (記得下載補?。?br />                             安裝文件 http://down1.greendown.cn//200611/AcroPro80_efg.rar
                                      破解          http://soft.greendown.cn//200611/AcroPro80_Crack.rar
                        (2)gs811w32.rar  (PDF轉換時所需要的腳本ps)
                                    
          http://www.allmail.com.cn/gs811w32.rar
                        (3)postscript.rar (PDF虛擬打印機的驅動)
                                   
          http://www.pdfhome.com.cn/Resource/DownLoad/postscript.rar
                       (4)jacob.jar
                                
          jacob_1.9.zip 
                       (5)office 2003
           
          2》原理
                                                       jacob.jar
                        doc -> ps ->pdf     >>>>   office 2003 ->gs811w32->Adobe Acrobat 8->postscript->打印機

                           
           (其中關于jacob,jar的安裝請看:jacob使用入門及問題解析)    

          3》安裝運行
                         (1)安裝   Adobe Acrobat 8 Professional 
                                     
                         (2)安裝   gs811w32.rar 
                                      
                         (3)配置打印機(這里不需要真實的打印機)
                            控制面板》  打印機及其他硬件》打印機和傳真》添加打印機
                          (如果添加時顯示“操作無法完成。打印后臺程序服務沒有運行。”
                            請打開控制面板》性能和維護》管理工具》服務》找到“Print Spooler”
                           》右擊屬性》啟動)》選擇本地打印機(如果沒有打印機請將“檢測并安裝
                           即插打印機”的鉤去掉)》下一步》選擇“使用以下端口”
                          (My Document/*.pdf  (Adobe PDF Port))》下一步 選擇打印機》我選擇
                            的是Apple的 Color LaserWriter 12/600(工作后,有錢一定要買個Apple  hp)
                           》下一步(記住打印機的名字:Apple Color LaserWriter 12/600)
                           》下一步(沒有打印機的朋友請選擇:不測試)
                                                
                        (4)安裝 postscript.rar (安裝時,注意每一步,選擇與前面設置相關的選項)
                                     
                       (5)設置Adobe Acrobat 8 Professional:選擇一個pdf文件,右擊打開方式選擇
                           使用 打開Adobe Acrobat 8 Professional》選擇file菜單》Print Setup...》打印選項
                           屬性“Apple Color LaserWriter 12/600”》確定
                                     
                         (6)運行下面的代碼:


          代碼:(相信大家已經看到成功的曙光了,有問題請留言,或找本人,QQ:492006004
                            歡迎加入我管理的群:37486623)

          /**
           * 
          @author XuMing Li
           * 
           * 
          @version 1.00, 2007-4-9
           *  
           
          */

          import com.jacob.activeX.ActiveXComponent;
          import com.jacob.com.ComThread;
          import com.jacob.com.Dispatch;
          import com.jacob.com.Variant;
          public class D2P {
              
          private ActiveXComponent wordCom = null;

              
          private Object wordDoc = null;

              
          private final Variant False = new Variant(false);

              
          private final Variant True = new Variant(true);

              
          /** *//**
               * 打開word文檔
               * 
               * 
          @param filePath
               *            word文檔
               * 
          @return 返回word文檔對象
               
          */

              
          public boolean openWord(String filePath) {
                  
          //建立ActiveX部件
                  wordCom = new ActiveXComponent("Word.Application");

                  
          try {
                      
          //返回wrdCom.Documents的Dispatch
                      Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
                      
          //調用wrdCom.Documents.Open方法打開指定的word文檔,返回wordDoc
                      wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,
                              
          new Object[] { filePath }new int[1]).toDispatch();
                      
          return true;
                  }
           catch (Exception ex) {
                      ex.printStackTrace();
                  }

                  
          return false;
              }


              
          /** *//**
               * 關閉word文檔
               
          */

              
          public void closeWord() {
                  
          //關閉word文件
                  wordCom.invoke("Quit"new Variant[] {});
              }


              
          /** *//**
               * * 將word文檔打印為PS文件后,使用Distiller將PS文件轉換為PDF文件 *
               * 
               * 
          @param sourceFilePath
               *            源文件路徑 *
               * 
          @param destinPSFilePath
               *            首先生成的PS文件路徑 *
               * 
          @param destinPDFFilePath
               *            生成PDF文件路徑
               
          */

              
          public void docToPDF(String sourceFilePath, String destinPSFilePath,
                      String destinPDFFilePath) 
          {
                  
          if (!openWord(sourceFilePath)) {
                      closeWord();
                      
          return;
                  }

                  
          //建立Adobe Distiller的com對象
                  ActiveXComponent distiller = new ActiveXComponent(
                          
          "PDFDistiller.PDFDistiller.1");
                  
          try {
                      
          //設置當前使用的打印機,我的Adobe Distiller打印機名字為"Adobe PDF"
                      wordCom.setProperty("ActivePrinter"new Variant("MS Publisher Color Printer"));
                      
          //設置printout的參數,將word文檔打印為postscript文檔。目前只使用了前5個參數,如果要使用更多的話可以參考MSDN的office開發相關api
                      
          //是否在后臺運行
                      Variant Background = False;
                      
          //是否追加打印
                      Variant Append = False;
                      
          //打印所有文檔
                      int wdPrintAllDocument = 0;
                      Variant Range 
          = new Variant(wdPrintAllDocument);
                      
          //輸出的postscript文件的路徑
                      Variant OutputFileName = new Variant(destinPSFilePath);

                      Dispatch.callN((Dispatch) wordDoc, 
          "PrintOut"new Variant[] {
                              Background, Append, Range, OutputFileName }
          );
                      System.out.println(
          "由word文檔打印為ps文檔成功!");
                      
          //調用Distiller對象的FileToPDF方法所用的參數,詳細內容參考Distiller Api手冊
                      
          //作為輸入的ps文檔路徑
                      Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
                      
          //作為輸出的pdf文檔的路徑
                      Variant outputPDFFilePath = new Variant(destinPDFFilePath);
                      
          //定義FileToPDF方法要使用adobe pdf設置文件的路徑,在這里沒有賦值表示并不使用pdf配置文件
                      Variant PDFOption = new Variant("");
                      
          //調用FileToPDF方法將ps文檔轉換為pdf文檔
                      Dispatch.callN(distiller, "FileToPDF"new Variant[] {
                              inputPostScriptFilePath, outputPDFFilePath, PDFOption }
          );
                      System.out.println(
          "由ps文檔轉換為pdf文檔成功!");
                  }
           catch (Exception ex) {
                      ex.printStackTrace();
                  }
           finally {
                      closeWord();
                      wordCom
          =null;                        
                      
          //釋放在程序線程中引用的其它com,比如Adobe PDFDistiller                        
                      ComThread.Release(); 
                  }

              }


              
          public static void main(String[] argv) {
                  D2P d2p 
          = new D2P();
                  d2p.docToPDF(
          "d:/12.doc""d:/1p.ps""d:/1p.pdf");
                              
          //這里是你建一個叫12.doc的word文檔,生成的文檔將在D盤下
                              
          //1p.ps和1p.pdf(這是我們要的)
              }

          }


                                                   
           




          地震讓大伙知道:居安思危,才是生存之道。
          posted on 2007-04-12 20:08 小尋 閱讀(1882) 評論(4)  編輯  收藏 所屬分類: j2se/j2ee/j2me

          FeedBack:
          # re: 用jcob實現 word to pdf ~~~~~~~~~~~ 謝謝網友幫助 2007-04-13 19:37 batxut
          不錯,有用到的時候再拿來參考.
          不過有個小問題:你提供的匿名網友的Blog鏈接打不開,會不會是給出的鏈接有錯!  回復  更多評論
            
          # re: 用jcob實現 word to pdf ~~~~~~~~~~~ 謝謝網友幫助 2007-04-13 22:18 幻想~@@~
           我試過了,可以打開;
          可能是DNS的問題吧!
          你可以換個地方上網試試!
          謝謝你的支持!
          祝你成功?。?!
            回復  更多評論
            
          # re: 用jcob實現 word to pdf ~~~~~~~~~~~ 謝謝網友幫助 2007-05-04 10:50 虎威
          好東西啊
          不過那個運行代碼 在哪里運行????  回復  更多評論
            
          # re: 用jcob實現 word to pdf ~~~~~~~~~~~ 謝謝網友幫助 2007-05-05 07:27 幻想~@@~
          按照步驟一步一步的去做就能完成  回復  更多評論
            
          主站蜘蛛池模板: 仙游县| 罗田县| 汤阴县| 宣武区| 安丘市| 郴州市| 泸州市| 博野县| 慈利县| 威远县| 监利县| 周口市| 岱山县| 镇沅| 化隆| 中卫市| 若尔盖县| 邹平县| 富川| 工布江达县| 汤原县| 湟源县| 临海市| 彰化县| 蛟河市| 璧山县| 专栏| 涡阳县| 清远市| 广河县| 昌吉市| 鲜城| 囊谦县| 蓝田县| 普兰县| 安化县| 铜梁县| 哈尔滨市| 农安县| 毕节市| 建瓯市|