<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 :: 首頁 :: 聯(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的文檔集合對象
        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;


    /**
     * 文檔對象
     * 說明:
     * 作者:何楊(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è)置頁眉的文字
         * 
         * 說明:
         * 
    @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();
            
            
    // 獲取水印圖形對象
            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();
            
            
    // 獲取水印圖形對象
            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.測試代碼
    package com.ibm.ahcms2.tool.jacob.word;


    public class Test{
        
    public static void main(String[] args) throws Exception{
            test09();
        }
        
        
    // 設(shè)置頁眉,圖片水印以及設(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(
    "頁眉文字", 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 何楊 閱讀(4790) 評論(1)  編輯  收藏

    Feedback

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


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲AV无码一区二区三区网址 | 香港一级毛片免费看| 亚洲va在线va天堂成人| 亚洲精品国产电影午夜| 亚洲精品网站在线观看你懂的| 老司机亚洲精品影院无码| 亚洲国产精品自在线一区二区| 青青草原精品国产亚洲av| 亚洲国产日韩在线人成下载| 亚洲激情视频图片| 伊人久久五月丁香综合中文亚洲| 亚洲日本中文字幕天天更新| 亚洲国产精华液2020| 青草青草视频2免费观看| 二级毛片免费观看全程| 久久久久久久久久免免费精品| 中国极品美軳免费观看| 13小箩利洗澡无码视频网站免费| 久久狠狠躁免费观看2020| 99热在线观看免费| 午夜性色一区二区三区免费不卡视频| 1024免费福利永久观看网站| 大香人蕉免费视频75| 免费成人av电影| 亚洲日产韩国一二三四区| 亚洲大尺度无码专区尤物| 亚洲欧洲日本天天堂在线观看| 亚洲avav天堂av在线网爱情| 亚洲AV无码资源在线观看| 手机永久免费的AV在线电影网| 成人爽a毛片免费| 国产成人精品免费视频网页大全| 在线中文高清资源免费观看| 亚洲国产婷婷香蕉久久久久久| 亚洲国产精品无码久久久秋霞2 | 久久亚洲精品成人777大小说| 亚洲性猛交xx乱| 国产亚洲漂亮白嫩美女在线| 三级网站在线免费观看| 在线永久看片免费的视频| 国产一卡二卡≡卡四卡免费乱码|