當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks
          代碼如下:
          1.Application類
          package com.ibm.ahcms2.tool.jacob.word;

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

          /**
           * Word應用程序類
           * 說明:
           * 作者:何楊(heyang78@gmail.com)
           * 創建時間:2011-6-4 下午05:16:47
           * 修改時間:2011-6-4 下午05:16:47
           
          */
          public class Application{
              
          // Word應用程序本身
              private ActiveXComponent wordApp;
              
              
          // Word的文檔集合對象
              private Documents documents;
              
              
          /**
               * 構造函數
               
          */
              
          public Application() throws Exception{
                  initialize();
              }
              
              
          /**
               * 應用程序初始化
               * 
               * 說明:
               * 創建時間:2011-6-4 下午05:17:59
               
          */
              
          public void initialize() throws Exception{
                  
          // 初始化com的線程,使用結束后要調用realease方法,見quit函數
                  ComThread.InitSTA();
                  
                  wordApp
          =new ActiveXComponent("Word.Application");
                  wordApp.setProperty(
          "Visible"new Variant(false));
                  
                  Dispatch d
          =wordApp.getProperty("Documents").toDispatch();
                  documents
          =new Documents(d);
              }
              
              
          /**
               * 應用程序退出
               * 
               * 說明:
               * 創建時間:2011-6-4 下午05:18:56
               
          */
              
          public void quit() throws Exception{
                  wordApp.invoke(
          "Quit"new Variant[]{});
                  ComThread.Release();
              }
              
              
          /**
               * 新建文檔,并返回新建文檔的句柄
               * 
               * 說明:
               * 
          @return
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午05:33:07
               
          */
              
          public Document addNewDocument() throws Exception{
                  Dispatch d
          =Dispatch.call(documents.getInstance(),"Add").toDispatch();
                  Document doc
          =new Document(d);
                  
          return doc;
              }
              
              
          /**
               * 得到當前選擇的文字
               * 
               * 說明:
               * 
          @return
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午05:38:28
               
          */
              
          public Selection getSelection() throws Exception{
                  Dispatch d
          =Dispatch.call(wordApp,"Selection").toDispatch();
                  Selection selection
          =new Selection(d,wordApp);
                  
          return selection;
              }
              
              
          /**
               * 打開一個已存在的文檔
               * 
               * 說明:
               * 
          @param filePathName
               * 
          @return
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午06:19:41
               
          */
              
          public Document openExistDocument(String filePathName) throws Exception{
                  Dispatch d 
          = Dispatch.call(documents.getInstance(), "Open", filePathName).toDispatch();
                  Document doc
          =new Document(d);
                  
          return doc;
              }
          }

          2.BaseWord類
          package com.ibm.ahcms2.tool.jacob.word;

          import com.jacob.com.Dispatch;

          public abstract class BaseWord{
              
          protected Dispatch instance;

              
          public BaseWord(Dispatch instance){
                  
          this.instance=instance;
              }
              
              
          public Dispatch getInstance() {
                  
          return instance;
              }

              
          public void setInstance(Dispatch instance) {
                  
          this.instance = instance;
              }
          }

          3.Document類
          package com.ibm.ahcms2.tool.jacob.word;

          import com.jacob.com.Dispatch;
          import com.jacob.com.Variant;


          /**
           * 文檔對象
           * 說明:
           * 作者:何楊(heyang78@gmail.com)
           * 創建時間:2011-6-4 下午05:41:47
           * 修改時間:2011-6-4 下午05:41:47
           
          */
          public class Document extends BaseWord{
              
              
          public Document(Dispatch instance) {
                  
          super(instance);
              }

              
          /**
               * 文檔另存為
               * 
               * 說明:
               * 
          @param filePathName
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午05:42:42
               
          */
              
          public void saveAs(String filePathName) throws Exception{
                  Dispatch.call(instance, 
          "SaveAs", filePathName);
              }
              
              
          public void save() throws Exception{
                  Dispatch.call(instance, 
          "Save");
              }
              
              
          /**
               * 關閉文檔
               * 
               * 說明:
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午05:43:52
               
          */
              
          public void close() throws Exception{
                  Dispatch.call(instance, 
          "Close"new Variant(true));
              }
              
              
          /**
               * 設置頁眉的文字
               * 
               * 說明:
               * 
          @param headerText
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午07:22:37
               
          */
              
          public void setHeaderText(String headerText,Dispatch selection) throws Exception{
                  Dispatch activeWindow 
          = Dispatch.get(instance, "ActiveWindow").toDispatch();
                  Dispatch view 
          = Dispatch.get(activeWindow, "View").toDispatch();
                  Dispatch.put(view, 
          "SeekView"new Variant(9)); //wdSeekCurrentPageHeader-9

                  Dispatch headerFooter 
          = Dispatch.get(selection, "HeaderFooter").toDispatch();
                  Dispatch range 
          = Dispatch.get(headerFooter, "Range").toDispatch();
                  Dispatch.put(range, 
          "Text"new Variant(headerText));
                  Dispatch font 
          = Dispatch.get(range, "Font").toDispatch();

                  Dispatch.put(font, 
          "Name"new Variant("楷體_GB2312"));
                  Dispatch.put(font, 
          "Bold"new Variant(true));
                  Dispatch.put(font, 
          "Size"9);

                  Dispatch.put(view, 
          "SeekView"new Variant(0)); //wdSeekMainDocument-0恢復視圖;
              }
              
              
          /**
               * 設置圖片水印
               * 
               * 說明:
               * 
          @param imagePath
               * 
          @param selection
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午07:48:53
               
          */
              
          public void setImageWaterMark(String imagePath,Dispatch selection) throws Exception{
                  Dispatch activeWindow 
          = Dispatch.get(instance, "ActiveWindow").toDispatch();
                  Dispatch view 
          = Dispatch.get(activeWindow, "View").toDispatch();
                  Dispatch.put(view, 
          "SeekView"new Variant(9)); //wdSeekCurrentPageHeader-9

                  Dispatch headerFooter 
          = Dispatch.get(selection, "HeaderFooter").toDispatch();
                  
                  
          // 獲取水印圖形對象
                  Dispatch shapes=Dispatch.get(headerFooter, "Shapes").toDispatch();
                  
                  Dispatch picture
          =Dispatch.call(shapes, "AddPicture",imagePath).toDispatch();
                  
                  Dispatch.call(picture, 
          "Select");
                  Dispatch.put(picture,
          "Left",new Variant(10));
                  Dispatch.put(picture,
          "Top",new Variant(10));
                  Dispatch.put(picture,
          "Width",new Variant(190));
                  Dispatch.put(picture,
          "Height",new Variant(190));
                  
                  Dispatch.put(view, 
          "SeekView"new Variant(0)); //wdSeekMainDocument-0恢復視圖;
              }
              
              
          /**
               * 設置圖片水印
               * 
               * 說明:
               * 
          @param imagePath
               * 
          @param selection
               * 
          @param left
               * 
          @param top
               * 
          @param width
               * 
          @param height
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午08:00:16
               
          */
              
          public void setImageWaterMark(String imagePath,Dispatch selection,int left,int top,int width,int height) throws Exception{
                  Dispatch activeWindow 
          = Dispatch.get(instance, "ActiveWindow").toDispatch();
                  Dispatch view 
          = Dispatch.get(activeWindow, "View").toDispatch();
                  Dispatch.put(view, 
          "SeekView"new Variant(9)); //wdSeekCurrentPageHeader-9

                  Dispatch headerFooter 
          = Dispatch.get(selection, "HeaderFooter").toDispatch();
                  
                  
          // 獲取水印圖形對象
                  Dispatch shapes=Dispatch.get(headerFooter, "Shapes").toDispatch();
                  
                  Dispatch picture
          =Dispatch.call(shapes, "AddPicture",imagePath).toDispatch();
                  
                  Dispatch.call(picture, 
          "Select");
                  Dispatch.put(picture,
          "Left",new Variant(left));
                  Dispatch.put(picture,
          "Top",new Variant(top));
                  Dispatch.put(picture,
          "Width",new Variant(width));
                  Dispatch.put(picture,
          "Height",new Variant(height));
                  
                  Dispatch.put(view, 
          "SeekView"new Variant(0)); //wdSeekMainDocument-0恢復視圖;
              }
              
              
          /**
               * 給文檔加上保護
               * 
               * 說明:
               * 
          @param pswd
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午07:33:44
               
          */
              
          public void setProtected(String pswd) throws Exception{
                  String protectionType 
          = Dispatch.get(instance, "ProtectionType").toString();
                  
          if(protectionType.equals("-1")){
                      Dispatch.call(instance, 
          "Protect"new Variant(3), new Variant(true), pswd);
                  } 
              }
              
              
          /**
               * 給文檔解除保護
               * 
               * 說明:
               * 
          @param pswd
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午07:38:04
               
          */
              
          public void releaseProtect(String pswd) throws Exception{
                  String protectionType 
          = Dispatch.get(instance, "ProtectionType").toString();
                  
          if(protectionType.equals("3")){
                      Dispatch.call(instance, 
          "Unprotect", pswd);
                  }
              }
          }

          4.Documents類
          package com.ibm.ahcms2.tool.jacob.word;

          import com.jacob.com.Dispatch;

          public class Documents extends BaseWord{

              
          public Documents(Dispatch instance) {
                  
          super(instance);
              }
          }

          5.Selection類
          package com.ibm.ahcms2.tool.jacob.word;

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

          /**
           * 一個文檔中選擇的部分
           * 說明:
           * 作者:何楊(heyang78@gmail.com)
           * 創建時間:2011-6-4 下午05:37:01
           * 修改時間:2011-6-4 下午05:37:01
           
          */
          public class Selection extends BaseWord{
              
          private ActiveXComponent wordApp;
              
              
          public Selection(Dispatch instance,ActiveXComponent wordApp) {
                  
          super(instance);
                  
          this.wordApp=wordApp;
              }

              
          /**
               * 設置選擇區的文字
               * 
               * 說明:
               * 
          @param text
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午05:41:28
               
          */
              
          public void setText(String text) throws Exception{
                  Dispatch.put(instance, 
          "Text", text);
              }
              
              
          /**
               * 用新文字替換舊文字
               * 
               * 說明:
               * 
          @param oldText
               * 
          @param newText
               * 
          @throws Exception
               * 創建時間:2011-6-4 下午06:32:43
               
          */
              
          public void replace(String oldText,String newText) throws Exception{
                  
          while (find(oldText)) {
                      Dispatch.put(instance, 
          "Text", newText);
                      Dispatch.call(instance, 
          "MoveRight");
                  }
              }
              
              
          /**
               * 查找字符串
               * 
               * 說明:
               * 
          @param toFindText
               * 
          @return
               * 創建時間:2011-6-4 下午07:15:39
               
          */
              
          private boolean find(String toFindText) {
                  
          if (toFindText == null || toFindText.equals(""))
                      
          return false;
                  
                  
          // 從selection所在位置開始查詢
                  Dispatch find = wordApp.call(instance, "Find").toDispatch();
                  
          // 設置要查找的內容
                  Dispatch.put(find, "Text", toFindText);
                  
          // 向前查找
                  Dispatch.put(find, "Forward""True");
                  
          // 設置格式
                  Dispatch.put(find, "Format""True");
                  
          // 大小寫匹配
                  Dispatch.put(find, "MatchCase""True");
                  
          // 全字匹配
                  Dispatch.put(find, "MatchWholeWord""False");
                  
          // 查找并選中
                  return Dispatch.call(find, "Execute").getBoolean();
              }
              
              
          /**
               * 插入圖片
               * 
               * 說明:
               * 
          @param imagePath
               * 創建時間:2011-6-4 下午07:17:18
               
          */
              
          public void insertPicture(String imagePath){
                  Dispatch.call(Dispatch.get(instance, 
          "InLineShapes").toDispatch(),
                          
          "AddPicture", imagePath);
              }
          }

          6.測試代碼
          package com.ibm.ahcms2.tool.jacob.word;


          public class Test{
              
          public static void main(String[] args) throws Exception{
                  test09();
              }
              
              
          // 設置頁眉,圖片水印以及設置保護
              public static void test09() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\2.doc");
                  Selection sel
          =app.getSelection();
                  
                  
                  doc.setHeaderText(
          "頁眉文字", sel.getInstance());
                  doc.setImageWaterMark(
          "c:\\1.jpg", sel.getInstance(),20,20,200,200);
                  doc.setProtected(
          "cms123");
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 設置圖片水印
              public static void test08() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  Selection sel
          =app.getSelection();
                  
                  doc.setImageWaterMark(
          "c:\\1.jpg", sel.getInstance());
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 解除密碼保護
              public static void test07() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  
                  doc.releaseProtect(
          "cms123");
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 設置密碼保護
              public static void test06() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  
                  doc.setProtected(
          "cms123");
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 設置表頭
              public static void test05() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  Selection sel
          =app.getSelection();
                  
                  doc.setHeaderText(
          "妞妞乖乖", sel.getInstance());
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 插入圖片
              public static void test04() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  Selection sel
          =app.getSelection();
                  
                  sel.insertPicture(
          "c:\\1.jpg");
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 打開文檔,用新文字替換舊文字后保存
              public static void test03() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  Selection sel
          =app.getSelection();
                  
                  sel.replace(
          "Success""勝利");
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 打開文檔,重新設置文字并保存
              public static void test02() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.openExistDocument("c:\\1.doc");
                  Selection sel
          =app.getSelection();
                  
                  sel.setText(
          "又勝利了!何楊,2011年6月4日18:23:46");
                  
                  doc.save();
                  
                  doc.close();
                  app.quit();
              }
              
              
          // 新建文檔,設置文檔并另存為
              public static void test01() throws Exception{
                  Application app
          =new Application();
                  
                  Document doc
          =app.addNewDocument();
                  Selection sel
          =app.getSelection();
                  
                  sel.setText(
          "勝利了");
                  
                  doc.saveAs(
          "c:\\1.doc");
                  
                  doc.close();
                  app.quit();
              }
          }

          以上代碼下載:
          http://www.aygfsteel.com/Files/heyang/JacobTest20110604201829.rar

          test09效果如下:
          posted on 2011-06-04 20:12 何楊 閱讀(4793) 評論(1)  編輯  收藏

          Feedback

          # 如果只是想給word文檔最后一頁添加水印呢? 2012-06-05 09:42 魯薇
          如果只是想給word文檔最后一頁添加水印呢?求指點。hateluwei@qq.com  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 章丘市| 东阳市| 吴堡县| 平果县| 醴陵市| 渭源县| 双江| 淮滨县| 麻栗坡县| 武宣县| 伊吾县| 龙岩市| 碌曲县| 冕宁县| 连州市| 普安县| 武鸣县| 阳春市| 泰安市| 县级市| 揭西县| 建瓯市| 苏尼特右旗| 二手房| 罗定市| 饶河县| 嘉义县| 韩城市| 汝州市| 唐山市| 南昌县| 东丰县| 昭平县| 合作市| 长子县| 肥城市| 遂宁市| 视频| 奈曼旗| 来安县| 广水市|