重点解决中文不显示的代码部分:
以及不同字体颜色处理:
//中文字体,解决中文不能显示问题 BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // BaseFont bfChinese = BaseFont.createFont("Helvetica", "Cp1252", BaseFont.NOT_EMBEDDED); //蓝色字体 Font blueFont = new Font(bfChinese); blueFont.setColor(BaseColor.BLUE); //绿色字体 Font greenFont = new Font(bfChinese); greenFont.setColor(BaseColor.GREEN); //红色字体 Font redFont = new Font(bfChinese); redFont.setColor(BaseColor.RED); //黑色字体 Font blackFont = new Font(bfChinese); blackFont.setColor(BaseColor.BLACK);
maven 依赖:
<!--java pdf的生成和导出依赖--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.8</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.1.5</version> </dependency>
@Override
public ResultMessage<String> exportExamResultPdf(UserExamScoreVo userExamScore) throws IOException, DocumentException {
List<RecruitExamQuestion> recruitExamQuestions = recruitExamQuestionMapper.selectByExamIdAndUserId(userExamScore);
ExamQuestionAnswerExample examQuestionAnswerExample = new ExamQuestionAnswerExample();
examQuestionAnswerExample.createCriteria().andUserIdEqualTo(userExamScore.getUserId()).andExamIdEqualTo(userExamScore.getExamId());
List<ExamQuestionAnswer> examQuestionAnswers = examQuestionAnswerMapper.selectByExample(examQuestionAnswerExample);
ArrayList<QuestionSingleVo> questionSingleVos = new ArrayList<>();
ArrayList<QuestionMultipleChoiceVo> questionMultipleChoiceVos = new ArrayList<>();
ArrayList<QuestionJudgeVo> questionJudgeVos = new ArrayList<>();
ArrayList<QuestionSubjectiveVo> questionBlankVos = new ArrayList<>();
ArrayList<QuestionSubjectiveVo> questionSolveVos = new ArrayList<>();
ArrayList<QuestionSubjectiveVo> questionAnalysisVos = new ArrayList<>();
List<String> ids = getRecruitmentIds(recruitExamQuestions, "0");
List<String> multiIds = getRecruitmentIds(recruitExamQuestions, "1");
List<String> judgeIds = getRecruitmentIds(recruitExamQuestions, "2");
List<String> subjectIds = new LinkedList<>();
recruitExamQuestions.forEach(e -> {
if (StringUtils.equals(e.getQuestionTypeId(), "3") || StringUtils.equals(e.getQuestionTypeId(), "4") || StringUtils.equals(e.getQuestionTypeId(), "5")) {
subjectIds.add(e.getQuestionId());
}
});
List<QuestionChoice> questionChoicesBatchList = new LinkedList<>();
List<QuestionChoice> questionMulChoicesBatchList = new LinkedList<>();
List<QuestionSingle> selectQuestionSingleBatch = new LinkedList<>();
List<QuestionMultipleChoice> selectQuestionMultiBatch = new LinkedList<>();
List<QuestionJudge> selectQuestionJudgeBatch = new LinkedList<>();
List<QuestionSubjective> selectQuestionSubjectBatch = new LinkedList<>();
if (!ids.isEmpty()) {
questionChoicesBatchList = questionChoiceMapper.selectQuestionChoicesBatch(ids);
}
if (!multiIds.isEmpty()) {
questionMulChoicesBatchList = questionChoiceMapper.selectQuestionChoicesBatch(multiIds);
}
if (!ids.isEmpty()) {
selectQuestionSingleBatch = questionSingleMapper.selectQuestionSingleBatch(ids);
}
if (!multiIds.isEmpty()) {
selectQuestionMultiBatch = questionMultipleChoiceMapper.selectQuestionMultiBatch(multiIds);
}
if (!judgeIds.isEmpty()) {
selectQuestionJudgeBatch = questionJudgeMapper.selectQuestionJudgeBatch(judgeIds);
}
if (!subjectIds.isEmpty()) {
selectQuestionSubjectBatch = questionSubjectiveMapper.selectQuestionSubjectBatch(subjectIds);
}
for (String id : ids) {
QuestionSingle questionSingle = selectQuestionSingleBatch.stream().filter(q -> StringUtils.equals(q.getId(), id)).findFirst().orElse(null);
QuestionSingleVo questionSingleVo = new QuestionSingleVo();
ExamQuestionAnswer examQuestionAnswer = examQuestionAnswers.stream().filter(e -> StringUtils.equals(e.getExamQuestionId(), id)).findFirst().orElse(null);
BeanUtils.copyProperties(questionSingle, questionSingleVo);
questionSingleVo.setAnswer(examQuestionAnswer.getAnswer());
questionSingleVo.setIsRight(examQuestionAnswer.getIsRight());
List<QuestionChoice> questionChoiceList = questionChoicesBatchList.stream().filter(c -> StringUtils.equals(c.getQuestionId(), id)).collect(Collectors.toList());
questionSingleVo.setChoices(questionChoiceList);
questionSingleVos.add(questionSingleVo);
}
for (String multiId : multiIds) {
QuestionMultipleChoiceVo questionMultipleChoiceVo = new QuestionMultipleChoiceVo();
QuestionMultipleChoice questionMultipleChoice = selectQuestionMultiBatch.stream().filter(q -> StringUtils.equals(q.getId(), multiId)).findFirst().orElse(null);
BeanUtils.copyProperties(questionMultipleChoice, questionMultipleChoiceVo);
ExamQuestionAnswer examQuestionAnswer = examQuestionAnswers.stream().filter(e -> StringUtils.equals(e.getExamQuestionId(), multiId)).findFirst().orElse(null);
if (StringUtils.isNotBlank(examQuestionAnswer.getAnswer())) {
String[] answerArray = examQuestionAnswer.getAnswer().split("_");
List<String> answerList = new LinkedList<>((Arrays.asList(answerArray)));
questionMultipleChoiceVo.setAnswers(answerList);
}
questionMultipleChoiceVo.setIsRight(examQuestionAnswer.getIsRight());
List<QuestionChoice> questionChoiceList = questionMulChoicesBatchList.stream().filter(c -> StringUtils.equals(c.getQuestionId(), multiId)).collect(Collectors.toList());
questionMultipleChoiceVo.setChoices(questionChoiceList);
questionMultipleChoiceVos.add(questionMultipleChoiceVo);
}
for (String judgeId : judgeIds) {
QuestionJudgeVo questionJudgeVo = new QuestionJudgeVo();
QuestionJudge questionJudge = selectQuestionJudgeBatch.stream().filter(q -> StringUtils.equals(q.getId(), judgeId)).findFirst().orElse(null);
BeanUtils.copyProperties(questionJudge, questionJudgeVo);
ExamQuestionAnswer examQuestionAnswer = examQuestionAnswers.stream().filter(e -> StringUtils.equals(e.getExamQuestionId(), judgeId)).findFirst().orElse(null);
questionJudgeVo.setAnswer(examQuestionAnswer.getAnswer());
questionJudgeVo.setIsRight(examQuestionAnswer.getIsRight());
questionJudgeVos.add(questionJudgeVo);
}
for (String subjectId : subjectIds) {
QuestionSubjectiveVo questionSubjectiveVo = new QuestionSubjectiveVo();
QuestionSubjective questionSubjective = selectQuestionSubjectBatch.stream().filter(q -> StringUtils.equals(q.getId(), subjectId)).findFirst().orElse(null);
BeanUtils.copyProperties(questionSubjective, questionSubjectiveVo);
ExamQuestionAnswer examQuestionAnswer = examQuestionAnswers.stream().filter(e -> StringUtils.equals(e.getExamQuestionId(), subjectId)).findFirst().orElse(null);
if (examQuestionAnswer != null) {
// 选择对应题的uri图片,如果没有传题那么将不会有uri
ExamQuestionAnswerImgExample examQuestionAnswerImgExample = new ExamQuestionAnswerImgExample();
examQuestionAnswerImgExample.createCriteria().andExamQuestionAnswerIdEqualTo(examQuestionAnswer.getId());
List<ExamQuestionAnswerImg> examQuestionAnswerImgs = examQuestionAnswerImgMapper.selectByExample(examQuestionAnswerImgExample);
if (!examQuestionAnswerImgs.isEmpty()) {
questionSubjectiveVo.setUrl(examQuestionAnswerImgs.get(0).getImgUrl());
}
questionSubjectiveVo.setIsRight(examQuestionAnswer.getIsRight());
questionSubjectiveVo.setUserAnswer(examQuestionAnswer.getAnswer());
}
if (StringUtils.equals(questionSubjective.getQuestionType(), "3")) {
questionBlankVos.add(questionSubjectiveVo);
} else if (StringUtils.equals(questionSubjective.getQuestionType(), "4")) {
questionSolveVos.add(questionSubjectiveVo);
} else {
questionAnalysisVos.add(questionSubjectiveVo);
}
}
ArrayList<Object> examList = new ArrayList<>();
ExamVo examVo = new ExamVo();
Exam exam1 = examMapper.selectByPrimaryKey(userExamScore.getExamId());
BeanUtils.copyProperties(exam1, examVo);
examList.add(questionSingleVos);
examList.add(questionMultipleChoiceVos);
examList.add(questionJudgeVos);
examList.add(questionBlankVos);
examList.add(questionSolveVos);
examList.add(questionAnalysisVos);
ResumePerson resumePerson = resumePersonMapper.selectByPrimaryKey(userExamScore.getUserId());
/**
*
*/ /**
*
*/ //创建文件
Document document = new Document();
//建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C://Users/liudaka/Desktop/" + resumePerson.getName() + "的考试结果.pdf"));
PdfWriter writer2 = PdfWriter.getInstance(document, new FileOutputStream("/data/site/cuckoo/" + resumePerson.getName() + "的考试结果.pdf"));
//打开文件
document.open();
//中文字体,解决中文不能显示问题
BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
// BaseFont bfChinese = BaseFont.createFont("Helvetica", "Cp1252", BaseFont.NOT_EMBEDDED);
//蓝色字体
Font blueFont = new Font(bfChinese);
blueFont.setColor(BaseColor.BLUE);
//绿色字体
Font greenFont = new Font(bfChinese);
greenFont.setColor(BaseColor.GREEN);
//红色字体
Font redFont = new Font(bfChinese);
redFont.setColor(BaseColor.RED);
//黑色字体
Font blackFont = new Font(bfChinese);
blackFont.setColor(BaseColor.BLACK);
//段落文本
Paragraph paragraphBlue = new Paragraph(exam1.getName() + "考试试卷解析", blackFont);
paragraphBlue.setIndentationLeft(160);
document.add(paragraphBlue);
Paragraph info = new Paragraph(resumePerson.getName(), blackFont);
paragraphBlue.setIndentationLeft(60);
document.add(info);
Paragraph score = new Paragraph(userExamScore.getScore(), blackFont);
paragraphBlue.setIndentationLeft(60);
document.add(score);
/**
* 获取时间转换为小时。
*/
try {
long min = dateDiff("2014-05-27 13:30:00", "2014-05-27 13:00:00", "yyyy-MM-dd HH:mm:ss");
// System.out.println("---------相隔分钟数: " + min);
Paragraph time = new Paragraph(String.valueOf(min), blackFont); paragraphBlue.setIndentationLeft(60);
document.add(time);
} catch (Exception e) {
e.printStackTrace();
}
String[] singleName = {"A", "B", "C", "D", "E", "F", "G"};
String answerId = "";
String answerContent = "";
for (int i = 0; i < questionSingleVos.size(); i++) {
Paragraph chunk2 = new Paragraph(i + 1 + "、" + questionSingleVos.get(i).getContent().trim(), blackFont);
chunk2.setExtraParagraphSpace(20);
// chunk2.setSpacingAfter(40);
chunk2.setSpacingBefore(40);
document.add(chunk2);
List<QuestionChoice> choices = questionSingleVos.get(i).getChoices();
for (int j = 0; j < choices.size(); j++) {
if (StringUtils.equals(choices.get(j).getIsTrue(), "1")) {
// 标准答案显示绿色
answerId = choices.get(j).getId();
answerContent = choices.get(j).getOptionContent();
Paragraph sectionContent1 = new Paragraph(singleName[j] + " : " + choices.get(j).getOptionContent(), greenFont);
sectionContent1.setIndentationLeft(20);
sectionContent1.setSpacingBefore(10);
document.add(sectionContent1);
} else {
Paragraph sectionContent2 = new Paragraph(singleName[j] + " : " + choices.get(j).getOptionContent(), blackFont);
sectionContent2.setIndentationLeft(20);
sectionContent2.setSpacingBefore(10);
document.add(sectionContent2);
}
}
String answer = questionSingleVos.get(i).getAnswer();
if (StringUtils.equals(answer, answerId)) {
// 答案相同显示绿色
Paragraph sectionContent4 = new Paragraph("您的答案:" + answerContent + "(正确)", greenFont);
sectionContent4.setIndentationLeft(20);
sectionContent4.setSpacingBefore(10);
document.add(sectionContent4);
} else {
// 答案不相同显示红色
choices.stream().filter(c -> StringUtils.equals(c.getId(), answer));
Paragraph sectionContent3 = new Paragraph("您的答案:" + choices.get(0).getOptionContent() + "(错误)", redFont);
sectionContent3.setSpacingBefore(10);
sectionContent3.setIndentationLeft(20);
document.add(sectionContent3);
}
}
HashSet<String> mulChoices = new HashSet<>();
for (int i = 0; i < questionMultipleChoiceVos.size(); i++) {
Paragraph mulContent = new Paragraph(questionSingleVos.size() + i + 1 + "、" + questionSingleVos.get(i).getContent().trim(), blackFont);
mulContent.setExtraParagraphSpace(40);
// mulContent.setSpacingAfter(40);
mulContent.setSpacingBefore(40);
document.add(mulContent);
List<QuestionChoice> choices = questionMultipleChoiceVos.get(i).getChoices();
for (int j = 0; j < choices.size(); j++) {
if (StringUtils.equals(choices.get(j).getIsTrue(), "1")) {
// 标准答案显示绿色
mulChoices.add(choices.get(j).getId());
answerContent = choices.get(j).getOptionContent();
Paragraph sectionContent1 = new Paragraph(singleName[j] + " : " + choices.get(j).getOptionContent(), greenFont);
sectionContent1.setIndentationLeft(20);
sectionContent1.setSpacingBefore(10);
document.add(sectionContent1);
} else {
Paragraph sectionContent2 = new Paragraph(singleName[j] + " : " + choices.get(j).getOptionContent(), blackFont);
sectionContent2.setIndentationLeft(20);
sectionContent2.setSpacingBefore(10);
document.add(sectionContent2);
}
}
HashSet<String> answers = new HashSet<>(questionMultipleChoiceVos.get(i).getAnswers());
answers.retainAll(mulChoices);
List<QuestionChoice> choices1 = new ArrayList<>();
if (answers.equals(mulChoices)) {
// 答案相同显示绿色
for (String mulChoice : mulChoices) {
/*choices1 = choices;
choices1.stream().filter(c -> StringUtils.equals(c.getId(), mulChoice));
Paragraph sectionContent4 = new Paragraph("您的答案:" + choices1.get(0).getOptionContent() + "(正确)", greenFont);
sectionContent4.setIndentationLeft(20);
sectionContent4.setSpacingBefore(10);
document.add(sectionContent4);*/
choices.forEach(c -> {
if (StringUtils.equals(c.getId(), mulChoice)) {
Paragraph sectionContent4 = new Paragraph("您的答案:" + c.getOptionContent() + "(正确)", greenFont);
sectionContent4.setIndentationLeft(20);
sectionContent4.setSpacingBefore(10);
try {
document.add(sectionContent4);
} catch (DocumentException e) {
e.printStackTrace();
}
}
});
}
} else {
for (String mulChoice : answers) {
choices.forEach(c -> {
if (StringUtils.equals(c.getId(), mulChoice)) {
Paragraph sectionContent4 = new Paragraph("您的答案:" + c.getOptionContent() + "(错误)", redFont);
sectionContent4.setIndentationLeft(20);
sectionContent4.setSpacingBefore(10);
try {
document.add(sectionContent4);
} catch (DocumentException e) {
e.printStackTrace();
}
}
});
}
}
}
//关闭文档
document.close();
//关闭书写器
writer.close();
//examList.add(questionAnalyseService.listAnalyseResult(userExamScore.getId()));
// ResumePerson resumePerson = resumePersonMapper.selectByPrimaryKey(userExamScore.getUserId());
//String dest = "C://Users/liudaka/Desktop-" + resumePerson.getName() + "-考试结果.pdf";
// String dest = "/data/site/cuckoo/软件-" + oneResume.getName() + "-录用通知书(实习).pdf";
/*
try {
String buildExamHtml = CommonUtil.buildExamHtml(resumePerson.getName(), examList);
JavaToPdfHtmlFreeMarker.createPdf(buildExamHtml, dest);
} catch (IOException | com.lowagie.text.DocumentException e) {
e.printStackTrace();
throw new CuckooException("E544440", "pdf转换异常");
// throw new ThrushException(Constant.SEND_EMAIL_ERROR, "发送邮件失败");
}*/
/* try {
toPdf(questionSingleVos, questionMultipleChoiceVos);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}*/
return ResultMessage.success();
}