1. 项目中引入工具类 WaterMarkUtil.java 2. 项目pom.xml引入第三方依赖包 pdf: <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>2.0.13</version> </dependency> excel: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>5.2.2</version> </dependency> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.12.0</version> </dependency> <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.xls.free</artifactId> <version>5.1.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/spire.xls.free-5.1.0.jar</systemPath> </dependency> word: <dependency> <groupId>e-iceblue</groupId> <artifactId>spire.doc.free</artifactId> <version>5.2.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/spire.doc.free-5.2.0.jar</systemPath> </dependency> 3. 调用方法 参数分别是输入文件、输出文件、水印文本。 WaterMarkUtil.addImageWaterMark("D:\\test\\test.png","D:\\test\\shuiyin.png","海信图片水印");
WaterMarkUtil.addPDFWaterMark("D:\\test\\test.pdf","D:\\test\\shuiyin.pdf","海信PDF水印");
WaterMarkUtil.addWordWaterMark("D:\\test\\test.docx","D:\\test\\shuiyin.docx","海信word水印");
WaterMarkUtil.addExcelWaterMark("D:\\test\\test.xlsx","D:\\test\\shuiyin.xlsx","海信excel水印"); 4. 测试效果
5. 其他说明 excel的水印效果有局限性,目前通过添加背景图片的间接方式实现,弊端就是需要将excel设置成受保护模式(不可编辑),否则水印图片作为excel内容的一部分可被任意编辑修改。
|