jacob修改页眉水印
http://wenwen.soso.com/z/q380024230.htm
?
?
mport com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;public class WordBean
{
??? //代表一个word程序
??? ActiveXComponent msWordApp = null;
???
??? //代表进行word文件处理
??? Dispatch document = null;
???
??? public WordBean()
??? {
??????? if (msWordApp == null)
??????? {
??????????? ComThread.InitSTA();
??????????? msWordApp = new ActiveXComponent("Word.Application");
??????? }
??? }
???
??? //TODO 设置文档是是否前台打开word
??? public void setVisible(boolean visible)
??? {
??????? msWordApp.setProperty("Visible", new Variant(visible));
??????? //与上一句作用相同
??????? //?? Dispatch.put(msWordApp, "Visible", new Variant(visible));
??? }
???
??? //TODO 创建一个新的文档
??? public void createNewDocument()
??? {
??????? //document表示word的所有文档窗口,(word是多文档窗口应用程序)
??????? Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();
??????? //调用add方法来创建一个新的文档
??????? document = Dispatch.call(documents, "Add").toDispatch();
??? }
???
??? //TODO 打开一个word文档
??? public void openFile(String wordFilePath)
??? {
??????? Dispatch documents = Dispatch.get(msWordApp, "Documents").toDispatch();
??????? document = Dispatch.call(documents, "Open", wordFilePath, new Variant(true)/**是否进行版本转换**/
???????
??????? , new Variant(false)/**是否是只读**/
???????
??????? ).toDispatch();
??????? //??????? document =
??????? //??????????? Dispatch.invoke(documents,
??????? //??????????????? "Open",
??????? //??????????????? Dispatch.Method,
??????? //??????????????? new Object[] {wordFilePath, new Variant(true), new Variant(false)},
??????? //??????????????? new int[1]).toDispatch();
??? }
???????
??? /**
???? * 文档设置图片水印
???? *
???? * @param waterMarkPath
???? *??????????? 水印路径? */
??? public void setWaterMark(String waterMarkPath)
??? {
??????? Dispatch activeWindow = Dispatch.get(msWordApp, "ActiveWindow").toDispatch();
??????? // 取得活动窗格对象
??????? Dispatch activePan = Dispatch.get(activeWindow, "ActivePane").toDispatch();
??????? // 取得视窗对象
??????? Dispatch view = Dispatch.get(activePan, "View").toDispatch();
??????? // 打开页眉,值为9,页脚为10
??????? Dispatch.put(view, "SeekView", new Variant(9));
??????? Dispatch docSelection = Dispatch.get(activeWindow, "Selection").toDispatch();
??????? //获取页眉和页脚
??????? Dispatch headfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();
??????? // 获取水印图形对象
??????? Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();
??????? // 给文档全部加上水印,设置了水印效果,内容,字体,大小,是否加粗,是否斜体,左边距,上边距。
??????? //调用shapes对象的AddPicture方法将全路径为picname的图片插入当前文档
??????? Dispatch picture = Dispatch.call(shapes, "AddPicture", waterMarkPath).toDispatch();
????
??????? //选择当前word文档的水印
??????? Dispatch.call(picture, "Select");
??????? Dispatch.put(picture, "Left", new Variant(0));
??????? Dispatch.put(picture, "Top", new Variant(150));
??????? Dispatch.put(picture, "Width", new Variant(150));
??????? Dispatch.put(picture, "Height", new Variant(150));
???????
??????? //关闭页眉
??????? Dispatch.put(view, "SeekView", new Variant(0));
???????
??? }
???
??? public void save()
??? {
??????? Dispatch.call(document, "Save");
??? }
???
??? public void saveFileAs(String fileName)
??? {
??????? Dispatch.call(document, "SaveAs", fileName);
??? }
???
??? public void closeDocument()
??? {
??????? Dispatch.call(document, "Close", "-1");
??????? document = null;
??? }
???
??? public void closeWord()
??? {
??????? Dispatch.call(msWordApp, "Quit");
??????? ComThread.Release();
??????? msWordApp = null;
??????? document = null;
??? }
???
??? public static void main(String[] args)
??? {
??????? WordBean bean = new WordBean();
??????? bean.setVisible(false);
??????? bean.createNewDocument();
??????? bean.insertText("你好");
??????? bean.setWaterMark("c:\\pic.jpg");
??????? bean.saveFileAs("c:\\a.doc");
??????? bean.closeDocument();
??????? bean.closeWord();
???????
??? }
}