当前位置:网站首页>word加水印
word加水印
2022-04-23 07:34:00 【beinlife】
word加水印
-
相关的jar包:jacob-1.17-x64.rar
- 注意区分32位和64系统
- 复制 jacob-1.17-M4-x64.dll 放在java jdk bin目录下、系统目录/WINDOWS/system32目录下
代码如下:
public class TestWaterWord {
public TestWaterWord() {}
private static TestWaterWord instance;
private Dispatch aDoc = null;//用于存储一个文档:比如新增一个文档时返回,新增的文档
private Dispatch activeWindow = null;//当前活动窗口
private Dispatch docSelection = null;//存储当前被选中文档
private Dispatch wordDocs = null;//存储所有的文档
private String fileName;
private ActiveXComponent wordApp;//Word对象
public final static synchronized TestWaterWord getInstance() {
if (instance == null){
instance = new TestWaterWord();
}
return instance;
}
/*** 初始化Word对象*/
public boolean initWordApp() {
boolean retFlag = false;
//初始化com线程【相当于打开冰箱门,准备放大箱】
//使用结束后要调用 realease方法关闭线程【相当于关上冰箱门】
ComThread.InitSTA();
wordApp = new ActiveXComponent("Word.Application");// 初始化word应用程序,初始化表格是:Excel.Application
try {
wordApp.setProperty("Visible", new Variant(false));//配置启动word时是显示执行还是隐式执行
wordDocs = wordApp.getProperty("Documents").toDispatch();// 获取word所有文档对象
retFlag = true;
} catch (Exception e) {
retFlag = false;
e.printStackTrace();
}
return retFlag;
}
/**打开一个已存在的文档*/
public void openDocument(String docPath) {
if (this.aDoc != null) {
this.closeDocument();
}
aDoc = Dispatch.call(wordDocs,"Open",new Variant(docPath)).toDispatch();//docPath要打开的文档的详细地址
docSelection = Dispatch.get(wordApp, "Selection").toDispatch();//获得该文档对象,并返回
}
/** 取得活动窗体对象*/
public void getActiveWindow() {
activeWindow = wordApp.getProperty("ActiveWindow").toDispatch();// 取得活动窗体对象
}
/***创建一个新的word文档*/
public void createNewDocument() {
aDoc = Dispatch.call(wordDocs, "Add").toDispatch();//创建一个新的word文档,并返回
docSelection = Dispatch.get(wordApp, "Selection").toDispatch();//获得该文档对象,并返回
}
/*** 保存并关闭当前word文档*/
public void closeDocument() {
if (aDoc != null) {
Dispatch.call(aDoc, "Save");//保存
Dispatch.call(aDoc, "Close", new Variant(0));//关闭
aDoc = null;
}
}
/*** 关闭Word资源*/
public void closeWordObj() {
wordApp.invoke("Quit", new Variant[] {});// 关闭word文件
ComThread.Release();// 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
/** * 插入图片 * @param pages 总页数 * @param imgPath 图片路径 * @param left 距离左上角位置 * @param top 距离上角位置 */
public void setImages(int pages,String imgPath,int left,int top){
for(int i = 0; i < pages;i++){
Dispatch selection = Dispatch.get(wordApp, "Selection").toDispatch();
Dispatch inLineShapes = Dispatch.get(selection, "InLineShapes").toDispatch();
Dispatch picture = Dispatch.call(inLineShapes, "AddPicture", imgPath).toDispatch();
//选中图片
Dispatch.call(picture, "Select");
//设置宽度高度
Dispatch.put(picture, "Width", new Variant(100));
Dispatch.put(picture, "Height", new Variant(100));
//设置图片相对左上角偏移位置
selection = Dispatch.get(wordApp, "Selection").toDispatch();
Dispatch shapeRange = Dispatch.get(selection, "ShapeRange").toDispatch();
Dispatch.put(shapeRange, "Left", new Variant(left));
Dispatch.put(shapeRange, "Top", new Variant(top));
//翻到下一页
Dispatch browser = Dispatch.get(wordApp, "Browser").toDispatch();
Dispatch.call(browser, "Next");
}
}
/** * 设置页眉 * @param waterMarkStr */
public void setHeader(String headerStr) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// 活动窗格
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// 视窗对象
Dispatch.put(view, "SeekView", new Variant(9));// 打开页眉,值为9,页脚值为10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// 获取页眉和页脚
Dispatch range = Dispatch.get(headerfooter, "Range").toDispatch();//页眉赋值
Dispatch.put(range, "Text", new Variant(headerStr));
Dispatch font = Dispatch.get(range, "Font").toDispatch();//设置字体
Dispatch.put(font, "Name", new Variant("微软雅黑"));
Dispatch.put(font, "Bold", new Variant(true));
Dispatch.put(font, "Size", 20);
Dispatch.put(font, "Color", Integer.valueOf("0000FF",16).toString());//颜色是16进制倒着写,然后转10进制
Dispatch.put(view, "SeekView", new Variant(0)); //0表示恢复视图;
}
/** 文档设置图片水印,waterPic水印图片路径*/
public void setWaterPic(String waterPic) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// 活动窗格
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// 视窗对象
Dispatch.put(view, "SeekView", new Variant(9));// 打开页眉,值为9,页脚值为10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// 获取页眉和页脚
Dispatch shapes = Dispatch.get(headerfooter, "Shapes").toDispatch();// 获取水印图形对象
//调用shapes对象的AddPicture方法将图片插入当前文档
Dispatch picture = Dispatch.call(shapes,"AddPicture",waterPic).toDispatch();
Dispatch.call(picture, "Select");//选择当前word文档的图片水印
Dispatch.put(picture, "Left", new Variant(120));//设置图片水印参数
Dispatch.put(picture, "Top", new Variant(240));
Dispatch.put(picture, "LockAspectRatio", new Boolean(true));//调整大小时保持其长宽比例不变
Dispatch.put(picture, "Width", new Variant(120));
Dispatch.put(picture, "Height", new Variant(90));
Dispatch.put(view, "SeekView", new Variant(0));//关闭页眉,0表示恢复视图;
}
/** 文档设置文字水印--实质是设置页眉,将文字转为艺术字图片*/
public void setWaterMark(String waterMarkStr) {
Dispatch activePane = Dispatch.get(activeWindow, "ActivePane").toDispatch();// 活动窗格
Dispatch view = Dispatch.get(activePane, "View").toDispatch();// 视窗对象
Dispatch.put(view, "SeekView", new Variant(9));// 打开页眉,值为9,页脚值为10
Dispatch headerfooter = Dispatch.get(docSelection, "HeaderFooter").toDispatch();// 获取页眉和页脚
Dispatch shapes = Dispatch.get(headerfooter, "Shapes").toDispatch();// 获取水印图形对象
/**插入文字,并转为图片: * 操作对象、方法、艺术字格式[0白色、1黑左下右上,2黑中上两下、3黑中小两大、4黑竖排、5黑中下两上] * 水印内容、字体、字体大小、字体是否粗体、字体是否斜体 * 左边距、上边距 */
Dispatch selection = Dispatch.call(shapes, "AddTextEffect",new Variant(0),
waterMarkStr, "微软雅黑", new Variant(10),new Variant(true), new Variant(false),
new Variant(150),new Variant(250)).toDispatch();
//选中当前文档水印
Dispatch.call(selection, "Select");
Dispatch shapeRange = Dispatch.get(docSelection, "ShapeRange").toDispatch();
/** * 设置水印透明度和颜色 */
Dispatch.put(shapeRange, "Name", "PowerPlusWaterMarkObject6");
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.1));// 设置水印透明度
Dispatch foreColor = Dispatch.get(fill, "ForeColor").toDispatch();
Dispatch.put(foreColor, "RGB", new Variant(255));// 设置水印颜色
Dispatch.call(fill, "Solid");
/** * 设置水印旋转角度、水印大小 */
Dispatch.put(shapeRange, "Rotation", new Variant(0));//旋转角度
Dispatch.put(shapeRange, "LockAspectRatio", new Boolean(true));//调整大小时保持其长宽比例不变
Dispatch.put(shapeRange, "Height", new Variant(10));//高
Dispatch.put(shapeRange, "Width", new Variant(40));//宽
Dispatch.put(shapeRange, "Left", new Variant(160));
Dispatch.put(shapeRange, "Top", new Variant(270));
Dispatch.put(view, "SeekView", new Variant(0));//0表示恢复视图;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//测试功能
public static void main(String[] argv) {
TestWaterWord d = TestWaterWord.getInstance();
try {
if (d.initWordApp()) {
d.openDocument("C:/Users/solexit06/Desktop/testWater.docx");
d.getActiveWindow();
String imgPath="C:/Users/solexit06/Desktop/Border.png";
d.setWaterPic(imgPath);//页中图片
Date date=Calendar.getInstance().getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
String waterDate=sdf.format(date);
d.setWaterMark(waterDate);//日期
d.setHeader("****科技");//页眉
d.closeDocument();
} else{
System.out.println("初始化Word读写对象失败!");
}
} catch (Exception e) {
System.out.println(e);
}finally{
d.closeWordObj();
}
}
}
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/53514064
边栏推荐
- freertos学习02-队列 stream buffer message buffer
- Weekly leetcode - 06 array topics 7 ~ 739 ~ 50 ~ offer 62 ~ 26 ~ 189 ~ 9
- LeetCode简单题之三除数
- 5.6 comprehensive case - RTU-
- Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
- Multi vision slam
- 【学习】从零开始的音视频开发(9)——NuPlayer
- 1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
- LeetCode简单题之计算字符串的数字和
- 情境领导者-第七章、解决绩效问题
猜你喜欢
WordPress love navigation theme 1.1.3 simple atmosphere website navigation source code website navigation source code
[learning] audio and video development from scratch (9) -- nuplayer
CSV column extract column extraction
synchronized 实现原理
One click cleanup of pycharm and jupyter cache files under the project
The simple problem of leetcode is to calculate the numerical sum of strings
2022.4.11-4.17 AI industry weekly (issue 93): the dilemma of AI industry
Qt编译QtXlsx库
AAAI 2022 recruit speakers!!
nn.Module类的讲解
随机推荐
LeetCode简单题之统计字符串中的元音子字符串
dmp引擎工作总结(2021,光剑)
How to read books and papers
C language learning record -- use and analysis of string function (2)
Jetson Xavier NX (3) bazel mediapipe installation
Data deletion and modification (MySQL)
vslam PPT
The whole house intelligence bet by the giant is driving the "self revolution" of Hisense, Huawei and Xiaomi
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
Briefly describe the hierarchical strategy of memory
谈谈那些基础但不简单的股票数据
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
[appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
dried food! Point based: differentiable Poisson solver
編譯原理題-帶答案
Rotation function of leetcode medium problem
Compiler des questions de principe - avec des réponses
Green apple film and television system source code film and television aggregation film and television navigation film and television on demand website source code
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍