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
1c17fb46
Commit
1c17fb46
authored
Feb 12, 2025
by
徐俊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
xujun
parent
00bf95bd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
25 deletions
+34
-25
ProjectInfoToPDF.java
...main/java/com/yiboshi/science/utils/ProjectInfoToPDF.java
+34
-25
No files found.
science-admin/src/main/java/com/yiboshi/science/utils/ProjectInfoToPDF.java
View file @
1c17fb46
...
@@ -19,31 +19,8 @@ public class ProjectInfoToPDF {
...
@@ -19,31 +19,8 @@ public class ProjectInfoToPDF {
PdfWriter
writer
=
PdfWriter
.
getInstance
(
document
,
new
FileOutputStream
(
outputPath
));
PdfWriter
writer
=
PdfWriter
.
getInstance
(
document
,
new
FileOutputStream
(
outputPath
));
document
.
open
();
document
.
open
();
// 设置中文字体
// 宋体
BaseFont
bfChinese
;
BaseFont
bfChinese
=
loadChineseFont
(
"/fonts/simsun.ttc"
);
try
{
// 从资源文件夹加载字体
InputStream
fontStream
=
ProjectInfoToPDF
.
class
.
getResourceAsStream
(
"/fonts/simsun.ttc"
);
if
(
fontStream
==
null
)
{
throw
new
FileNotFoundException
(
"在resources/fonts目录下未找到simsun.ttc字体文件"
);
}
// 将字体文件复制到临时文件
File
tempFile
=
File
.
createTempFile
(
"simsun"
,
".ttc"
);
tempFile
.
deleteOnExit
();
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
tempFile
))
{
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
fontStream
.
read
(
buffer
))
>
0
)
{
fos
.
write
(
buffer
,
0
,
length
);
}
}
bfChinese
=
BaseFont
.
createFont
(
tempFile
.
getAbsolutePath
()
+
",0"
,
BaseFont
.
IDENTITY_H
,
BaseFont
.
EMBEDDED
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"加载字体文件失败,请确保resources/fonts目录下存在simsun.ttc文件:"
+
e
.
getMessage
(),
e
);
}
Font
labelFont
=
new
Font
(
bfChinese
,
16
,
Font
.
NORMAL
);
Font
labelFont
=
new
Font
(
bfChinese
,
16
,
Font
.
NORMAL
);
Font
valueFont
=
new
Font
(
bfChinese
,
16
,
Font
.
NORMAL
);
Font
valueFont
=
new
Font
(
bfChinese
,
16
,
Font
.
NORMAL
);
...
@@ -98,6 +75,37 @@ public class ProjectInfoToPDF {
...
@@ -98,6 +75,37 @@ public class ProjectInfoToPDF {
document
.
close
();
document
.
close
();
}
}
/**
* 加载中文字体
* @return BaseFont 中文字体
* @throws RuntimeException 加载字体失败时抛出异常
*/
private
static
BaseFont
loadChineseFont
(
String
fontPath
)
{
try
{
// 从资源文件夹加载字体
InputStream
fontStream
=
ProjectInfoToPDF
.
class
.
getResourceAsStream
(
fontPath
);
if
(
fontStream
==
null
)
{
throw
new
FileNotFoundException
(
"在resources/fonts目录下未找到simsun.ttc字体文件"
);
}
// 将字体文件复制到临时文件
File
tempFile
=
File
.
createTempFile
(
"simsun"
,
".ttc"
);
tempFile
.
deleteOnExit
();
try
(
FileOutputStream
fos
=
new
FileOutputStream
(
tempFile
))
{
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
fontStream
.
read
(
buffer
))
>
0
)
{
fos
.
write
(
buffer
,
0
,
length
);
}
}
return
BaseFont
.
createFont
(
tempFile
.
getAbsolutePath
()
+
",0"
,
BaseFont
.
IDENTITY_H
,
BaseFont
.
EMBEDDED
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
"加载字体文件失败,请确保resources/fonts目录下存在simsun.ttc文件:"
+
e
.
getMessage
(),
e
);
}
}
//首页
//首页
private
static
Document
FirstPageInfo
(
Document
document
,
ComProjectDTO
project
,
BaseFont
bfChinese
)
throws
DocumentException
{
private
static
Document
FirstPageInfo
(
Document
document
,
ComProjectDTO
project
,
BaseFont
bfChinese
)
throws
DocumentException
{
...
@@ -142,6 +150,7 @@ public class ProjectInfoToPDF {
...
@@ -142,6 +150,7 @@ public class ProjectInfoToPDF {
return
document
;
return
document
;
}
}
//添加表格
private
static
void
addTableRow
(
PdfPTable
table
,
String
label
,
String
value
,
Font
labelFont
,
Font
valueFont
)
{
private
static
void
addTableRow
(
PdfPTable
table
,
String
label
,
String
value
,
Font
labelFont
,
Font
valueFont
)
{
PdfPCell
labelCell
=
new
PdfPCell
(
new
Phrase
(
label
,
labelFont
));
PdfPCell
labelCell
=
new
PdfPCell
(
new
Phrase
(
label
,
labelFont
));
labelCell
.
setMinimumHeight
(
25
f
);
labelCell
.
setMinimumHeight
(
25
f
);
...
...
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