From fe4d15a8945e168615687a57a3534e5a467efd3c Mon Sep 17 00:00:00 2001 From: caihaohan Date: Tue, 2 Jan 2024 17:25:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8A=A0=E6=B0=B4=E5=8D=B0=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E5=8A=A8=E6=80=81=E5=AD=97=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- laws-module-system/pom.xml | 2 +- .../modules/system/util/SysWaterMarkUtil.java | 29 ++++++++++--------- laws-modules/pom.xml | 2 +- .../laws/enterprise/util/WaterMarkUtil.java | 24 ++++++++++----- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/laws-module-system/pom.xml b/laws-module-system/pom.xml index d1245fb5..29b0d54d 100644 --- a/laws-module-system/pom.xml +++ b/laws-module-system/pom.xml @@ -75,7 +75,7 @@ org.apache.pdfbox pdfbox - 2.0.6 + 2.0.27 diff --git a/laws-module-system/src/main/java/com/jero/modules/system/util/SysWaterMarkUtil.java b/laws-module-system/src/main/java/com/jero/modules/system/util/SysWaterMarkUtil.java index c6a6bb05..a87e6d99 100644 --- a/laws-module-system/src/main/java/com/jero/modules/system/util/SysWaterMarkUtil.java +++ b/laws-module-system/src/main/java/com/jero/modules/system/util/SysWaterMarkUtil.java @@ -1,8 +1,6 @@ package com.jero.modules.system.util; import com.jero.common.system.vo.LoginUser; -import com.jero.modules.system.entity.SysUser; -import com.jero.modules.system.service.ISysUserService; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; @@ -15,14 +13,11 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Component; -import javax.annotation.Resource; import java.awt.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.time.LocalDate; -import java.time.format.DateTimeFormatter; /** * 水印工具类 @@ -59,10 +54,11 @@ public class SysWaterMarkUtil { String watermarkText = getWaterMarkConfig(); PDDocument document = PDDocument.load(inputStream); - // 设置水印字体、字体大小、颜色和透明度 + // 加载水印字体 ClassPathResource fontResource = new ClassPathResource("fonts/SourceHanSerif-VF.ttf"); PDType0Font font = PDType0Font.load(document, fontResource.getInputStream()); - float fontSize = this.fontSize; + + // 设置水印颜色和透明度 Color color = new Color(r, g, b, a); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(0.4f); @@ -70,18 +66,24 @@ public class SysWaterMarkUtil { // 遍历每一页并添加水印 for (PDPage page : document.getPages()) { - // 获取页面尺寸以计算水印位置 + // 获取页面尺寸以计算水印位置和大小 PDRectangle pageSize = page.getMediaBox(); - float xStep = pageSize.getWidth() / 4; - float yStep = pageSize.getHeight() / 4; + float pageWidth = pageSize.getWidth(); + float pageHeight = pageSize.getHeight(); + + // 根据页面宽度动态调整字体大小 + float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位 + + float xStep = pageWidth / 4; + float yStep = pageHeight / 4; float rotationInRadians = (float) Math.toRadians(20); - for (float xPosition = pageSize.getLowerLeftX(); xPosition <= pageSize.getWidth(); xPosition += xStep) { - for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageSize.getHeight(); yPosition += yStep) { + 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, fontSize); + contentStream.setFont(font, dynamicFontSize); contentStream.setNonStrokingColor(color); contentStream.beginText(); contentStream.setRenderingMode(RenderingMode.FILL); @@ -102,5 +104,4 @@ public class SysWaterMarkUtil { // 将ByteArrayOutputStream转换为ByteArrayInputStream以供返回 return new ByteArrayInputStream(baos.toByteArray()); } - } diff --git a/laws-modules/pom.xml b/laws-modules/pom.xml index 03233359..2dca17bf 100644 --- a/laws-modules/pom.xml +++ b/laws-modules/pom.xml @@ -128,7 +128,7 @@ org.apache.pdfbox pdfbox - 2.0.6 + 2.0.27 com.googlecode.json-simple diff --git a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/WaterMarkUtil.java b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/WaterMarkUtil.java index dea5e0fc..0f972eb2 100644 --- a/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/WaterMarkUtil.java +++ b/laws-modules/src/main/java/com/jero/modules/laws/enterprise/util/WaterMarkUtil.java @@ -51,10 +51,11 @@ public class WaterMarkUtil { String watermarkText = getWaterMarkConfig(); PDDocument document = PDDocument.load(inputStream); - // 设置水印字体、字体大小、颜色和透明度 + // 加载水印字体 ClassPathResource fontResource = new ClassPathResource("fonts/SourceHanSerif-VF.ttf"); PDType0Font font = PDType0Font.load(document, fontResource.getInputStream()); - float fontSize = this.fontSize; + + // 设置水印颜色和透明度 Color color = new Color(r, g, b, a); PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState(); graphicsState.setNonStrokingAlphaConstant(0.4f); @@ -62,18 +63,24 @@ public class WaterMarkUtil { // 遍历每一页并添加水印 for (PDPage page : document.getPages()) { - // 获取页面尺寸以计算水印位置 + // 获取页面尺寸以计算水印位置和大小 PDRectangle pageSize = page.getMediaBox(); - float xStep = pageSize.getWidth() / 4; - float yStep = pageSize.getHeight() / 4; + float pageWidth = pageSize.getWidth(); + float pageHeight = pageSize.getHeight(); + + // 根据页面宽度动态调整字体大小 + float dynamicFontSize = pageWidth * (this.fontSize / 595); // 假设基准页面宽度为595单位 + + float xStep = pageWidth / 4; + float yStep = pageHeight / 4; float rotationInRadians = (float) Math.toRadians(20); - for (float xPosition = pageSize.getLowerLeftX(); xPosition <= pageSize.getWidth(); xPosition += xStep) { - for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageSize.getHeight(); yPosition += yStep) { + 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, fontSize); + contentStream.setFont(font, dynamicFontSize); contentStream.setNonStrokingColor(color); contentStream.beginText(); contentStream.setRenderingMode(RenderingMode.FILL); @@ -94,4 +101,5 @@ public class WaterMarkUtil { // 将ByteArrayOutputStream转换为ByteArrayInputStream以供返回 return new ByteArrayInputStream(baos.toByteArray()); } + }