From ce8db0389eaaac78d35456644185fdadd428db00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=93=B2=E8=B4=A4?= <1037512352@qq.com> Date: Tue, 16 Apr 2024 22:42:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=B0=B4=E5=8D=B0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sa/admin/util/excel/Watermark.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/util/excel/Watermark.java diff --git a/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/util/excel/Watermark.java b/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/util/excel/Watermark.java new file mode 100644 index 00000000..8e80dd23 --- /dev/null +++ b/smart-admin-api/sa-admin/src/main/java/net/lab1024/sa/admin/util/excel/Watermark.java @@ -0,0 +1,95 @@ +package net.lab1024.sa.admin.util.excel; + +import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.date.DateUtil; +import lombok.Data; + +import javax.swing.*; +import java.awt.*; +import java.util.Date; + + +/** + * @author liuzhexian + * @since 2024-04-16 + * @email 1037512352@qq.com + */ +@Data +public class Watermark { + + public Watermark(String content) { + this.content = content; + init(); + } + + public Watermark(String content, Color color, Font font, double angle) { + this.content = content; + this.color = color; + this.font = font; + this.angle = angle; + init(); + } + + /** + * 根据水印内容长度自适应水印图片大小,简单的三角函数 + */ + private void init() { + FontMetrics fontMetrics = new JLabel().getFontMetrics(this.font); + int stringWidth = fontMetrics.stringWidth(this.content); + int charWidth = fontMetrics.charWidth('A'); + this.width = (int) Math.abs(stringWidth * Math.cos(Math.toRadians(this.angle))) + 2 * charWidth; + this.height = (int) Math.abs(stringWidth * Math.sin(Math.toRadians(this.angle))) + 2 * charWidth; + this.yAxis = this.height; + this.xAxis = charWidth; + } + + /** + * 水印内容 + */ + private String content; + + /** + * 画笔颜色 + */ + private Color color = new Color(239,239,239); + + /** + * 字体样式 + */ + private Font font = new Font("Microsoft YaHei", Font.BOLD, 15); + + /** + * 水印宽度 + */ + private int width; + + /** + * 水印高度 + */ + private int height; + + /** + * 倾斜角度,非弧度制 + */ + private double angle = 25; + + /** + * 字体的y轴位置 + */ + private int yAxis = 50; + + /** + * 字体的X轴位置 + */ + private int xAxis; + + /** + * 水平倾斜度 + */ + private double shearX = 0.1; + + /** + * 垂直倾斜度 + */ + private double shearY = -0.26; +}