Java如何给Word文档添加多行文字水印
作者:E-iceblue 时间:2023-11-10 07:40:58
前言
我在以往的文章中曾介绍过如何给Word文档添加文本水印和图片水印,及怎样删除文档中的水印。关于文本水印,之前那篇教程里主要指的是单行字体的水印,而在操作Word文档时,有时也会碰到需要添加多行文字水印的情况。所以,本文将演示如何使用Java程序来为Word文档添加多行文字水印。
代码测试环境:
Intellij Idea2019.1
JDK 1.8.0
Spire.Doc.jar
Jar包获取及导入
方法一:手动将Jar包导入IDEA。具体步骤如下:
步骤 1:在 E-iceblue中文官网注册并登陆账号,进入Free Spire.Doc for Java下载页面下载产品包。
步骤 2:解压产品包,找到lib文件下的Spire.Doc.jar。
步骤 3:将Spire.Doc.jar手动导入IDEA中。
方法二: 通过Maven仓库安装导入产品及相关依赖。创建一个Maven应用程序,在pom.xml文件中配置Maven仓库路径及指定Spire.Doc for Java的Maven依赖。
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.doc.free</artifactId>
<version>2.7.3</version>
</dependency>
</dependencies>
配置完成后,在IDEA中,您需点击"Import Changes"即可导入JAR包;在Eclipse中,则需要点击"Save"按钮。
代码演示
Free Spire.Doc for Java通过在Word页眉中添加艺术字来实现为文档添加多行文字水印。具体代码如下:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;
import java.awt.*;
public class multiTextWatermark {
public static void main(String[] args) {
//加载示例文档
Document doc = new Document();
doc.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample.docx");
//添加艺术字并设置大小
ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
shape.setWidth(60);
shape.setHeight(20);
//设置艺术字文本内容、颜色,位置及样式
shape.setVerticalPosition(30);
shape.setHorizontalPosition(20);
shape.setRotation(315);
shape.getWordArt().setText("内部使用");
shape.setFillColor(Color.green);
shape.setLineStyle(ShapeLineStyle.Single);
shape.setStrokeColor(new Color(192, 192, 192, 255));
shape.setStrokeWeight(1);
Section section;
HeaderFooter header;
for (int n = 0; n < doc.getSections().getCount(); n++) {
section = doc.getSections().get(n);
//获取section的页眉
header = section.getHeadersFooters().getHeader();
Paragraph paragraph1;
for (int i = 0; i < 4; i++) {
//添加段落到页眉
paragraph1 = header.addParagraph();
for (int j = 0; j < 3; j++) {
//复制艺术字并设置多行多列位置
shape = (ShapeObject) shape.deepClone();
shape.setVerticalPosition(50 + 150 * i);
shape.setHorizontalPosition(20 + 160 * j);
paragraph1.getChildObjects().add(shape);
}
}
}
//保存文档
doc.saveToFile("output/multi-lineTextwatermark.docx", FileFormat.Docx_2013);
}
}
添加效果:
总结:
通过运用以上代码,多行文字水印能够完整清晰准确地 * 入到Word文档中。若对代码或Jar包导入有疑问的,可评论或私信。
来源:https://www.cnblogs.com/Yesi/p/13627502.html
标签:Java,Word,文字水印
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现日期拆分的方法
2023-06-19 00:28:59
Android Flutter实现仿闲鱼动画效果
2023-07-15 15:32:47
![](https://img.aspxhome.com/file/2023/3/87323_0s.jpg)
一篇文章带你了解java接口与继承
2022-07-15 02:44:08
Java常用排序算法及性能测试集合
2022-01-15 22:29:09
SpringBoot 文件上传和下载的实现源码
2021-05-28 14:12:46
![](https://img.aspxhome.com/file/2023/1/85531_0s.png)
Android多渠道打包的方法步骤
2022-10-17 12:05:07
![](https://img.aspxhome.com/file/2023/4/91024_0s.png)
Java时间复杂度、空间复杂度的深入详解
2023-10-22 01:22:40
![](https://img.aspxhome.com/file/2023/5/69915_0s.png)
Java实现图片拼接
2023-02-28 23:01:27
SpringBoot实现动态配置及项目打包部署上线功能
2021-07-13 21:36:53
![](https://img.aspxhome.com/file/2023/0/127220_0s.png)
浅谈单例模式和线程安全问题
2023-11-25 06:27:34
![](https://img.aspxhome.com/file/2023/2/98402_0s.jpg)
Android中的全局变量与局部变量使用小结
2023-12-07 10:11:49
![](https://img.aspxhome.com/file/2023/8/130318_0s.jpg)
JAVA8 十大新特性详解
2023-07-02 10:03:27
Ajax 验证用户输入的验证码是否与随机生成的一致
2022-06-29 00:43:32
Android中Window的管理深入讲解
2022-03-18 17:48:28
详解Java使用JMH进行基准性能测试
2021-08-01 12:22:32
Java多线程之Worker Thread模式
2021-06-11 12:11:51
C#使用log4net打日志
2022-11-08 17:50:57
![](https://img.aspxhome.com/file/2023/3/76403_0s.png)
Java简易登录注册小程序
2023-01-23 09:16:13
Spring5学习之基础知识总结
2021-08-16 02:19:15
![](https://img.aspxhome.com/file/2023/0/104000_0s.png)
Mybatis插件之自动生成不使用默认的驼峰式操作
2023-11-19 01:20:03