Commit 1c17fb46 authored by 徐俊's avatar 徐俊

xujun

parent 00bf95bd
......@@ -19,31 +19,8 @@ public class ProjectInfoToPDF {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPath));
document.open();
// 设置中文字体
BaseFont bfChinese;
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);
}
// 宋体
BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc");
Font labelFont = new Font(bfChinese, 16, Font.NORMAL);
Font valueFont = new Font(bfChinese, 16, Font.NORMAL);
......@@ -98,6 +75,37 @@ public class ProjectInfoToPDF {
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 {
......@@ -142,6 +150,7 @@ public class ProjectInfoToPDF {
return document;
}
//添加表格
private static void addTableRow(PdfPTable table, String label, String value, Font labelFont, Font valueFont) {
PdfPCell labelCell = new PdfPCell(new Phrase(label, labelFont));
labelCell.setMinimumHeight(25f);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment