feat: 双水印工具类同步
This commit is contained in:
@@ -65,8 +65,7 @@ public class SysWaterMarkUtil {
|
|||||||
Date curDate = new Date();
|
Date curDate = new Date();
|
||||||
String cudDateStr = DateUtils.date2Str(curDate, new SimpleDateFormat("yyyyMMddHHmmss"));
|
String cudDateStr = DateUtils.date2Str(curDate, new SimpleDateFormat("yyyyMMddHHmmss"));
|
||||||
List<String> result = new ArrayList<>();
|
List<String> result = new ArrayList<>();
|
||||||
// result.add(loginUser.getUsername() + " " + loginUser.getRealname());
|
result.add(loginUser.getUsername() + " " + loginUser.getRealname());
|
||||||
result.add("009644" + " " + "刘长森");
|
|
||||||
result.add(cudDateStr);
|
result.add(cudDateStr);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
+47
-17
@@ -15,13 +15,19 @@ import org.springframework.beans.factory.annotation.Value;
|
|||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.*;
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 水印工具类
|
* 水印工具类
|
||||||
|
*
|
||||||
* @author CaiHaohan
|
* @author CaiHaohan
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
@@ -42,18 +48,31 @@ public class WaterMarkUtil {
|
|||||||
@Value("${watermark.color.a}")
|
@Value("${watermark.color.a}")
|
||||||
private int 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();
|
LoginUser loginUser = UserUtils.getLoginUser();
|
||||||
if (loginUser == null) {
|
if (loginUser == null) {
|
||||||
return "";
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
Date curDate = new Date();
|
Date curDate = new Date();
|
||||||
String cudDateStr = DateUtils.date2Str(curDate, new SimpleDateFormat("yyyyMMddHHmmss"));
|
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(cudDateStr);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException {
|
public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException {
|
||||||
String watermarkText = getWaterMarkConfig();
|
List<String> watermarkText = getWaterMarkConfig();
|
||||||
PDDocument document = PDDocument.load(inputStream);
|
PDDocument document = PDDocument.load(inputStream);
|
||||||
|
|
||||||
// 加载水印字体
|
// 加载水印字体
|
||||||
@@ -76,23 +95,35 @@ public class WaterMarkUtil {
|
|||||||
// 根据页面宽度动态调整字体大小
|
// 根据页面宽度动态调整字体大小
|
||||||
float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位
|
float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位
|
||||||
|
|
||||||
float xStep = pageWidth / 2;
|
float xStep = pageWidth / columnNumPerPage;
|
||||||
float yStep = pageHeight / 8;
|
float yStep = pageHeight / lineNumPerPage;
|
||||||
float rotationInRadians = (float) Math.toRadians(30);
|
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 xPosition = pageSize.getLowerLeftX(); xPosition <= pageWidth; xPosition += xStep) {
|
||||||
for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageHeight; yPosition += yStep) {
|
for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageHeight; yPosition += yStep) {
|
||||||
// 创建内容流并设置图形状态参数
|
|
||||||
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
||||||
contentStream.setGraphicsStateParameters(graphicsState);
|
contentStream.setGraphicsStateParameters(graphicsState);
|
||||||
contentStream.setFont(font, dynamicFontSize);
|
|
||||||
contentStream.setNonStrokingColor(color);
|
contentStream.setNonStrokingColor(color);
|
||||||
contentStream.beginText();
|
|
||||||
contentStream.setRenderingMode(RenderingMode.FILL);
|
float textYPosition = yPosition + (totalTextHeight / 2);
|
||||||
// 设置水印文本的旋转和位置
|
|
||||||
contentStream.setTextMatrix(Matrix.getRotateInstance(rotationInRadians, xPosition, yPosition));
|
for (String line : watermarkText) {
|
||||||
contentStream.showText(watermarkText);
|
contentStream.beginText();
|
||||||
contentStream.endText();
|
contentStream.setFont(font, dynamicFontSize);
|
||||||
|
contentStream.setRenderingMode(RenderingMode.FILL);
|
||||||
|
contentStream.setTextMatrix(Matrix.getRotateInstance(rotationInRadians, xPosition, textYPosition));
|
||||||
|
contentStream.showText(line);
|
||||||
|
contentStream.endText();
|
||||||
|
|
||||||
|
// 更新文本的Y坐标,为下一行准备
|
||||||
|
textYPosition -= (dynamicFontSize + lineSpacing);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -106,5 +137,4 @@ public class WaterMarkUtil {
|
|||||||
// 将ByteArrayOutputStream转换为ByteArrayInputStream以供返回
|
// 将ByteArrayOutputStream转换为ByteArrayInputStream以供返回
|
||||||
return new ByteArrayInputStream(baos.toByteArray());
|
return new ByteArrayInputStream(baos.toByteArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user