Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
T
travel-cloud
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
cloud
travel-cloud
Commits
039e2cc5
提交
039e2cc5
authored
5月 27, 2025
作者:
hzh
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
水印修复
上级
f51db8fc
显示空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
34 行增加
和
12 行删除
+34
-12
WatermarkUtil.java
...ava/org/dromara/common/core/utils/file/WatermarkUtil.java
+33
-11
FileApproveServiceImpl.java
...dromara/workflow/service/impl/FileApproveServiceImpl.java
+1
-1
没有找到文件。
ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/utils/file/WatermarkUtil.java
浏览文件 @
039e2cc5
...
...
@@ -2,6 +2,7 @@ package org.dromara.common.core.utils.file;
import
cn.hutool.core.io.FileUtil
;
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.config.WatermarkConfig
;
import
org.dromara.common.core.utils.file.watermark.processor.ImageWatermarkProcessor
;
...
...
@@ -34,6 +35,7 @@ public class WatermarkUtil {
PROCESSOR_MAP
.
put
(
"pdf"
,
new
PdfWatermarkProcessor
());
}
/**
* 为文件添加文字水印
*
...
...
@@ -57,6 +59,23 @@ public class WatermarkUtil {
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 {
}
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
()
.
watermarkText
(
"测试水印"
)
.
fontName
(
"宋体"
)
.
watermarkText
(
"测试水印
PDF
"
)
.
fontName
(
getFontName
(
sourceImagePath
.
toFile
().
getPath
(),
"测试水印PDF"
)
)
.
fontSize
(
30
)
.
color
(
Color
.
RED
)
.
alpha
(
0.5f
)
.
angle
(
30
)
.
build
();
// 示例:给图片添加水印
Path
sourceImagePath
=
Paths
.
get
(
"D:\\1.jpg"
);
Path
targetImagePath
=
Paths
.
get
(
"D:\\output.jpg"
);
// 验证源文件是否存在
if
(!
Files
.
exists
(
sourceImagePath
))
{
System
.
err
.
println
(
"源文件不存在: "
+
sourceImagePath
);
...
...
@@ -97,19 +118,20 @@ public class WatermarkUtil {
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水印参数
WatermarkConfig
pdfConfig
=
new
WatermarkConfig
.
Builder
()
.
watermarkText
(
"
Confidential
"
)
.
fontName
(
"Helvetica"
)
.
watermarkText
(
"
测试水印PDF
"
)
.
fontName
(
getFontName
(
pdfFile
.
getPath
(),
"测试水印PDF"
)
)
.
fontSize
(
30
)
.
color
(
Color
.
RED
)
.
alpha
(
0.3f
)
.
angle
(
45
)
.
build
();
// 示例:给PDF添加水印
File
pdfFile
=
new
File
(
"C:\\Users\\wenhe\\Downloads\\安盛保险-卓越个人基础版.pdf"
);
File
pdfOutputFile
=
FileUtil
.
touch
(
FileUtils
.
addFolderBeforeFileName
(
pdfFile
.
getPath
(),
"watermark"
));
addTextWatermark
(
pdfFile
,
pdfOutputFile
,
pdfConfig
);
}
}
...
...
ruoyi-modules/ruoyi-workflow/src/main/java/org/dromara/workflow/service/impl/FileApproveServiceImpl.java
浏览文件 @
039e2cc5
...
...
@@ -131,7 +131,7 @@ public class FileApproveServiceImpl extends AbstractBaseService<FileApproveVo, F
watermarkFile
=
FileUtil
.
touch
(
FileUtils
.
addFolderBeforeFileName
(
originFile
.
getPath
(),
"watermark"
));
WatermarkConfig
watermarkConfig
=
new
WatermarkConfig
.
Builder
()
.
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
)
.
alpha
(
0.3f
)
.
angle
(
45
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论