word模板中添加图片

来源:互联网 发布:怎样在淘宝买到好衣服 编辑:程序博客网 时间:2024/05/07 18:43

http://www.iteye.com/problems/72865


1.打开word文件 

Java代码  收藏代码
  1. /** 
  2.      * 打开一个已存在的文档 
  3.      *  
  4.      * @param docPath 
  5.      */  
  6.     public void openDocument(String docPath)  
  7.     {  
  8.         doc = Dispatch.call(documents, "Open", docPath).toDispatch();  
  9.         selection = Dispatch.get(word, "Selection").toDispatch();  
  10.         System.out.println("open a word file!!");  
  11.     }  

2.在指定位置插入图片(a.通过在要添加图片的位置设置书签   b.在光标位置插入图片) 
a方法 
Java代码  收藏代码
  1. /** 
  2.      * 在指定书签的添加图片 
  3.      *  
  4.      * @param bookMarkKey 
  5.      *            书签名 
  6.      * @param imagePath 
  7.      *            图片路径 
  8.      */  
  9.     public void addImageAtBookMark(String bookMarkKey, String imagePath)  
  10.     {  
  11.         Dispatch activeDocument = word.getProperty("ActiveDocument")  
  12.                 .toDispatch();  
  13.         // 得到文档所有的书签  
  14.         Dispatch bookMarks = word.call(activeDocument, "Bookmarks")  
  15.                 .toDispatch();  
  16.         // 获得指定的书签  
  17.         boolean bookMarkExist1 = Dispatch  
  18.                 .call(bookMarks, "Exists", bookMarkKey).toBoolean();  
  19.         if (bookMarkExist1 == true)  
  20.         {  
  21.             System.out.println("已找到书签 " + bookMarkKey);  
  22.             Dispatch rangeItem = Dispatch.call(bookMarks, "Item", bookMarkKey)  
  23.                     .toDispatch();  
  24.             Dispatch range = Dispatch.call(rangeItem, "Range").toDispatch();  
  25.             // 在书签选中范围内插入图片  
  26.             Dispatch.call(Dispatch.get(range, "InLineShapes").toDispatch(),  
  27.                     "AddPicture", imagePath);  
  28.   
  29.         }  
  30.         else  
  31.         {  
  32.             System.out.println("指定的书签不存在 " + bookMarkKey);  
  33.         }  
  34.     }  

b方法 
Java代码  收藏代码
  1. /** 
  2.      * 在当前插入点插入图片 
  3.      *  
  4.      * @param imagePath 
  5.      *            图片路径 
  6.      */  
  7.     public void insertImage(String imagePath)  
  8.     {  
  9.         Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),  
  10.                 "AddPicture", imagePath);  
  11.     }  

b方法徐结合光标的移动 
Java代码  收藏代码
  1. /** 
  2.      * 把选定的内容或插入点向上移动 
  3.      *  
  4.      * @param pos 
  5.      *            移动的距离 
  6.      */  
  7.     public void moveUp(int pos)  
  8.     {  
  9.         if (selection == null)  
  10.             selection = Dispatch.get(word, "Selection").toDispatch();  
  11.         for (int i = 0; i < pos; i++)  
  12.             Dispatch.call(selection, "MoveUp");  
  13.     }  
  14.   
  15.     /** 
  16.      * 把选定的内容或者插入点向下移动 
  17.      *  
  18.      * @param pos 
  19.      *            移动的距离 
  20.      */  
  21.     public void moveDown(int pos)  
  22.     {  
  23.         if (selection == null)  
  24.             selection = Dispatch.get(word, "Selection").toDispatch();  
  25.         for (int i = 0; i < pos; i++)  
  26.             Dispatch.call(selection, "MoveDown");  
  27.     }  
  28.   
  29.     /** 
  30.      * 把选定的内容或者插入点向左移动 
  31.      *  
  32.      * @param pos 
  33.      *            移动的距离 
  34.      */  
  35.     public void moveLeft(int pos)  
  36.     {  
  37.         if (selection == null)  
  38.             selection = Dispatch.get(word, "Selection").toDispatch();  
  39.         for (int i = 0; i < pos; i++)  
  40.         {  
  41.             Dispatch.call(selection, "MoveLeft");  
  42.         }  
  43.     }  
  44.   
  45.     /** 
  46.      * 把选定的内容或者插入点向右移动 
  47.      *  
  48.      * @param pos 
  49.      *            移动的距离 
  50.      */  
  51.     public void moveRight(int pos)  
  52.     {  
  53.         if (selection == null)  
  54.             selection = Dispatch.get(word, "Selection").toDispatch();  
  55.         for (int i = 0; i < pos; i++)  
  56.             Dispatch.call(selection, "MoveRight");  
  57.     }  
  58.   
  59.     /** 
  60.      * 把插入点移动到文件首位置 
  61.      *  
  62.      */  
  63.     public void moveStart()  
  64.     {  
  65.         if (selection == null)  
  66.             selection = Dispatch.get(word, "Selection").toDispatch();  
  67.         Dispatch.call(selection, "HomeKey"new Variant(6));  
  68.     }  
  69.   
  70.     public void moveEnd()  
  71.     {  
  72.         if (selection == null)  
  73.             selection = Dispatch.get(word, "Selection").toDispatch();  
  74.         Dispatch.call(selection, "EndKey"new Variant(6));  
  75.     }  

3.关闭word 
 
Java代码  收藏代码
  1. /** 
  2.      * 文件保存或另存为 
  3.      *  
  4.      * @param savePath 
  5.      *            保存或另存为路径 
  6.      */  
  7.     public void save(String savePath)  
  8.     {  
  9.         Dispatch.call(  
  10.                 (Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),  
  11.                 "FileSaveAs", savePath);  
  12.     }  
  13.   
  14.     /** 
  15.      * 关闭当前word文档 
  16.      *  
  17.      */  
  18.     public void closeDocument()  
  19.     {  
  20.         if (doc != null)  
  21.         {  
  22.             Dispatch.call(doc, "Save");  
  23.             Dispatch.call(doc, "Close"new Variant(saveOnExit));  
  24.             doc = null;  
  25.             System.out.println("close a word file!!");  
  26.         }  
  27.     }  
  28.   
  29.     /** 
  30.      * 关闭全部应用 
  31.      *  
  32.      */  
  33.     public void closeWord()  
  34.     {  
  35.   
  36.         if (word != null)  
  37.         {  
  38.             Dispatch.call(word, "Quit"new Variant(false));  
  39.             word = null;  
  40.         }  
  41.         selection = null;  
  42.         documents = null;  
  43.     }  


0 0
原创粉丝点击