Commit 9b124aeb authored by wujipeng's avatar wujipeng
Browse files

优化

parent 6ae40a60
......@@ -252,8 +252,8 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
/** 新版模板提供一个横向合并的身份证号码格,直接写入完整号码,避免拆格导致缺位或换行。 */
private void putIdCard(List<RtfEdit> edits, List<RtfRow> rows, String value) {
String idCard = trim(value);
// 9磅字号可以让18位号码在新版合并单元格中保持单行,同时仍便于打印阅读。
putWithFontSize(edits, rows, 4, 2, idCard, 18);
// 新版合并格清空原文后不会保留段落缩进;增加180缇(约3毫米)左缩进,避免号码紧贴边框。
putWithFontSizeAndLeftIndent(edits, rows, 4, 2, idCard, 18, 180);
}
/**
......@@ -284,17 +284,21 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
}
}
/** 把日期拆进模板预留的“年份 / 年 / 月日”窄格,防止完整日期在首格中竖排。 */
/**
* 填写日期一行按“年份值 / 年 / 月份值 / 月 / 日期值 / 日”交替排布。
* 只写第1、3、5号值格并保留模板固定标签,避免把“月”覆盖后造成末尾“日”错位。
*/
private void putSubmitDate(List<RtfEdit> edits, List<RtfRow> rows, Date submitTime) {
if (submitTime == null) {
put(edits, rows, 0, 1, "", true);
put(edits, rows, 0, 3, "", true);
put(edits, rows, 0, 5, "", true);
return;
}
String[] parts = new SimpleDateFormat("yyyy-MM-dd").format(submitTime).split("-");
putWithFontSize(edits, rows, 0, 1, parts[0], 16);
put(edits, rows, 0, 2, "年", true);
putWithFontSize(edits, rows, 0, 3, parts[1] + "月", 12);
putWithFontSize(edits, rows, 0, 4, parts[2] + "日", 12);
putWithFontSize(edits, rows, 0, 3, parts[1], 16);
putWithFontSize(edits, rows, 0, 5, parts[2], 16);
}
private void markOne(List<RtfEdit> edits, List<RtfRow> rows, int rowIndex,
......@@ -345,6 +349,21 @@ public class StudentCertificationAdminServiceImpl implements StudentCertificatio
}
}
/** 在指定字号基础上增加单元格内的段落左缩进;RTF中的缩进单位为缇,20缇等于1磅。 */
private void putWithFontSizeAndLeftIndent(List<RtfEdit> edits, List<RtfRow> rows, int rowIndex,
int cellIndex, Object value, int halfPoints, int leftIndent) {
String text = value == null ? "" : String.valueOf(value).trim();
RtfRow row = rows.get(rowIndex);
int end = row.cellEnds.get(cellIndex);
int start = cellIndex == 0 ? row.start : row.cellEnds.get(cellIndex - 1) + "\\cell".length();
edits.add(new RtfEdit(start, end, null));
if (!text.isEmpty()) {
// \pard重置模板示例文本遗留的段落属性,\intbl确保新段落仍属于当前表格单元格。
edits.add(new RtfEdit(end, end, "\\pard\\intbl\\ql\\li" + leftIndent
+ "\\fs" + halfPoints + " " + encodeRtfText(text)));
}
}
private String applyEdits(String template, List<RtfEdit> edits) {
// 从后向前处理,确保前方插入文字后不会让尚未处理的原模板坐标发生偏移。
edits.sort((left, right) -> Integer.compare(right.start, left.start));
......
......@@ -9,7 +9,7 @@ import java.io.ByteArrayInputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -37,7 +37,11 @@ class B2TemplateInspectionTest {
profile.setTrainingOccupation("焊工"); profile.setTrainingLevel("三级");
profile.setTrainingCategory("社会培训"); profile.setSubsidyFlag(false);
profile.setAssessmentOccupation("电工"); profile.setAssessmentLevel("二级");
profile.setClassId(12); profile.setSubmitTime(new Date());
profile.setClassId(12);
Calendar submitDate = Calendar.getInstance();
submitDate.clear();
submitDate.set(2031, Calendar.DECEMBER, 29);
profile.setSubmitTime(submitDate.getTime());
byte[] bytes = new StudentCertificationAdminServiceImpl().createDocument(profile);
String rtf = new String(bytes, StandardCharsets.US_ASCII);
......@@ -71,6 +75,12 @@ class B2TemplateInspectionTest {
birthdayIndex < 0 ? "birthday missing" : rtf.substring(birthdayIndex - 24, birthdayIndex + 16));
assertTrue(rtf.contains("\\fs16 06"));
assertTrue(rtf.contains("\\fs16 01"));
// 填写日期必须分别进入年、月、日三个值格,不能覆盖模板自带的固定标签格。
assertTrue(rtf.contains("\\fs16 2031"));
assertTrue(rtf.contains("\\fs16 12"));
assertTrue(rtf.contains("\\fs16 29"));
// 身份证合并格保留约3毫米左内边距,防止号码贴住左边框。
assertTrue(rtf.contains("\\pard\\intbl\\ql\\li180\\fs18 310101199506011234"));
assertTrue(visibleText.contains("焊工"));
assertTrue(visibleText.contains("电工"));
}
......
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