Commit 24f66377 authored by 徐俊's avatar 徐俊

xujun

parent 87c03d57
...@@ -6,6 +6,8 @@ import com.yiboshi.science.param.dto.ComProjectDTO; ...@@ -6,6 +6,8 @@ import com.yiboshi.science.param.dto.ComProjectDTO;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import java.io.*; import java.io.*;
import java.util.Objects; import java.util.Objects;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ProjectInfoToPDF { public class ProjectInfoToPDF {
...@@ -22,35 +24,16 @@ public class ProjectInfoToPDF { ...@@ -22,35 +24,16 @@ public class ProjectInfoToPDF {
// 宋体 // 宋体
BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc"); BaseFont bfChinese = loadChineseFont("/fonts/simsun.ttc");
// 仿宋 // 仿宋
BaseFont fsChinese = loadChineseFont("/fonts/方正仿宋简体.ttf"); BaseFont fsChinese = loadChineseFont("/fonts/simfang.ttf");
Font labelFont = new Font(fsChinese, 16, Font.NORMAL);
Font valueFont = new Font(bfChinese, 16, Font.NORMAL);
Font normalFont = new Font(bfChinese, 12, Font.NORMAL); Font normalFont = new Font(bfChinese, 12, Font.NORMAL);
Font boldFont = new Font(bfChinese, 12, Font.BOLD); Font boldFont = new Font(bfChinese, 12, Font.BOLD);
//首页内容 //首页内容
FirstPageInfo(document, project, bfChinese); FirstPageInfo(document, project, bfChinese, fsChinese);
// 添加项目基本信息表格 // 添加新页面
PdfPTable table = new PdfPTable(2); document.newPage();
table.setWidthPercentage(100);
table.setSpacingBefore(10f);
table.setSpacingAfter(10f);
// 设置列宽度
float[] columnWidths = {0.3f, 0.7f};
table.setWidths(columnWidths);
// 添加表格内容
addTableRow(table, "项目名称:", project.getProjName(), labelFont, valueFont);
addTableRow(table, "项目申报单位:", project.getProjNo(), labelFont, valueFont);
addTableRow(table, "申请单位:", project.getAppUnitName(), labelFont, valueFont);
addTableRow(table, "项目负责人:", project.getAppPersonName(), labelFont, valueFont);
addTableRow(table, "联系电话:", project.getMobile(), labelFont, valueFont);
addTableRow(table, "电子邮箱:", project.getEmail(), labelFont, valueFont);
document.add(table);
// 添加项目内容章节 // 添加项目内容章节
addSection(document, "一、项目实施目标", boldFont); addSection(document, "一、项目实施目标", boldFont);
...@@ -86,39 +69,26 @@ public class ProjectInfoToPDF { ...@@ -86,39 +69,26 @@ public class ProjectInfoToPDF {
private static BaseFont loadChineseFont(String fontPath) { private static BaseFont loadChineseFont(String fontPath) {
try { try {
// 从资源文件夹加载字体 // 从资源文件夹加载字体
InputStream fontStream = ProjectInfoToPDF.class.getResourceAsStream(fontPath); String path = ProjectInfoToPDF.class.getResource(fontPath).getPath();
if (fontStream == null) {
throw new FileNotFoundException("找不到字体文件:" + fontPath);
}
// 获取文件名作为临时文件的前缀
String fileName = fontPath.substring(fontPath.lastIndexOf('/') + 1);
String prefix = fileName.substring(0, fileName.lastIndexOf('.'));
String suffix = fileName.substring(fileName.lastIndexOf('.'));
// 将字体文件复制到临时文件 // 根据字体文件类型使用不同的加载方式
File tempFile = File.createTempFile(prefix, suffix); if (fontPath.toLowerCase().endsWith(".ttc")) {
tempFile.deleteOnExit(); return BaseFont.createFont(path + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} else {
try (FileOutputStream fos = new FileOutputStream(tempFile)) { return BaseFont.createFont(path, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
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) { } catch (Exception e) {
throw new RuntimeException("加载字体文件失败,请确保resources目录下存在字体文件 " + fontPath + ":" + e.getMessage(), e); throw new RuntimeException("加载字体文件失败,请确保resources目录下存在字体文件 " + fontPath + ":" + 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, BaseFont fsChinese) throws DocumentException {
Font titleFont = new Font(bfChinese, 20, Font.BOLD); Font titleFont = new Font(bfChinese, 20, Font.BOLD);
Font normalFont = new Font(bfChinese, 12, Font.NORMAL); Font normalFont = new Font(bfChinese, 12, Font.NORMAL);
Font labelFont = new Font(fsChinese, 16, Font.NORMAL);
Font valueFont = new Font(bfChinese, 16, Font.NORMAL);
// 添加项目编号到左上角 // 添加项目编号到左上角
Paragraph projNo; Paragraph projNo;
...@@ -146,42 +116,93 @@ public class ProjectInfoToPDF { ...@@ -146,42 +116,93 @@ public class ProjectInfoToPDF {
projNo.add(Chunk.NEWLINE); projNo.add(Chunk.NEWLINE);
} }
projNo.setAlignment(Element.ALIGN_LEFT); projNo.setAlignment(Element.ALIGN_LEFT);
projNo.setSpacingAfter(10); projNo.setSpacingAfter(50f);
document.add(projNo); document.add(projNo);
// 添加标题 // 添加标题
Paragraph title = new Paragraph("省级临床医学中心科研项目申请书", titleFont); Paragraph title = new Paragraph("省级临床医学中心科研项目申请书", titleFont);
title.setAlignment(Element.ALIGN_CENTER); title.setAlignment(Element.ALIGN_CENTER);
title.setSpacingAfter(20); title.setSpacingAfter(50f); // 增加标题后的间距
document.add(title); document.add(title);
// 添加项目基本信息表格
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(95);
table.setSpacingBefore(50f); // 增加表格前的间距
table.setSpacingAfter(10f);
table.setHorizontalAlignment(Element.ALIGN_LEFT); // 设置表格左对齐
// 设置列宽度
float[] columnWidths = {0.35f, 0.65f};
table.setWidths(columnWidths);
// 添加表格内容
addTableRow(table, "项目名称:", project.getProjName(), labelFont, valueFont);
addTableRow(table, "申报单位:", project.getAppUnitName(), labelFont, valueFont);
addTableRow(table, "推荐部门(丙方):", "", labelFont, valueFont);
addTableRow(table, "项目负责人:", project.getAppPersonName(), labelFont, valueFont);
addTableRow(table, "联系电话:", project.getMobile(), labelFont, valueFont);
addTableRow(table, "起止年限:", formatDateRange(project.getStartDate(), 1) + "至" + formatDateRange(project.getEndDate(), 1), labelFont, valueFont);
addTableRow(table, "填报日期:", formatDateRange(project.getCreated(), 2), labelFont, valueFont);
document.add(table);
return document; return document;
} }
//添加表格 /**
* 格式化起止年限
* @param date 开始日期
* @return 格式化后的日期范围字符串
*/
private static String formatDateRange(Date date, int type) {
// 定义日期格式
SimpleDateFormat sdf;
if (type == 1)
sdf = new SimpleDateFormat("yyyy年MM月");
else
sdf = new SimpleDateFormat("yyyy年MM月dd日");
return sdf.format(date);
}
//添加首页项目表格信息
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(25f); labelCell.setMinimumHeight(28f);
labelCell.setVerticalAlignment(Element.ALIGN_MIDDLE); labelCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
labelCell.setHorizontalAlignment(Element.ALIGN_RIGHT); labelCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
labelCell.setPaddingRight(5f); labelCell.setPaddingRight(5f);
labelCell.setBorder(Rectangle.NO_BORDER); labelCell.setBorder(Rectangle.NO_BORDER);
// 创建带下划线的值 // 值单元格
Chunk valueChunk; Phrase valuePhrase;
if (value != null && !value.trim().isEmpty()) { if (value != null && !value.trim().isEmpty()) {
valueChunk = new Chunk(value, valueFont); valuePhrase = new Phrase(value, valueFont);
} else { } else {
valueChunk = new Chunk(" ", valueFont); valuePhrase = new Phrase(" ", valueFont); // 空值时使用一个空格
} }
valueChunk.setUnderline(0.1f, -2f); // 添加下划线
PdfPCell valueCell = new PdfPCell(new Phrase(valueChunk)); PdfPCell valueCell = new PdfPCell(valuePhrase);
valueCell.setMinimumHeight(25f); valueCell.setMinimumHeight(28f);
valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE); valueCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
valueCell.setHorizontalAlignment(Element.ALIGN_LEFT); // 修改为左对齐
valueCell.setPaddingLeft(5f); valueCell.setPaddingLeft(5f);
valueCell.setPaddingRight(10f);
valueCell.setBorder(Rectangle.NO_BORDER); valueCell.setBorder(Rectangle.NO_BORDER);
// 添加自定义事件来绘制下划线
valueCell.setCellEvent(new PdfPCellEvent() {
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
cb.setLineWidth(0.5f);
float y = position.getBottom() + 2; // 调整下划线位置
cb.moveTo(position.getLeft(), y);
cb.lineTo(position.getRight(), y);
cb.stroke();
}
});
table.addCell(labelCell); table.addCell(labelCell);
table.addCell(valueCell); table.addCell(valueCell);
} }
......
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