feat: 水印支持双行显示 配置项提取

This commit is contained in:
2024-01-09 16:23:41 +08:00
parent 33c03383cd
commit 17fabd9e8b
4 changed files with 64 additions and 15 deletions
@@ -14,6 +14,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -45,18 +47,32 @@ public class SysWaterMarkUtil {
@Value("${watermark.color.a}")
private int a;
public String getWaterMarkConfig() {
@Value("${watermark.lineNumPerPage}")
private int lineNumPerPage;
@Value("${watermark.columnNumPerPage}")
private int columnNumPerPage;
@Value("${watermark.degree}")
private int degree;
public List<String> getWaterMarkConfig() {
LoginUser loginUser = UserUtils.getLoginUser();
if (loginUser == null) {
return "";
return new ArrayList<>();
}
Date curDate = new Date();
String cudDateStr = DateUtils.date2Str(curDate, new SimpleDateFormat("yyyyMMddHHmmss"));
return loginUser.getUsername() + " " + loginUser.getRealname() + " " + cudDateStr;
List<String> result = new ArrayList<>();
// result.add(loginUser.getUsername() + " " + loginUser.getRealname());
result.add("009644" + " " + "刘长森");
result.add(cudDateStr);
return result;
}
public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException {
String watermarkText = getWaterMarkConfig();
List<String> watermarkText = getWaterMarkConfig();
PDDocument document = PDDocument.load(inputStream);
// 加载水印字体
@@ -79,23 +95,35 @@ public class SysWaterMarkUtil {
// 根据页面宽度动态调整字体大小
float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位
float xStep = pageWidth / 2;
float yStep = pageHeight / 8;
float rotationInRadians = (float) Math.toRadians(30);
float xStep = pageWidth / columnNumPerPage;
float yStep = pageHeight / lineNumPerPage;
float rotationInRadians = (float) Math.toRadians(degree);
// 行间距,设为字体大小的一半
float lineSpacing = dynamicFontSize / 2;
// 计算两行文本的总高度
float totalTextHeight = (dynamicFontSize + lineSpacing) * (watermarkText.size() - 1);
for (float xPosition = pageSize.getLowerLeftX(); xPosition <= pageWidth; xPosition += xStep) {
for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageHeight; yPosition += yStep) {
// 创建内容流并设置图形状态参数
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
contentStream.setGraphicsStateParameters(graphicsState);
contentStream.setFont(font, dynamicFontSize);
contentStream.setNonStrokingColor(color);
contentStream.beginText();
contentStream.setRenderingMode(RenderingMode.FILL);
// 设置水印文本的旋转和位置
contentStream.setTextMatrix(Matrix.getRotateInstance(rotationInRadians, xPosition, yPosition));
contentStream.showText(watermarkText);
contentStream.endText();
float textYPosition = yPosition + (totalTextHeight / 2);
for (String line : watermarkText) {
contentStream.beginText();
contentStream.setFont(font, dynamicFontSize);
contentStream.setRenderingMode(RenderingMode.FILL);
contentStream.setTextMatrix(Matrix.getRotateInstance(rotationInRadians, xPosition, textYPosition));
contentStream.showText(line);
contentStream.endText();
// 更新文本的Y坐标,为下一行准备
textYPosition -= (dynamicFontSize + lineSpacing);
}
}
}
}