当前位置:网站首页>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
边栏推荐
- [effective go Chinese translation] part I
- clang 如何产生汇编文件
- Using qlst excel file
- 分组背包呀
- AAAI 2022 recruit speakers!!
- 利用Js实现一个千分位
- The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
- Talk about the basic but not simple stock data
- QFileDialog 选择多个文件或文件夹
- [C语言] 文件操作《一》
猜你喜欢

跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“

【深度好文】Flink SQL流批⼀体化技术详解(一)

freertos学习02-队列 stream buffer message buffer

AAAI 2022招募讲者啦!!

A simple theme of Typecho with beautiful appearance_ Scarfskin source code download

关于ORB——SLAM运行中关键帧位置越来越近的异常说明

Distributed service governance Nacos

Smart business card applet business card details page function implementation key code

CSV column extract column extraction

1216_ MISRA_ C standard learning notes_ Rule requirements for control flow
随机推荐
为什么会存在1px问题?怎么解决?
一个必看的微信小程序开发指南1-基础知识了解
clang 如何产生汇编文件
分布式消息中间件框架选型-数字化架构设计(7)
LeetCode簡單題之計算字符串的數字和
An article understands variable lifting
二维01背包
redis主从服务器问题
AAAI 2022 recruit speakers!!
每周leetcode - 06 数组专题 7~739~50~offer 62~26~189~9
synchronized 实现原理
The simple problem of leetcode is to calculate the numerical sum of strings
Briefly describe the hierarchical strategy of memory
npm安装yarn
万物互联下如何对设备进行加密
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
There are some problems when using numeric type to query string type fields in MySQL
总线结构概述
An example of network communication based on TCP / IP protocol -- file transmission
Kubernetes in browser and IDE | interactive learning platform killercoda