Java 在Excel里面加水印 单一水印 以及 平铺水印
程序环境:
- 测试文档:Office Excel 2013
- 编译环境:IntelliJ IDEA 2018
- JDK版本:1.8.0
- Excel库:Java系列free spire.xls.jar 3.9.1
import com.spire.xls.*; import java.awt.*; import java.awt.image.BufferedImage; import static java.awt.image.BufferedImage.TYPE_INT_ARGB; //java项目www.fhadmin.org public class SingleWatermark { public static void main(String[] args) { //加载Excel测试文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //设置文本和字体大小 Font font = new Font("仿宋", Font.PLAIN, 40); for (int i =0;i<wb.getWorksheets().getCount();i++) { Worksheet sheet = wb.getWorksheets().get(i); //调用DrawText() 方法插入图片 BufferedImage imgWtrmrk = drawText("内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth()); //将图片设置为页眉 sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk); sheet.getPageSetup().setCenterHeader("&G"); //将显示模式设置为Layout sheet.setViewMode(ViewMode.Layout); } //保存文档 wb.saveToFile("SingleWatermark.xlsx", ExcelVersion.Version2013); } private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width) { //定义图片宽度和高度 BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB); Graphics2D loGraphic = img.createGraphics(); //获取文本size FontMetrics loFontMetrics = loGraphic.getFontMetrics(font); int liStrWidth = loFontMetrics.stringWidth(text); int liStrHeight = loFontMetrics.getHeight(); //文本显示样式及位置 loGraphic.setColor(backColor); loGraphic.fillRect(0, 0, (int) width, (int) height); loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); loGraphic.rotate(Math.toRadians(-45)); loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2); loGraphic.setFont(font); loGraphic.setColor(textColor); loGraphic.drawString(text, ((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); loGraphic.dispose(); return img; } }
单一水印效果:
2.平铺水印效果
import com.spire.xls.*; import java.awt.*; import java.awt.image.BufferedImage; import static java.awt.image.BufferedImage.TYPE_INT_ARGB; //java项目www.fhadmin.org public class TiledWatermark { public static void main(String[] args) { //加载Excel测试文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //设置文本和字体大小 Font font = new Font("仿宋", Font.PLAIN, 25); for (int i =0;i<wb.getWorksheets().getCount();i++) { Worksheet sheet = wb.getWorksheets().get(i); //调用DrawText() 方法插入图片 BufferedImage imgWtrmrk = drawText("内部专用 内部专用 内部专用 内部专用", font, Color.pink, Color.white, sheet.getPageSetup().getPageHeight(), sheet.getPageSetup().getPageWidth()); //将图片设置为页眉 sheet.getPageSetup().setCenterHeaderImage(imgWtrmrk); sheet.getPageSetup().setCenterHeader("&G"); //将显示模式设置为Layout sheet.setViewMode(ViewMode.Layout); } //保存文档 wb.saveToFile("TiledWatermark.xlsx", ExcelVersion.Version2013); } private static BufferedImage drawText (String text, Font font, Color textColor, Color backColor,double height, double width) { //定义图片宽度和高度 BufferedImage img = new BufferedImage((int) width, (int) height, TYPE_INT_ARGB); Graphics2D loGraphic = img.createGraphics(); //获取文本size FontMetrics loFontMetrics = loGraphic.getFontMetrics(font); int liStrWidth = loFontMetrics.stringWidth(text); int liStrHeight = loFontMetrics.getHeight(); //文本显示样式及位置 loGraphic.setColor(backColor); loGraphic.fillRect(0, 0, (int) width, (int) height); loGraphic.translate(((int) width - liStrWidth) / 2, ((int) height - liStrHeight) / 2); //loGraphic.rotate(Math.toRadians(-45)); loGraphic.translate(-((int) width - liStrWidth) / 2, -((int) height - liStrHeight) / 2); loGraphic.setFont(font); loGraphic.setColor(textColor); loGraphic.drawString(text, ((int) width - liStrWidth) /6 , ((int) height - liStrHeight) /6); loGraphic.drawString(text,((int) width - liStrWidth) /3, ((int) height - liStrHeight) /3); loGraphic.drawString(text,((int) width - liStrWidth) /2, ((int) height - liStrHeight) /2); loGraphic.dispose(); return img; } }
平铺水印效果:
★ 需要注意的是:在添加完水印效果后,查看文档时,在“普通视图”水印不可见,需在“页面布局”模式或“打印预览”模式下查看。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
【超详细】一文学会链表解题
前言 如果说数据结构是算法的基础,那么数组和链表就是数据结构的基础。 因为像堆,栈,对,图等比较复杂的数组结基本上都可以由数组和链表来表示,所以掌握数组和链表的基本操作十分重要。 今天就来看看链表的基本操作及其在面试中的常见解题思路,本文将从以下几个点来讲解链表的核心知识 什么是链表,链表的优缺点 链表的表示及基本操作 面试中链表的常见解题思路---翻转 面试中链表的常见解题思路---快慢指针 什么是链表 相信大家已经开始迫不及待地想用链表解题了,不过在开始之前我们还是要先来温习下链表的定义,以及它的优势与劣势,磨刀不误砍柴功! 链表的定义 链表是物理存储单元上非连续的、非顺序的存储结构,它是由一个个结点,通过指针来联系起来的,其中每个结点包括数据和指针。 链表的非连续,非顺序,对应数组的连续,顺序,我们来看看整型数组 1,2,3,4 在内存中是如何表示的 可以看到数组的每个元素都是连续紧邻分配的,这叫连续性,同时由于数组的元素占用的大小是一样的,在 Java 中 int 型大小固定为 4 个字节,所以如果数组的起始地址是 100, 由于这些元素在内存中都是连续紧邻分配的,大小也一样,...
- 下一篇
一篇与面试官和蔼交流的深入了解JVM(JDK8)
文章目录 面试系列 15.1、100%CPU的排查 15.2、死锁的检查 13.1、CMS(“标记-清除”算法, -XX:+UseConcMarkSweepGC(old) 13.2、G1 13.3、ZGC 13.1.6.1、增量更新(Incremental Update)+写屏障 13.1.6.2、原始快照(Snapshot At The Beginning,SATB)+写屏障 13.1.6.3、并发标记时对漏标的处理方案 13.1.1、运作过程(5大步骤) 13.1.2、三色标记法 13.1.3、concurrent model failure(浮动垃圾) 13.1.4、background/foreground collector 13.1.5、为什么G1用SATB?CMS用增量更新? 13.1.6、漏标-读写屏障(解决方案) 13.1.7、promotion failed 13.1.8、过早提升和提升失败 13.1.9、早提升的原因 13.1.10、提升失败原因 13.2.1、运作流程 13.2.2、Remembered Set(记录集)/Card Table(卡表) 13.2....
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2全家桶,快速入门学习开发网站教程
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装