|
| 1 | +package cn.lynu.lyq.java_exam.test; |
| 2 | + |
| 3 | +import java.io.FileOutputStream; |
| 4 | + |
| 5 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 6 | + |
| 7 | +import com.itextpdf.text.BaseColor; |
| 8 | +import com.itextpdf.text.Chunk; |
| 9 | +import com.itextpdf.text.Document; |
| 10 | +import com.itextpdf.text.DocumentException; |
| 11 | +import com.itextpdf.text.Font; |
| 12 | +import com.itextpdf.text.PageSize; |
| 13 | +import com.itextpdf.text.Paragraph; |
| 14 | +import com.itextpdf.text.pdf.BaseFont; |
| 15 | +import com.itextpdf.text.pdf.PdfWriter; |
| 16 | + |
| 17 | +import cn.lynu.lyq.java_exam.dao.BankQuestionDao; |
| 18 | +import cn.lynu.lyq.java_exam.entity.BankBlankFillingQuestion; |
| 19 | +import cn.lynu.lyq.java_exam.entity.BankChoiceQuestion; |
| 20 | +import cn.lynu.lyq.java_exam.entity.BankJudgeQuestion; |
| 21 | + |
| 22 | +public class PDFGenerateTest2 { |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception{ |
| 25 | + System.out.println("\u2713"); |
| 26 | + System.out.println("\u2717"); |
| 27 | + Document doc = new Document(PageSize.A4); |
| 28 | + PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream("c:\\Problems.pdf")); |
| 29 | + doc.open(); |
| 30 | + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); |
| 31 | + BankQuestionDao bankQuestionDao = (BankQuestionDao) context.getBean("bankQuestionDao"); |
| 32 | + for(int i=1; i<=5; i++){ |
| 33 | + BankChoiceQuestion bq = bankQuestionDao.findChoiceById(i); |
| 34 | + addChoiceQuestionAndAnswer(doc,bq); |
| 35 | + } |
| 36 | + |
| 37 | + for(int i=1; i<=5; i++){ |
| 38 | + BankJudgeQuestion bq = bankQuestionDao.findJudgeById(i); |
| 39 | + addJudgeQuestionAndAnswer(doc,bq); |
| 40 | + } |
| 41 | + |
| 42 | + for(int i=1; i<=5; i++){ |
| 43 | + BankBlankFillingQuestion bq = bankQuestionDao.findBlankFillingById(i); |
| 44 | + addBlankFillingQuestionAndAnswer(doc,bq); |
| 45 | + } |
| 46 | + doc.close(); |
| 47 | + context.close(); |
| 48 | + } |
| 49 | + |
| 50 | + public static void addChoiceQuestionAndAnswer(Document doc, BankChoiceQuestion bq) throws Exception{ |
| 51 | + |
| 52 | + BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); |
| 53 | + Font fontCn = new Font(bfChinese, 20, Font.NORMAL); |
| 54 | + Font fontCn2 = new Font(bfChinese, 12, Font.NORMAL); |
| 55 | + Font fontEn = new Font(Font.FontFamily.TIMES_ROMAN,19,Font.NORMAL); |
| 56 | + |
| 57 | + addParagraphForChineseAndEnglish(bq.getId()+"."+bq.getContent(),doc,fontCn,fontEn); |
| 58 | + addChunkForChineseAndEnglish(bq.getChoiceA(),doc,fontCn,fontEn); |
| 59 | + doc.add(Chunk.NEWLINE); |
| 60 | + addChunkForChineseAndEnglish(bq.getChoiceB(),doc,fontCn,fontEn); |
| 61 | + doc.add(Chunk.NEWLINE); |
| 62 | + addChunkForChineseAndEnglish(bq.getChoiceC(),doc,fontCn,fontEn); |
| 63 | + doc.add(Chunk.NEWLINE); |
| 64 | + addChunkForChineseAndEnglish(bq.getChoiceD(),doc,fontCn,fontEn); |
| 65 | + doc.add(Chunk.NEWLINE); |
| 66 | + addChunkForChineseAndEnglish("答案是:"+bq.getAnswer(),doc,fontCn,fontEn); |
| 67 | + |
| 68 | + Chunk ck=new Chunk(bq.getKnowledgePoint().substring(0, bq.getKnowledgePoint().indexOf('*')),fontCn2); |
| 69 | + ck.setBackground(BaseColor.ORANGE, 1f,2f,2f,1f); |
| 70 | + ck.setTextRise(12.5f); |
| 71 | + doc.add(ck); |
| 72 | + |
| 73 | + doc.add(Chunk.NEWLINE); |
| 74 | + doc.add(Chunk.NEWLINE); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public static void addJudgeQuestionAndAnswer(Document doc, BankJudgeQuestion bq) throws Exception{ |
| 80 | + |
| 81 | + BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); |
| 82 | + Font fontCn = new Font(bfChinese, 20, Font.NORMAL); |
| 83 | + Font fontCn2 = new Font(bfChinese, 12, Font.NORMAL); |
| 84 | + Font fontEn = new Font(Font.FontFamily.TIMES_ROMAN,19,Font.NORMAL); |
| 85 | + |
| 86 | + addParagraphForChineseAndEnglish(bq.getId()+"."+bq.getContent(),doc,fontCn,fontEn); |
| 87 | + doc.add(new Chunk("答案是:"+(bq.getAnswer().equals("T")?"正确":"错误"),fontCn)); |
| 88 | + |
| 89 | + Chunk ck=new Chunk(bq.getKnowledgePoint().substring(0, bq.getKnowledgePoint().indexOf('*')),fontCn2); |
| 90 | + ck.setBackground(BaseColor.ORANGE, 1f,2f,2f,1f); |
| 91 | + ck.setTextRise(12.5f); |
| 92 | + doc.add(ck); |
| 93 | + |
| 94 | + doc.add(Chunk.NEWLINE); |
| 95 | + doc.add(Chunk.NEWLINE); |
| 96 | + } |
| 97 | + |
| 98 | + public static void addBlankFillingQuestionAndAnswer(Document doc, BankBlankFillingQuestion bq) throws Exception{ |
| 99 | + |
| 100 | + BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); |
| 101 | + Font fontCn = new Font(bfChinese, 20, Font.NORMAL); |
| 102 | + Font fontCn2 = new Font(bfChinese, 12, Font.NORMAL); |
| 103 | + Font fontEn = new Font(Font.FontFamily.TIMES_ROMAN,19,Font.NORMAL); |
| 104 | + |
| 105 | + addParagraphForChineseAndEnglish(bq.getId()+"."+bq.getContent(),doc,fontCn,fontEn); |
| 106 | + addChunkForChineseAndEnglish("答案是:"+bq.getAnswer(),doc,fontCn,fontEn); |
| 107 | + |
| 108 | + Chunk ck=new Chunk(bq.getKnowledgePoint().substring(0, bq.getKnowledgePoint().indexOf('*')),fontCn2); |
| 109 | + ck.setBackground(BaseColor.ORANGE, 1f,2f,2f,1f); |
| 110 | + ck.setTextRise(12.5f); |
| 111 | + doc.add(ck); |
| 112 | + |
| 113 | + doc.add(Chunk.NEWLINE); |
| 114 | + doc.add(Chunk.NEWLINE); |
| 115 | + } |
| 116 | + |
| 117 | + public static void addChunkForChineseAndEnglish(String str, Document doc, Font fontCn, Font fontEn) throws DocumentException{ |
| 118 | + StringBuilder sb = new StringBuilder(); |
| 119 | + char[] chars = str.toCharArray(); |
| 120 | + for(int i=0; i<chars.length; i++){ |
| 121 | + if(i==0){ |
| 122 | + sb.append(chars[i]); |
| 123 | + }else{ |
| 124 | + if(isChinese(chars[i]) && !isChinese(chars[i-1])){ |
| 125 | + doc.add(new Chunk(sb.toString(),fontEn)); |
| 126 | + sb = new StringBuilder(); |
| 127 | + sb.append(chars[i]); |
| 128 | + }else if(!isChinese(chars[i]) && isChinese(chars[i-1])){ |
| 129 | + doc.add(new Chunk(sb.toString(),fontCn)); |
| 130 | + sb = new StringBuilder(); |
| 131 | + sb.append(chars[i]); |
| 132 | + }else{ |
| 133 | + sb.append(chars[i]); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if(i==chars.length-1){ |
| 138 | + if(isChinese(chars[i])){ |
| 139 | + doc.add(new Chunk(sb.toString(),fontCn)); |
| 140 | + }else{ |
| 141 | + doc.add(new Chunk(sb.toString(),fontEn)); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + public static void addParagraphForChineseAndEnglish(String str, Document doc, Font fontCn, Font fontEn) throws DocumentException{ |
| 149 | + StringBuilder sb = new StringBuilder(); |
| 150 | + Paragraph para = new Paragraph(); |
| 151 | + char[] chars = str.toCharArray(); |
| 152 | + for(int i=0; i<chars.length; i++){ |
| 153 | + if(i==0){ |
| 154 | + sb.append(chars[i]); |
| 155 | + }else{ |
| 156 | + if(isChinese(chars[i]) && !isChinese(chars[i-1])){ |
| 157 | + para.add(new Chunk(sb.toString(),fontEn)); |
| 158 | + sb = new StringBuilder(); |
| 159 | + sb.append(chars[i]); |
| 160 | + }else if(!isChinese(chars[i]) && isChinese(chars[i-1])){ |
| 161 | + para.add(new Chunk(sb.toString(),fontCn)); |
| 162 | + sb = new StringBuilder(); |
| 163 | + sb.append(chars[i]); |
| 164 | + }else{ |
| 165 | + sb.append(chars[i]); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + if(i==chars.length-1){ |
| 170 | + if(isChinese(chars[i])){ |
| 171 | + para.add(new Chunk(sb.toString(),fontCn)); |
| 172 | + }else{ |
| 173 | + para.add(new Chunk(sb.toString(),fontEn)); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + para.setLeading(25f); |
| 178 | + doc.add(para); |
| 179 | + } |
| 180 | + |
| 181 | + private static final boolean isChinese(char c) { |
| 182 | + Character.UnicodeBlock ub = Character.UnicodeBlock.of(c); |
| 183 | + if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS |
| 184 | + || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS |
| 185 | + || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A |
| 186 | + || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION |
| 187 | + || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION |
| 188 | + || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) { |
| 189 | + return true; |
| 190 | + } |
| 191 | + return false; |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments