doc2pdf

http://xuexin0714.javaeye.com/blog/458026

http://topic.csdn.net/u/20080407/16/15E7B2B1-FD3F-41A6-9007-BF515EC41517.html

http://blog.csdn.net/lyzhang87/archive/2010/06/10/5662217.aspx

http://blog.csdn.net/wudi626/archive/2008/04/28/2337857.aspx

http://www.open-open.com/34.htm

http://www.oschina.net/p/podofo

http://podofo.sourceforge.net/

http://hi.baidu.com/jiang365/blog/item/a972fc95892c5444d1135ea4.html

http://adf.ly/91637/banner/http://sourceforge.net/projects/pdfcreator/

http://wiki.services.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling

http://api.openoffice.org/docs/DevelopersGuide/ProfUNO/ProfUNO.xhtml#1_4_2_C_2B_2B_Language_Binding

http://wiki.services.openoffice.org/wiki/API/Tutorials/PDF_export#How_to_use_it_from_C.2B.2B

 

http://www.javaeye.com/topic/352103

 

 

package   com.rizon.word2pdf;

 

import   com.jacob.activeX.ActiveXComponent; 

import   com.jacob.com.ComThread;

import   com.jacob.com.Dispatch; 

import   com.jacob.com.Variant; 

 

/**   word 文檔轉化爲 pdf 文檔

 * 

 * @author   SUJL

 *

*/  

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( boolean   saveOnExit) {

            if   (wordCom!= null ) {

                //關閉 word 文件

               //Dispatch.call(wordDoc, "Close", new Variant(saveOnExit));                  wordCom.invoke("Quit", new   Variant (0) );

                //wordCom.invoke("Quit",new Variant[0]);                  wordCom.release(); //add 2008-07-07                  wordCom= null ;

                //釋放在程序線程中引用的其它 com ,比如 Adobe PDFDistiller                  ComThread.Release();

            }

        } 

 

        /**  

          *  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( true ); 

                return

            } 

            //建立 Adobe Distiller com 對象              ActiveXComponent distiller=   new   ActiveXComponent("PDFDistiller.PDFDistiller.1");

            try  

                //設置當前使用的打印機,我的 Adobe Distiller 打印機名字爲  "Adobe PDF"                  wordCom.setProperty("ActivePrinter", new   Variant("Adobe PDF"));

                //設置 printout 的參數,將 word 文檔打印爲 postscript 文檔。目前只使用了前 5 個參數,如果要使用更多的話可以參考 MSDN office 開發相關 api 

               //是否在後臺運行                  Variant Background=   True;

                //是否追加打印                  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 });

                //add 2008-07-07                  Dispatch.call(wordDoc,"Close", new   Variant( false ));

                

                System.out.println(" ps 文檔轉換爲 pdf 文檔成功! "); 

            } catch   (Exception ex) { 

                ex.printStackTrace();

            } finally  

                closeWord( true ); 

            } 

        } 

 

        public   static   void   main(String[] argv) { 

                D2P d2p=   new   D2P(); 

                //d2p.openWord("D:/D2P/doc/doc.doc");

               //d2p.callWordMacro( "c:/12.docc ", "MyWordMacro ", 

               //               new String[] { "這是調用 word 宏的測試程序 " } );                  d2p.docToPDF("D:/D2P/doc/doc.doc","D:/D2P/ps/ps.ps","D:/D2P/pdf/pdf.pdf"); //只轉一個沒問題

               //d2p.docToPDF("D:/D2P/doc/部署說明 .doc", "D:/D2P/ps/ 部署說明 .ps", "D:/D2P/pdf/ 部署說明 .pdf");// 把註釋去掉,再轉一個就抱錯了。爲什麼呢?一些資源沒釋放???          } 

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章