删除无用代码.
This commit is contained in:
-4
@@ -12,7 +12,6 @@ import com.jero.common.system.api.ISysBaseAPI;
|
||||
import com.jero.common.util.*;
|
||||
import com.jero.modules.oss.entity.OSSFile;
|
||||
import com.jero.modules.oss.service.IOSSFileService;
|
||||
import com.jero.modules.system.util.SysWaterMarkUtil;
|
||||
import com.jero.modules.system.util.UserUtils;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -55,9 +54,6 @@ public class CommonController {
|
||||
@Resource
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
@Resource
|
||||
private SysWaterMarkUtil sysWaterMarkUtil;
|
||||
|
||||
@Value(value = "${jero.path.upload}")
|
||||
private String uploadpath;
|
||||
|
||||
|
||||
-146
@@ -1,146 +0,0 @@
|
||||
package com.jero.modules.system.util;
|
||||
|
||||
import com.jero.common.system.vo.LoginUser;
|
||||
import com.jero.common.util.DateUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
|
||||
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
|
||||
import org.apache.pdfbox.util.Matrix;
|
||||
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;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 水印工具类
|
||||
*
|
||||
* @author CaiHaohan
|
||||
*/
|
||||
@Component
|
||||
public class SysWaterMarkUtil {
|
||||
|
||||
@Value("${watermark.font-size}")
|
||||
private float fontSize;
|
||||
|
||||
@Value("${watermark.color.r}")
|
||||
private int r;
|
||||
|
||||
@Value("${watermark.color.g}")
|
||||
private int g;
|
||||
|
||||
@Value("${watermark.color.b}")
|
||||
private int b;
|
||||
|
||||
@Value("${watermark.color.a}")
|
||||
private int a;
|
||||
|
||||
@Value("${watermark.lineNumPerPage}")
|
||||
private int lineNumPerPage;
|
||||
|
||||
@Value("${watermark.columnNumPerPage}")
|
||||
private int columnNumPerPage;
|
||||
|
||||
@Value("${watermark.degree}")
|
||||
private int degree;
|
||||
|
||||
|
||||
@Value("${watermark.margin.left}")
|
||||
private float marginLeft;
|
||||
|
||||
@Value("${watermark.margin.top}")
|
||||
private float marginTop;
|
||||
|
||||
|
||||
public List<String> getWaterMarkConfig() {
|
||||
LoginUser loginUser = UserUtils.getLoginUser();
|
||||
if (loginUser == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
Date curDate = new Date();
|
||||
String cudDateStr = DateUtils.date2Str(curDate, new SimpleDateFormat("yyyyMMddHHmmss"));
|
||||
List<String> result = new ArrayList<>();
|
||||
result.add(loginUser.getUsername() + " " + loginUser.getRealname());
|
||||
result.add(cudDateStr);
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized InputStream addWatermarkToPdf(InputStream inputStream) throws IOException {
|
||||
List<String> watermarkText = getWaterMarkConfig();
|
||||
PDDocument document = PDDocument.load(inputStream);
|
||||
|
||||
// 加载水印字体
|
||||
ClassPathResource fontResource = new ClassPathResource("fonts/SourceHanSerif-VF.ttf");
|
||||
PDType0Font font = PDType0Font.load(document, fontResource.getInputStream());
|
||||
|
||||
// 设置水印颜色和透明度
|
||||
Color color = new Color(r, g, b, a);
|
||||
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
|
||||
graphicsState.setNonStrokingAlphaConstant(0.4f);
|
||||
graphicsState.setStrokingAlphaConstant(0.4f);
|
||||
|
||||
// 遍历每一页并添加水印
|
||||
for (PDPage page : document.getPages()) {
|
||||
// 获取页面尺寸以计算水印位置和大小
|
||||
PDRectangle pageSize = page.getMediaBox();
|
||||
float pageWidth = pageSize.getWidth();
|
||||
float pageHeight = pageSize.getHeight();
|
||||
|
||||
// 根据页面宽度动态调整字体大小
|
||||
float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位
|
||||
|
||||
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() + marginLeft; xPosition <= pageWidth - marginLeft; xPosition += xStep) {
|
||||
for (float yPosition = pageSize.getLowerLeftY() + marginTop; yPosition <= pageHeight - marginTop; yPosition += yStep) {
|
||||
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
||||
contentStream.setGraphicsStateParameters(graphicsState);
|
||||
contentStream.setNonStrokingColor(color);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建一个临时的ByteArrayOutputStream保存修改过的PDF
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
document.save(baos);
|
||||
document.close();
|
||||
|
||||
// 将ByteArrayOutputStream转换为ByteArrayInputStream以供返回
|
||||
return new ByteArrayInputStream(baos.toByteArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user