最近要做电子合同,客户提出为了安全性要将合同中都添加水印,这个之前在网上看到过,貌似使用POI很好加。去网上一搜发现,清一色的只有一篇文章,并且这段代码是用不了的;在文章下边的评论里也发现都说用不了,不能用。唉,木办法了,只能自己探索。

1、pom依赖:

<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
</dependencies>

废话不多说,上demo;

2、代码:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;

import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;

public class Test1 {

public static void main(String[] args) throws Exception {

//输入的docx文档
InputStream in = new FileInputStream(new File("D:/aa.docx"));
XWPFDocument doc= new XWPFDocument(in);

// the body content
XWPFParagraph paragraph = doc.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body:");

// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy = doc.getHeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = doc.createHeaderFooterPolicy();

// 水印内容
headerFooterPolicy.createWatermark("WaterMaker");

// get the default header
// Note: createWatermark also sets FIRST and EVEN headers
// but this code does not updating those other headers
XWPFHeader header = headerFooterPolicy.getHeader(XWPFHeaderFooterPolicy.DEFAULT);
paragraph = header.getParagraphArray(0);

// get com.microsoft.schemas.vml.CTShape where fill color and rotation is set
org.apache.xmlbeans.XmlObject[] xmlobjects = paragraph.getCTP().getRArray(0).getPictArray(0).selectChildren(
new javax.xml.namespace.QName("urn:schemas-microsoft-com:vml", "shape"));

if (xmlobjects.length > 0) {
com.microsoft.schemas.vml.CTShape ctshape = (com.microsoft.schemas.vml.CTShape)xmlobjects[0];
// set fill color
ctshape.setFillcolor("#d8d8d8");
// set rotation
ctshape.setStyle(ctshape.getStyle() + ";rotation:315");
//System.out.println(ctshape);
}
//文件输出地址
FileOutputStream out = new FileOutputStream("D:\\watermark.docx");
System.out.println("水印添加成功!");
doc.write(out);
out.close();
doc.close();
}
}

3、总结

虽然实现了,但是还是比较简陋;水印的字体、大小、颜色等都木有设置,用的都是默认的;这是以后可以优化的地方。不过整体效果还是可以的,而且这样添加水印后生成pdf也是带有水印的。至于生成pdf的代码后边有时间再写吧,谁想要可以在评论区给我留言,我看到了就把demo发给你。



最新文章

  1. 通过数组和枚举简化GPIO操作编码
  2. Thread基本介绍
  3. Android Studio 引入Lambda表达式
  4. Mongo中append方法使用
  5. EXchange2010配置
  6. 使用Git push时出现的一些问题处理
  7. Unity3d 使用DX11的曲面细分
  8. 在Nginx上配置ThinkPHP项目
  9. Hive 执行计划
  10. weex官方demo weex-hackernews代码解读(下)
  11. 购物车自己sql错误
  12. SpringBoot2.0 最简单的 idea 快速创建项目
  13. 用css实现圆形波浪效果图
  14. js 使用a标签 下载资源
  15. Python之路,第二篇:Python入门与基础2
  16. Oracle卸载之Win7操作系统下Oracle11g 数据库卸载过程图解
  17. 非对齐访问(unaligned accesses)
  18. 终端(terminal)、tty、shell、控制台(console)、bash之间的区别与联系
  19. JS BOM操作
  20. C/C++预处理指令常见的预处理指令

热门文章

  1. Samba 源码解析之内存管理
  2. NTLM验证过程
  3. C++ 未初始化内存出现 flashback
  4. AT2642 [ARC076A] Reconciled? 题解
  5. velocity使用foreach进行遍历时$velocityCount不起作用
  6. SpringBoot 整合Spring Security框架
  7. JAVA判断某个元素是否在某个数组中
  8. 【LeetCode】1012. Complement of Base 10 Integer 解题报告(Python)
  9. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
  10. OA系统中手写签批功能的实现