添加水印
This commit is contained in:
@@ -110,7 +110,7 @@ public class WordUtils {
|
||||
// 打开文档
|
||||
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
|
||||
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
||||
//addWatermark(wordFile,wordFile,mark);
|
||||
addWatermark(app,documents,document,wordFile,"D:\\anrui\\1.doc",mark);
|
||||
//setWaterMark(app,mark);
|
||||
File targetFile = new File(pdfPath);
|
||||
if (!targetFile.exists()) {
|
||||
@@ -139,6 +139,144 @@ public class WordUtils {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void addWatermark(ActiveXComponent wordApp, Dispatch documents,Dispatch doc,String src, String des, String text) throws Exception {
|
||||
ComThread.InitMTA();
|
||||
// 文档隐藏时进行应用操作
|
||||
wordApp.setProperty("Visible", new Variant(false));
|
||||
// 实例化模板Workbooks对象
|
||||
//打开文件
|
||||
Dispatch docSelect = Dispatch.get(wordApp, "Selection").toDispatch();
|
||||
//取得活动窗体对象
|
||||
Dispatch activeDoc = wordApp.getProperty("ActiveWindow").toDispatch();
|
||||
try {
|
||||
//添加水印,多个水印
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
//取得活动窗体对象
|
||||
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), text, "宋体", new Variant(10),
|
||||
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 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(11));
|
||||
Dispatch.put(shapeRange, "Width", new Variant(90));
|
||||
//设置水印位置,相对左上角
|
||||
Dispatch.put(shapeRange, "Left", new Variant(j * 120 - 60));
|
||||
Dispatch.put(shapeRange, "Top", new Variant(i * 270));
|
||||
//恢复视图
|
||||
Dispatch.put(view, "SeekView", new Variant(0));
|
||||
}
|
||||
}
|
||||
//另存为目标文件
|
||||
Dispatch.call(doc, "SaveAS",des,new Variant(true) );
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
/* Dispatch.call(doc, "Close", new Variant(-1),new Variant(1),new Variant(true));
|
||||
if (wordApp != null) {
|
||||
wordApp.invoke("Quit", new Variant[]{});
|
||||
}
|
||||
ComThread.Release();*/
|
||||
// 关闭文档
|
||||
// Dispatch.call(doc, "Close", false);
|
||||
// 关闭office
|
||||
// wordApp.invoke("Quit", 0);
|
||||
System.out.println("关闭文档");
|
||||
// if (app != null)
|
||||
// app.invoke("Quit", new Variant[]{});
|
||||
// 如果没有这句话,winword.exe进程将不会关闭
|
||||
// ComThread.Release();
|
||||
}
|
||||
}
|
||||
/*
|
||||
public static void setWaterMark(ActiveXComponent activeWindow,String waterMarkStr)
|
||||
{
|
||||
|
||||
|
||||
// 取得活动窗格对象
|
||||
Dispatch activePan = Dispatch.get(activeWindow, "ActivePane").toDispatch();
|
||||
// 取得视窗对象
|
||||
Dispatch view = Dispatch.get(activePan, "View").toDispatch();
|
||||
Dispatch docSelection=Dispatch.get(activeWindow, "Selection").toDispatch();
|
||||
//输入页眉内容
|
||||
Dispatch.put(view, "SeekView", new Variant(9));
|
||||
Dispatch headfooter = Dispatch.get(docSelection, "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(docSelection, "ShapeRange")
|
||||
.toDispatch();
|
||||
Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject1");
|
||||
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(16711680));
|
||||
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, "RelativeHorizontalPosition", new Variant(0));
|
||||
Dispatch.put(shapeRange, "RelativeVerticalPosition", new Variant(0));
|
||||
Dispatch.put(view, "SeekView", new Variant(0));
|
||||
}*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
Map<String, Object> dataMap = new HashMap<String, Object>();
|
||||
String curDate = DateUtils.dateConvertStr(new Date(), "yyyy年MM月dd日");
|
||||
@@ -212,136 +350,4 @@ public class WordUtils {
|
||||
this.remarks = remarks;
|
||||
}
|
||||
}
|
||||
public static void addWatermark(String src, String des, String text) throws Exception {
|
||||
ComThread.InitMTA();
|
||||
ActiveXComponent wordApp = new ActiveXComponent("word.Application");
|
||||
// 文档隐藏时进行应用操作
|
||||
wordApp.setProperty("Visible", new Variant(false));
|
||||
// 实例化模板Workbooks对象
|
||||
Dispatch documents = wordApp.getProperty("Documents").toDispatch();
|
||||
//打开文件
|
||||
Dispatch doc = Dispatch.call(documents, "Open", src, new Variant(false),new Variant(false)).toDispatch();
|
||||
Dispatch docSelect = Dispatch.get(wordApp, "Selection").toDispatch();
|
||||
//取得活动窗体对象
|
||||
Dispatch activeDoc = wordApp.getProperty("ActiveWindow").toDispatch();
|
||||
try {
|
||||
//添加水印,多个水印
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
//取得活动窗体对象
|
||||
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), text, "宋体", new Variant(10),
|
||||
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 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(11));
|
||||
Dispatch.put(shapeRange, "Width", new Variant(90));
|
||||
//设置水印位置,相对左上角
|
||||
Dispatch.put(shapeRange, "Left", new Variant(j * 120 - 60));
|
||||
Dispatch.put(shapeRange, "Top", new Variant(i * 270));
|
||||
//恢复视图
|
||||
Dispatch.put(view, "SeekView", new Variant(0));
|
||||
}
|
||||
}
|
||||
//另存为目标文件
|
||||
Dispatch.call(doc, "SaveAS",des,new Variant(true) );
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
Dispatch.call(doc, "Close", new Variant(-1),new Variant(1),new Variant(true));
|
||||
if (wordApp != null) {
|
||||
wordApp.invoke("Quit", new Variant[]{});
|
||||
}
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
/*
|
||||
public static void setWaterMark(ActiveXComponent activeWindow,String waterMarkStr)
|
||||
{
|
||||
|
||||
|
||||
// 取得活动窗格对象
|
||||
Dispatch activePan = Dispatch.get(activeWindow, "ActivePane").toDispatch();
|
||||
// 取得视窗对象
|
||||
Dispatch view = Dispatch.get(activePan, "View").toDispatch();
|
||||
Dispatch docSelection=Dispatch.get(activeWindow, "Selection").toDispatch();
|
||||
//输入页眉内容
|
||||
Dispatch.put(view, "SeekView", new Variant(9));
|
||||
Dispatch headfooter = Dispatch.get(docSelection, "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(docSelection, "ShapeRange")
|
||||
.toDispatch();
|
||||
Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject1");
|
||||
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(16711680));
|
||||
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, "RelativeHorizontalPosition", new Variant(0));
|
||||
Dispatch.put(shapeRange, "RelativeVerticalPosition", new Variant(0));
|
||||
Dispatch.put(view, "SeekView", new Variant(0));
|
||||
}*/
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user