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

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

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

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

    真正的快樂來源于創造

      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.tkk7.com/Files/heyang/JacobTest20110604201829.rar

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

    Feedback

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


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲电影一区二区| 美女免费视频一区二区三区| 国产精品免费观看久久| 日本系列1页亚洲系列| 久久亚洲高清观看| 成人啪精品视频免费网站| 国产免费久久精品丫丫| 亚洲人色大成年网站在线观看| 国产中文字幕免费| 99热在线免费观看| 特级毛片全部免费播放a一级 | 亚洲精品高清一二区久久| 无码日韩精品一区二区免费暖暖| 亚洲色大成网站www永久网站| 久久亚洲高清综合| 啦啦啦高清视频在线观看免费| eeuss影院免费直达入口| tom影院亚洲国产一区二区| 亚洲免费在线观看| 成年女人喷潮毛片免费播放| 一个人免费视频观看在线www| 久久亚洲AV成人无码国产电影| 亚洲电影中文字幕| 国产91精品一区二区麻豆亚洲 | 一本久久综合亚洲鲁鲁五月天| 中文字幕免费视频一| 一级毛片aaaaaa视频免费看| 33333在线亚洲| 亚洲丁香色婷婷综合欲色啪| 一本色道久久综合亚洲精品高清| 四虎www免费人成| 2022久久国产精品免费热麻豆| 成人无码精品1区2区3区免费看| 亚洲精品宾馆在线精品酒店| 亚洲国色天香视频| 亚洲精品高清国产一久久| 4338×亚洲全国最大色成网站| 免费精品一区二区三区在线观看| 亚欧色视频在线观看免费| 污视频在线观看免费| 中文无码日韩欧免费视频|