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

          歡迎光臨! 
          閑聊 QQ:1074961813

          隨筆分類(357)

          我管理的群

          公共blog

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

          參與管理的論壇

          好友的blog

          我的其他blog

          朋友的網(wǎng)站

          搜索

          •  

          最新評(píng)論

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

                 
                   前幾天,網(wǎng)友遇到一個(gè)用Java實(shí)現(xiàn) word 轉(zhuǎn)換成 pdf 的問題。悲哀,本人解決不了!
          后來網(wǎng)友解決了問題,找到本人,本人在這里謝謝那個(gè)網(wǎng)友!下面會(huì)介紹他的blog希望
          大家沒事去看看,支持支持!謝謝!

          匿名網(wǎng)友的blog:http://blog.csdn.net/gavin_sw/archive/2007/04/11/1561254.aspx

            好了,進(jìn)入正題 :

          其實(shí),本人之所以失敗,和代碼沒關(guān)系;問題出在下面的設(shè)置上,本人因?yàn)闆]有
          仔細(xì)閱讀方法中的設(shè)置部分,導(dǎo)致失敗!希望大家看完這個(gè)文,能避免我的錯(cuò)誤

          本人把問題的解決分成多步,用最詳細(xì)的內(nèi)容讓大家一步成功!!!祝大家好運(yùn)!!!!!

          1》要用到的軟件
                        (1)Adobe Acrobat 8 Professional (最低版本7.03)
                                  (個(gè)人非商業(yè)使用)8.0破解版下載地址:    
          http://green.crsky.com/soft/2205.html (記得下載補(bǔ)丁)
                                      安裝文件 http://down1.greendown.cn//200611/AcroPro80_efg.rar
                                      破解          http://soft.greendown.cn//200611/AcroPro80_Crack.rar
                        (2)gs811w32.rar  (PDF轉(zhuǎn)換時(shí)所需要的腳本ps)
                                    
          http://www.allmail.com.cn/gs811w32.rar
                        (3)postscript.rar (PDF虛擬打印機(jī)的驅(qū)動(dòng))
                                   
          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->打印機(jī)

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

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


          代碼:(相信大家已經(jīng)看到成功的曙光了,有問題請(qǐng)留言,或找本人,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文檔對(duì)象
               
          */

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

                  
          try {
                      
          //返回wrdCom.Documents的Dispatch
                      Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();
                      
          //調(diào)用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;
              }


              
          /** *//**
               * 關(guān)閉word文檔
               
          */

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


              
          /** *//**
               * * 將word文檔打印為PS文件后,使用Distiller將PS文件轉(zhuǎn)換為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對(duì)象
                  ActiveXComponent distiller = new ActiveXComponent(
                          
          "PDFDistiller.PDFDistiller.1");
                  
          try {
                      
          //設(shè)置當(dāng)前使用的打印機(jī),我的Adobe Distiller打印機(jī)名字為"Adobe PDF"
                      wordCom.setProperty("ActivePrinter"new Variant("MS Publisher Color Printer"));
                      
          //設(shè)置printout的參數(shù),將word文檔打印為postscript文檔。目前只使用了前5個(gè)參數(shù),如果要使用更多的話可以參考MSDN的office開發(fā)相關(guān)api
                      
          //是否在后臺(tái)運(yùn)行
                      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文檔成功!");
                      
          //調(diào)用Distiller對(duì)象的FileToPDF方法所用的參數(shù),詳細(xì)內(nèi)容參考Distiller Api手冊(cè)
                      
          //作為輸入的ps文檔路徑
                      Variant inputPostScriptFilePath = new Variant(destinPSFilePath);
                      
          //作為輸出的pdf文檔的路徑
                      Variant outputPDFFilePath = new Variant(destinPDFFilePath);
                      
          //定義FileToPDF方法要使用adobe pdf設(shè)置文件的路徑,在這里沒有賦值表示并不使用pdf配置文件
                      Variant PDFOption = new Variant("");
                      
          //調(diào)用FileToPDF方法將ps文檔轉(zhuǎn)換為pdf文檔
                      Dispatch.callN(distiller, "FileToPDF"new Variant[] {
                              inputPostScriptFilePath, outputPDFFilePath, PDFOption }
          );
                      System.out.println(
          "由ps文檔轉(zhuǎn)換為pdf文檔成功!");
                  }
           catch (Exception ex) {
                      ex.printStackTrace();
                  }
           finally {
                      closeWord();
                      wordCom
          =null;                        
                      
          //釋放在程序線程中引用的其它c(diǎn)om,比如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");
                              
          //這里是你建一個(gè)叫12.doc的word文檔,生成的文檔將在D盤下
                              
          //1p.ps和1p.pdf(這是我們要的)
              }

          }


                                                   
           




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

          FeedBack:
          # re: 用jcob實(shí)現(xiàn) word to pdf ~~~~~~~~~~~ 謝謝網(wǎng)友幫助 2007-04-13 19:37 batxut
          不錯(cuò),有用到的時(shí)候再拿來參考.
          不過有個(gè)小問題:你提供的匿名網(wǎng)友的Blog鏈接打不開,會(huì)不會(huì)是給出的鏈接有錯(cuò)!  回復(fù)  更多評(píng)論
            
          # re: 用jcob實(shí)現(xiàn) word to pdf ~~~~~~~~~~~ 謝謝網(wǎng)友幫助 2007-04-13 22:18 幻想~@@~
           我試過了,可以打開;
          可能是DNS的問題吧!
          你可以換個(gè)地方上網(wǎng)試試!
          謝謝你的支持!
          祝你成功!!!
            回復(fù)  更多評(píng)論
            
          # re: 用jcob實(shí)現(xiàn) word to pdf ~~~~~~~~~~~ 謝謝網(wǎng)友幫助 2007-05-04 10:50 虎威
          好東西啊
          不過那個(gè)運(yùn)行代碼 在哪里運(yùn)行啊??  回復(fù)  更多評(píng)論
            
          # re: 用jcob實(shí)現(xiàn) word to pdf ~~~~~~~~~~~ 謝謝網(wǎng)友幫助 2007-05-05 07:27 幻想~@@~
          按照步驟一步一步的去做就能完成  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 石台县| 保定市| 咸丰县| 苏尼特左旗| 永昌县| 剑河县| 广元市| 翼城县| 苏尼特右旗| 天津市| 当阳市| 乌审旗| 乌什县| 新乡县| 绥宁县| 高雄县| 莱芜市| 依安县| 安阳市| 达州市| 镇雄县| 玉溪市| 东方市| 榆中县| 肇庆市| 普安县| 镇雄县| 平顶山市| 余干县| 姜堰市| 无棣县| 旌德县| 阿克| 阿拉尔市| 循化| 祁东县| 塘沽区| 巩义市| 东兴市| 苏尼特左旗| 施秉县|