提交 039e2cc5 authored 作者: hzh's avatar hzh

水印修复

上级 f51db8fc
...@@ -2,6 +2,7 @@ package org.dromara.common.core.utils.file; ...@@ -2,6 +2,7 @@ package org.dromara.common.core.utils.file;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.watermark.WatermarkProcessor; import org.dromara.common.core.utils.file.watermark.WatermarkProcessor;
import org.dromara.common.core.utils.file.watermark.config.WatermarkConfig; import org.dromara.common.core.utils.file.watermark.config.WatermarkConfig;
import org.dromara.common.core.utils.file.watermark.processor.ImageWatermarkProcessor; import org.dromara.common.core.utils.file.watermark.processor.ImageWatermarkProcessor;
...@@ -34,6 +35,7 @@ public class WatermarkUtil { ...@@ -34,6 +35,7 @@ public class WatermarkUtil {
PROCESSOR_MAP.put("pdf", new PdfWatermarkProcessor()); PROCESSOR_MAP.put("pdf", new PdfWatermarkProcessor());
} }
/** /**
* 为文件添加文字水印 * 为文件添加文字水印
* *
...@@ -57,6 +59,23 @@ public class WatermarkUtil { ...@@ -57,6 +59,23 @@ public class WatermarkUtil {
return processor.process(sourceFile, targetFile, config); return processor.process(sourceFile, targetFile, config);
} }
/**
* 获取文件footName,
*
* @param path 文件路径
* @param watermarkText 水印文字
* @return 文件字体
*/
public static String getFontName(String path, String watermarkText) {
// 获取文件扩展名
String extension = getFileExtension(path).toLowerCase();
if (StringUtils.equals(extension, "pdf")) {
return watermarkText.codePoints().anyMatch(c -> Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) ? "SimHei" : "Arial";
} else {
return "宋体";
}
}
/** /**
* 获取文件扩展名 * 获取文件扩展名
*/ */
...@@ -66,20 +85,22 @@ public class WatermarkUtil { ...@@ -66,20 +85,22 @@ public class WatermarkUtil {
} }
public static void main(String[] args) { public static void main(String[] args) {
// 示例:给图片添加水印
Path sourceImagePath = Paths.get("D:\\1.jpg");
Path targetImagePath = Paths.get("D:\\output.jpg");
// 配置水印参数 // 配置水印参数
WatermarkConfig imageConfig = new WatermarkConfig.Builder() WatermarkConfig imageConfig = new WatermarkConfig.Builder()
.watermarkText("测试水印") .watermarkText("测试水印PDF")
.fontName("宋体") .fontName(getFontName(sourceImagePath.toFile().getPath(), "测试水印PDF"))
.fontSize(30) .fontSize(30)
.color(Color.RED) .color(Color.RED)
.alpha(0.5f) .alpha(0.5f)
.angle(30) .angle(30)
.build(); .build();
// 示例:给图片添加水印
Path sourceImagePath = Paths.get("D:\\1.jpg");
Path targetImagePath = Paths.get("D:\\output.jpg");
// 验证源文件是否存在 // 验证源文件是否存在
if (!Files.exists(sourceImagePath)) { if (!Files.exists(sourceImagePath)) {
System.err.println("源文件不存在: " + sourceImagePath); System.err.println("源文件不存在: " + sourceImagePath);
...@@ -97,19 +118,20 @@ public class WatermarkUtil { ...@@ -97,19 +118,20 @@ public class WatermarkUtil {
addTextWatermark(sourceImagePath.toFile(), targetImagePath.toFile(), imageConfig); addTextWatermark(sourceImagePath.toFile(), targetImagePath.toFile(), imageConfig);
// 示例:给PDF添加水印
File pdfFile = new File("C:\\Users\\wenhe\\Downloads\\安盛保险-卓越个人基础版.pdf");
File pdfOutputFile = FileUtil.touch(FileUtils.addFolderBeforeFileName(pdfFile.getPath(), "watermark"));
// 配置PDF水印参数 // 配置PDF水印参数
WatermarkConfig pdfConfig = new WatermarkConfig.Builder() WatermarkConfig pdfConfig = new WatermarkConfig.Builder()
.watermarkText("Confidential") .watermarkText("测试水印PDF")
.fontName("Helvetica") .fontName(getFontName(pdfFile.getPath(),"测试水印PDF"))
.fontSize(30) .fontSize(30)
.color(Color.RED) .color(Color.RED)
.alpha(0.3f) .alpha(0.3f)
.angle(45) .angle(45)
.build(); .build();
// 示例:给PDF添加水印
File pdfFile = new File("C:\\Users\\wenhe\\Downloads\\安盛保险-卓越个人基础版.pdf");
File pdfOutputFile = FileUtil.touch(FileUtils.addFolderBeforeFileName(pdfFile.getPath(), "watermark"));
addTextWatermark(pdfFile, pdfOutputFile, pdfConfig); addTextWatermark(pdfFile, pdfOutputFile, pdfConfig);
} }
} }
......
...@@ -131,7 +131,7 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F ...@@ -131,7 +131,7 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F
watermarkFile = FileUtil.touch(FileUtils.addFolderBeforeFileName(originFile.getPath(), "watermark")); watermarkFile = FileUtil.touch(FileUtils.addFolderBeforeFileName(originFile.getPath(), "watermark"));
WatermarkConfig watermarkConfig = new WatermarkConfig.Builder() WatermarkConfig watermarkConfig = new WatermarkConfig.Builder()
.watermarkText(fa.getPurpose()) .watermarkText(fa.getPurpose())
.fontName(fa.getPurpose().codePoints().anyMatch(c -> Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) ? "SimHei" : "Arial") .fontName(WatermarkUtil.getFontName(originFile.getPath(), fa.getPurpose()))
.color(Color.RED) .color(Color.RED)
.alpha(0.3f) .alpha(0.3f)
.angle(45) .angle(45)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论