java利用jacob实现word文档水印操作

现已实现了jacob对word文件文字水印的添加,问题是水印是重复添加,如果原先文件中已添水印,会在文档某角落存在。想要效果:新水印添加时,替换掉原来水印,即只存在一个水... 现已实现了jacob对word文件文字水印的添加,问题是水印是重复添加,如果原先文件中已添水印,会在文档某角落存在。想要效果:新水印添加时,替换掉原来水印,即只存在一个水印。
问题已解决。
//查看word的开发者工具,使用shapeRange的Delete方法,可删除旧水印
Dispatch shapeRange_old = Dispatch.call(shapes, "Range",new Variant(1)).toDispatch();
Dispatch.call(shapeRange_old,"Delete");
展开
 我来答
电商运营的机会
2017-06-27 · 知道合伙人互联网行家
电商运营的机会
知道合伙人互联网行家
采纳数:436 获赞数:998
潭州教育网络科技有限公司最佳电商运营总监。

向TA提问 私信TA
展开全部
http://www.baidu.com/s?wd=52070475597
package com.ymo.word;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TestJacobWord {

private ActiveXComponent wrdCom = null;
private Dispatch doc = null;
private Dispatch activeDoc = null;
private Dispatch docSelect = null;
private Dispatch docs = null;
private static TestJacobWord instance = null;
private String docName = "";

public static TestJacobWord getInstance() {
if (instance == null) {
instance = new TestJacobWord();
}
return instance;
}

private boolean initWord() {
boolean flag = false;
ComThread.InitSTA();
wrdCom = new ActiveXComponent("word.Application");
try {
docs = wrdCom.getProperty("Documents").toDispatch();
wrdCom.setProperty("Visible", new Variant(false));
flag = true;
} catch (Exception e) {
flag = false;
e.printStackTrace();
}
return flag;
}

private void createNewDocument() {
doc = Dispatch.call(docs, "Add").toDispatch();
docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();
}

private void getActiveDoc() {
activeDoc = wrdCom.getProperty("ActiveWindow").toDispatch();
System.out.println(activeDoc.getProgramId());
}

private void openDocument(String docPath) {
if (this.doc != null) {
closeDocument();
}
this.doc = Dispatch.call(docs, "Open", docPath, new Variant(false),
new Variant(false)).toDispatch();
docSelect = Dispatch.get(wrdCom, "Selection").toDispatch();
}

private void closeDocument() {
if (doc != null) {
Dispatch.call(doc, "Save");
Dispatch.call(doc, "Close", new Variant(true));
doc = null;
}
}

private void setImgWaterMark(String waterMarkPath) {
Dispatch activePan = Dispatch.get(activeDoc, "ActivePane").toDispatch();
Dispatch view = Dispatch.get(activePan, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(9));
Dispatch headfooter = Dispatch.get(docSelect, "HeaderFooter")
.toDispatch();
// 取得图形对象
Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();
Dispatch pic = Dispatch.call(shapes, "AddPicture", waterMarkPath)
.toDispatch();

Dispatch.call(pic, "Select");
Dispatch.put(pic, "Left", new Variant(10));
Dispatch.put(pic, "Top", new Variant(200));
Dispatch.put(pic, "Width", new Variant(150));
Dispatch.put(pic, "Height", new Variant(80));

Dispatch.put(view, "SeekView", new Variant(0));
}

public void setTextWaterMark(String waterMarkStr) {
Dispatch activePan = Dispatch.get(activeDoc, "ActivePane").toDispatch();
Dispatch view = Dispatch.get(activePan, "View").toDispatch();
Dispatch.put(view, "SeekView", new Variant(9));
Dispatch headfooter = Dispatch.get(docSelect, "HeaderFooter")
.toDispatch();
Dispatch shapes = Dispatch.get(headfooter, "Shapes").toDispatch();
Dispatch selection = Dispatch.call(shapes, "AddTextEffect",
new Variant(9), waterMarkStr, "宋体", new Variant(1),
new Variant(false), new Variant(false), new Variant(0),
new Variant(0)).toDispatch();
Dispatch.call(selection, "Select");
Dispatch shapeRange = Dispatch.get(docSelect, "ShapeRange")
.toDispatch();
Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject
1");
Dispatch textEffect = Dispatch.get(shapeRange, "TextEffect")
.toDispatch();
Dispatch.put(textEffect, "NormalizedHeight", new Boolean(false));
Dispatch line = Dispatch.get(shapeRange, "Line").toDispatch();
Dispatch.put(line, "Visible", new Boolean(false));
Dispatch fill = Dispatch.get(shapeRange, "Fill").toDispatch();
Dispatch.put(fill, "Visible", new Boolean(true));
// 设置水印透明度
Dispatch.put(fill, "Transparency", new Variant(0.5));
Dispatch foreColor = Dispatch.get(fill, "ForeColor").toDispatch();
Dispatch.put(foreColor, "RGB", new Variant(16711620));
Dispatch.call(fill, "Solid");
// 设置水印旋转
Dispatch.put(shapeRange, "Rotation", new Variant(315));
Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));
Dispatch.put(shapeRange, "Height", new Variant(117.0709));
Dispatch.put(shapeRange, "Width", new Variant(468.2835));
Dispatch.put(shapeRange, "Left", new Variant(-999995));
Dispatch.put(shapeRange, "Top", new Variant(-999995));
Dispatch wrapFormat = Dispatch.get(shapeRange, "WrapFormat")
.toDispatch();
// 是否允许交叠
Dispatch.put(wrapFormat, "AllowOverlap", new Variant(true));
Dispatch.put(wrapFormat, "Side", new Variant(3));
Dispatch.put(wrapFormat, "Type", new Variant(3));
Dispatch.put(shapeRange, "RelativeHorizontalPositi
on", new Variant(0));
Dispatch.put(shapeRange, "RelativeVerticalPosition
", new Variant(0));
Dispatch.put(view, "SeekView", new Variant(0));
}

private void closeWord() {
// 关闭word文件
wrdCom.invoke("Quit", new Variant[] {});
// 释放com线程
ComThread.Release();
}

public String getDocName() {
return docName;
}

public void setDocName(String docName) {
this.docName = docName;
}

private boolean addWaterMark(String wordPath, String waterMarkPath) {
boolean flag = false;
try {
if (initWord()) {
openDocument(wordPath);
getActiveDoc();
setImgWaterMark(waterMarkPath);
closeDocument();
closeWord();
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
flag = false;
e.printStackTrace();
closeDocument();
closeWord();
}
return flag;
}

public static void main(String[] args) {
TestJacobWord jacob = TestJacobWord.getInstance();
// jacob.addWaterMark("F://test//test.doc", "F://test//ymo.jpg");
try {
if (jacob.initWord()) {
jacob.openDocument("F://test/test.doc");
jacob.getActiveDoc();
jacob.setTextWaterMark("重庆宇能科技有限公司");
jacob.closeDocument();
jacob.closeWord();
}
} catch (Exception e) {
e.printStackTrace();
jacob.closeDocument();
jacob.closeWord();
}

}
}
博思aippt
2024-07-20 广告
作为深圳市博思云创科技有限公司的工作人员,对于Word文档生成PPT的操作,我们有以下建议:1. 使用另存为功能:在Word中编辑完文档后,点击文件->另存为,选择PowerPoint演示文稿(*.pptx)格式,即可将文档内容转换为PPT... 点击进入详情页
本回答由博思aippt提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式