Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
Y
yn-health-science
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
徐俊
yn-health-science
Commits
8e4417e9
Commit
8e4417e9
authored
Feb 14, 2025
by
徐俊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
xujun
parent
eedbce92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
2 deletions
+84
-2
ProjectInfoToPDF.java
...main/java/com/yiboshi/science/utils/ProjectInfoToPDF.java
+84
-2
No files found.
science-admin/src/main/java/com/yiboshi/science/utils/ProjectInfoToPDF.java
View file @
8e4417e9
...
...
@@ -5,6 +5,7 @@ import com.itextpdf.kernel.font.PdfFontFactory;
import
com.itextpdf.text.*
;
import
com.itextpdf.text.pdf.*
;
import
com.yiboshi.science.entity.SystemParameter
;
import
com.yiboshi.science.enumeration.CommonEnum
;
import
com.yiboshi.science.param.dto.*
;
import
java.io.*
;
...
...
@@ -23,8 +24,12 @@ public class ProjectInfoToPDF {
public
static
void
generateProjectPDF
(
ComProjectDTO
project
,
String
outputPath
,
List
<
SystemParameter
>
projAttributeList
)
throws
DocumentException
,
IOException
{
Document
document
=
new
Document
(
PageSize
.
A4
);
PdfWriter
writer
=
PdfWriter
.
getInstance
(
document
,
new
FileOutputStream
(
outputPath
));
document
.
open
();
// 文件水印
String
watermark
=
project
.
getAppNo
();
if
(
project
.
getProjState
()
>=
CommonEnum
.
projState
.
pass
.
getCode
())
{
watermark
=
project
.
getProjNo
();
}
// 宋体
BaseFont
bfChinese
=
loadChineseFont
(
"/fonts/simsun.ttc"
);
// 仿宋
...
...
@@ -32,6 +37,13 @@ public class ProjectInfoToPDF {
// WINGDNG2
BaseFont
WINGDNG2
=
loadChineseFont
(
"/fonts/WINGDNG2.ttf"
);
// 添加水印
writer
.
setPageEvent
(
new
WatermarkPageEvent
(
watermark
,
fsChinese
));
document
.
open
();
Font
normalFont
=
new
Font
(
bfChinese
,
12
,
Font
.
NORMAL
);
Font
boldFont
=
new
Font
(
bfChinese
,
12
,
Font
.
BOLD
);
...
...
@@ -854,4 +866,74 @@ public class ProjectInfoToPDF {
unitSignature
.
setSpacingBefore
(
30
);
document
.
add
(
unitSignature
);
}
/**
* 水印页面事件
*/
private
static
class
WatermarkPageEvent
extends
PdfPageEventHelper
{
private
String
watermarkText
;
private
BaseFont
baseFont
;
public
WatermarkPageEvent
(
String
watermarkText
,
BaseFont
baseFont
)
{
this
.
watermarkText
=
watermarkText
;
this
.
baseFont
=
baseFont
;
}
@Override
public
void
onEndPage
(
PdfWriter
writer
,
Document
document
)
{
try
{
PdfContentByte
canvas
=
writer
.
getDirectContentUnder
();
Rectangle
pageSize
=
document
.
getPageSize
();
float
width
=
pageSize
.
getWidth
();
float
height
=
pageSize
.
getHeight
();
// 设置水印字体
canvas
.
saveState
();
canvas
.
beginText
();
canvas
.
setFontAndSize
(
baseFont
,
30
);
// 减小字体大小
canvas
.
setGrayFill
(
0.9f
);
// 计算水印间距
float
xStep
=
width
/
2
;
// 水平间距
float
yStep
=
height
/
3
;
// 垂直间距
// 在页面上添加多个水印
for
(
float
y
=
yStep
/
2
;
y
<
height
;
y
+=
yStep
)
{
for
(
float
x
=
xStep
/
2
;
x
<
width
;
x
+=
xStep
)
{
canvas
.
showTextAligned
(
Element
.
ALIGN_CENTER
,
watermarkText
,
x
,
y
,
45
);
}
}
canvas
.
endText
();
canvas
.
restoreState
();
// 添加页码
PdfContentByte
canvasOver
=
writer
.
getDirectContent
();
canvasOver
.
saveState
();
canvasOver
.
beginText
();
canvasOver
.
setFontAndSize
(
baseFont
,
12
);
// 设置页码字体大小
canvasOver
.
setColorFill
(
BaseColor
.
BLACK
);
// 页码文本
String
text
=
String
.
format
(
"第 %d 页"
,
writer
.
getPageNumber
());
// 在页面底部居中添加页码
canvasOver
.
showTextAligned
(
Element
.
ALIGN_CENTER
,
text
,
width
/
2
,
// 页面中心
15
,
// 距离底部15单位,降低页码位置
0
);
// 不旋转
canvasOver
.
endText
();
canvasOver
.
restoreState
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment