<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    當(dāng)柳上原的風(fēng)吹向天際的時(shí)候...

    真正的快樂來源于創(chuàng)造

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      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應(yīng)用程序類
     * 說明:
     * 作者:何楊(heyang78@gmail.com)
     * 創(chuàng)建時(shí)間:2011-6-4 下午05:16:47
     * 修改時(shí)間:2011-6-4 下午05:16:47
     
    */
    public class Application{
        
    // Word應(yīng)用程序本身
        private ActiveXComponent wordApp;
        
        
    // Word的文檔集合對(duì)象
        private Documents documents;
        
        
    /**
         * 構(gòu)造函數(shù)
         
    */
        
    public Application() throws Exception{
            initialize();
        }
        
        
    /**
         * 應(yīng)用程序初始化
         * 
         * 說明:
         * 創(chuàng)建時(shí)間:2011-6-4 下午05:17:59
         
    */
        
    public void initialize() throws Exception{
            
    // 初始化com的線程,使用結(jié)束后要調(diào)用realease方法,見quit函數(shù)
            ComThread.InitSTA();
            
            wordApp
    =new ActiveXComponent("Word.Application");
            wordApp.setProperty(
    "Visible"new Variant(false));
            
            Dispatch d
    =wordApp.getProperty("Documents").toDispatch();
            documents
    =new Documents(d);
        }
        
        
    /**
         * 應(yīng)用程序退出
         * 
         * 說明:
         * 創(chuàng)建時(shí)間:2011-6-4 下午05:18:56
         
    */
        
    public void quit() throws Exception{
            wordApp.invoke(
    "Quit"new Variant[]{});
            ComThread.Release();
        }
        
        
    /**
         * 新建文檔,并返回新建文檔的句柄
         * 
         * 說明:
         * 
    @return
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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;
        }
        
        
    /**
         * 得到當(dāng)前選擇的文字
         * 
         * 說明:
         * 
    @return
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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;
        }
        
        
    /**
         * 打開一個(gè)已存在的文檔
         * 
         * 說明:
         * 
    @param filePathName
         * 
    @return
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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;


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

        
    /**
         * 文檔另存為
         * 
         * 說明:
         * 
    @param filePathName
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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");
        }
        
        
    /**
         * 關(guān)閉文檔
         * 
         * 說明:
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間:2011-6-4 下午05:43:52
         
    */
        
    public void close() throws Exception{
            Dispatch.call(instance, 
    "Close"new Variant(true));
        }
        
        
    /**
         * 設(shè)置頁(yè)眉的文字
         * 
         * 說明:
         * 
    @param headerText
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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恢復(fù)視圖;
        }
        
        
    /**
         * 設(shè)置圖片水印
         * 
         * 說明:
         * 
    @param imagePath
         * 
    @param selection
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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();
            
            
    // 獲取水印圖形對(duì)象
            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恢復(fù)視圖;
        }
        
        
    /**
         * 設(shè)置圖片水印
         * 
         * 說明:
         * 
    @param imagePath
         * 
    @param selection
         * 
    @param left
         * 
    @param top
         * 
    @param width
         * 
    @param height
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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();
            
            
    // 獲取水印圖形對(duì)象
            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恢復(fù)視圖;
        }
        
        
    /**
         * 給文檔加上保護(hù)
         * 
         * 說明:
         * 
    @param pswd
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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);
            } 
        }
        
        
    /**
         * 給文檔解除保護(hù)
         * 
         * 說明:
         * 
    @param pswd
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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;

    /**
     * 一個(gè)文檔中選擇的部分
     * 說明:
     * 作者:何楊(heyang78@gmail.com)
     * 創(chuàng)建時(shí)間:2011-6-4 下午05:37:01
     * 修改時(shí)間: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;
        }

        
    /**
         * 設(shè)置選擇區(qū)的文字
         * 
         * 說明:
         * 
    @param text
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間:2011-6-4 下午05:41:28
         
    */
        
    public void setText(String text) throws Exception{
            Dispatch.put(instance, 
    "Text", text);
        }
        
        
    /**
         * 用新文字替換舊文字
         * 
         * 說明:
         * 
    @param oldText
         * 
    @param newText
         * 
    @throws Exception
         * 創(chuàng)建時(shí)間: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
         * 創(chuàng)建時(shí)間: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();
            
    // 設(shè)置要查找的內(nèi)容
            Dispatch.put(find, "Text", toFindText);
            
    // 向前查找
            Dispatch.put(find, "Forward""True");
            
    // 設(shè)置格式
            Dispatch.put(find, "Format""True");
            
    // 大小寫匹配
            Dispatch.put(find, "MatchCase""True");
            
    // 全字匹配
            Dispatch.put(find, "MatchWholeWord""False");
            
    // 查找并選中
            return Dispatch.call(find, "Execute").getBoolean();
        }
        
        
    /**
         * 插入圖片
         * 
         * 說明:
         * 
    @param imagePath
         * 創(chuàng)建時(shí)間:2011-6-4 下午07:17:18
         
    */
        
    public void insertPicture(String imagePath){
            Dispatch.call(Dispatch.get(instance, 
    "InLineShapes").toDispatch(),
                    
    "AddPicture", imagePath);
        }
    }

    6.測(cè)試代碼
    package com.ibm.ahcms2.tool.jacob.word;


    public class Test{
        
    public static void main(String[] args) throws Exception{
            test09();
        }
        
        
    // 設(shè)置頁(yè)眉,圖片水印以及設(shè)置保護(hù)
        public static void test09() throws Exception{
            Application app
    =new Application();
            
            Document doc
    =app.openExistDocument("c:\\2.doc");
            Selection sel
    =app.getSelection();
            
            
            doc.setHeaderText(
    "頁(yè)眉文字", sel.getInstance());
            doc.setImageWaterMark(
    "c:\\1.jpg", sel.getInstance(),20,20,200,200);
            doc.setProtected(
    "cms123");
            doc.save();
            
            doc.close();
            app.quit();
        }
        
        
    // 設(shè)置圖片水印
        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();
        }
        
        
    // 解除密碼保護(hù)
        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();
        }
        
        
    // 設(shè)置密碼保護(hù)
        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();
        }
        
        
    // 設(shè)置表頭
        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();
        }
        
        
    // 打開文檔,重新設(shè)置文字并保存
        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();
        }
        
        
    // 新建文檔,設(shè)置文檔并另存為
        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://m.tkk7.com/Files/heyang/JacobTest20110604201829.rar

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

    Feedback

    # 如果只是想給word文檔最后一頁(yè)添加水印呢? 2012-06-05 09:42 魯薇
    如果只是想給word文檔最后一頁(yè)添加水印呢?求指點(diǎn)。hateluwei@qq.com  回復(fù)  更多評(píng)論
      


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 在线观看av永久免费| 亚洲国产精品专区| 久久精品电影免费动漫| 男女作爱免费网站| 亚洲人AV在线无码影院观看| 久久久亚洲AV波多野结衣| 久久久久亚洲AV无码专区网站 | 国产成人亚洲综合色影视| 亚洲国产成人乱码精品女人久久久不卡 | 亚洲福利精品一区二区三区| 国产曰批免费视频播放免费s| 国产午夜亚洲精品不卡免下载| 亚洲熟妇无码一区二区三区导航| 亚洲男人的天堂在线| 777亚洲精品乱码久久久久久| 久久精品国产亚洲麻豆| 国外亚洲成AV人片在线观看| 亚洲精品国产福利一二区| 四虎精品亚洲一区二区三区| 国产免费私拍一区二区三区| 国产精品永久免费10000| 无码人妻丰满熟妇区免费| 免费国产99久久久香蕉| 免费在线观影网站| 免费91最新地址永久入口| 97人妻精品全国免费视频| 免费精品99久久国产综合精品| 九九九国产精品成人免费视频| 亚洲成a人片在线不卡一二三区| 亚洲av永久无码精品三区在线4| 亚洲日本中文字幕区| 亚洲三级电影网站| 亚洲国产精品网站久久| 亚洲国产日韩在线人成下载| 亚洲久悠悠色悠在线播放| 亚洲欧美日韩中文无线码| 亚洲国产精品精华液| 香港一级毛片免费看| 成在线人直播免费视频| 99在线免费观看| 在线观看AV片永久免费|