1- 准備PDF域 數據
1 public void createNewPDFModel(String rid, String mid, JSONArray array, Map<String, Object> resultMap) throws DocumentException, IOException { 2 // TODO 自動生成的方法存根 3 Match match=matchMapper.selectByPrimaryKey(mid);//賽事信息 4 Race race=raceMapper.selectByPrimaryKey(rid);//賽項信息 5 List<Map<String,Object>> cupList = raceTeamScoreMapper.findCupListByMidRid(mid,rid);//獎杯 6 List<Map<String,Object>> awardsList = raceTeamScoreMapper.findAwardsListByMidRid(mid,rid);//獎項 7 List<Map<String,Object>> entryList = raceTeamScoreMapper.findEntryListByMidRid(mid,rid);//參賽證明 8 9 String mname = removeUnlawfulFileName(match.getMname()); 10 String rname = removeUnlawfulFileName(race.getRname()); 11 //獎勵證書 12 String wordModelPathCup = match.getCupmodel().replace(MyProperties.getKey("RootFileUrlkey"), MyProperties.getKey("RootPathkey")); 13 //wordModelPathCup="D:/pdf/獎勵證書模板V4.0表單.pdf"; 14 for(Map<String,Object> map:cupList){ 15 String tname =removeUnlawfulFileName((String) map.get("tname")); 16 String savePdfPath = MyProperties.getKey("RootPathkey")+"pdf/"+mname+"/"+rname+"/獎勵證書/"+mname+rname+tname+"獎勵證書.pdf"; 17 String url = MyProperties.getKey("RootFileUrlkey")+"pdf/"+mname+"/"+rname+"/獎勵證書/"+mname+rname+tname+"獎勵證書.pdf"; 18 File file = new File(savePdfPath); 19 if (!file.exists()) { 20 // 如果路徑不存在,則創建 21 file.getParentFile().mkdirs(); 22 } 23 Map<String, String> dataParamMap = new HashMap<String, String>(); 24 dataParamMap.put("編號", (String) map.get("cupno")); 25 26 // String name = (String) map.get("school")+" \n\r "+(String) map.get("tname"); 27 dataParamMap.put("學校", (String) map.get("school")); 28 dataParamMap.put("隊伍", (String) map.get("tname")); 29 // dataParamMap.put("學校隊伍", name); 30 31 dataParamMap.put("小項", (String) map.get("rname")); 32 dataParamMap.put("獎杯", (String) map.get("cup")); 33 dataParamMap.put("教師",(String) map.get("teachers")); 34 dataParamMap.put("隊員",(String) map.get("students")); 35 36 PdfModeForPdf.formFdfModeForPdf(dataParamMap, savePdfPath, wordModelPathCup); 37 38 39 RaceTeamScore raceTeamScore = new RaceTeamScore(); 40 raceTeamScore.setTid((String) map.get("tid")); 41 raceTeamScore.setRid((String) map.get("rid")); 42 raceTeamScore.setCupurl(url); 43 raceTeamScore.setCreatstatus("00"); 44 raceTeamScoreMapper.updateByPrimaryKeySelective(raceTeamScore); 45 46 } 47 48 String zipFilePath = MyProperties.getKey("RootPathkey")+"zip/"+mname+"/"+rname+"/"; 49 File file = new File(zipFilePath); 50 if (!file.exists()) { 51 // 如果路徑不存在,則創建 52 file.mkdirs(); 53 } 54 55 sourceFilePath = MyProperties.getKey("RootPathkey")+"pdf/"+mname+"/"+rname+"/獎勵證書"; 56 fileName = mname+rname+"獎勵證書ZIP"; 57 FileToZip.fileToZip(sourceFilePath, zipFilePath, fileName); 58 59 System.out.println("全部生成完畢"); 60 resultMap.put("status", 0); 61 }
2- 繪制PDF
1 package com.zts.robot.util; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.List; 7 import java.util.Map; 8 9 import com.itextpdf.text.Chunk; 10 import com.itextpdf.text.Document; 11 import com.itextpdf.text.DocumentException; 12 import com.itextpdf.text.Element; 13 import com.itextpdf.text.Font; 14 import com.itextpdf.text.Paragraph; 15 import com.itextpdf.text.Rectangle; 16 import com.itextpdf.text.pdf.AcroFields; 17 import com.itextpdf.text.pdf.BaseFont; 18 import com.itextpdf.text.pdf.PdfContentByte; 19 import com.itextpdf.text.pdf.PdfCopy; 20 import com.itextpdf.text.pdf.PdfImportedPage; 21 import com.itextpdf.text.pdf.PdfPCell; 22 import com.itextpdf.text.pdf.PdfPTable; 23 import com.itextpdf.text.pdf.PdfReader; 24 import com.itextpdf.text.pdf.PdfStamper; 25 26 public class PdfModeForPdf { 27 public static void formFdfModeForPdf(Map<String, String> dataParamMap, String savePdfPath, 28 String wordModelPathCup) throws DocumentException, IOException { 29 30 BaseFont simheiChinese = BaseFont.createFont(MyProperties.getKey("simhei"),BaseFont.IDENTITY_H,BaseFont.EMBEDDED); 31 32 PdfReader reader; 33 FileOutputStream out; 34 ByteArrayOutputStream bos; 35 PdfStamper stamper; 36 37 out = new FileOutputStream(savePdfPath);// 輸出流 38 reader = new PdfReader(wordModelPathCup);// 讀取pdf模板 39 bos = new ByteArrayOutputStream(); 40 stamper = new PdfStamper(reader, bos); 41 AcroFields form = stamper.getAcroFields(); 42 43 PdfContentByte pcb = stamper.getOverContent(1); 44 //獲取表單域坐標 45 List<AcroFields.FieldPosition> list = form.getFieldPositions("成員"); 46 Rectangle rect = list.get(0).position; 47 //創建PDF表格 48 PdfPTable table = new PdfPTable(2); 49 float tatalWidth = rect.getRight() - rect.getLeft() - 1; 50 //計算表格寬度 51 float[] columnWidth ={(float) (tatalWidth*0.17),(float)(tatalWidth*0.83)}; 52 table.setTotalWidth(columnWidth); 53 54 Font FontProve = new Font(simheiChinese, 15, 0); 55 Paragraph p11 = new Paragraph("指導教師:", FontProve); 56 Paragraph p12 = new Paragraph((String) dataParamMap.get("教師"), FontProve); 57 Paragraph p21 = new Paragraph("隊 員:", FontProve); 58 Paragraph p22 = new Paragraph((String) dataParamMap.get("隊員"), FontProve); 59 60 PdfPCell cell11 = new PdfPCell(p11); 61 //cell11.setFixedHeight(rect.getTop() - rect.getBottom() - 1); 62 cell11.setBorderWidth(0); 63 cell11.setVerticalAlignment(Element.ALIGN_LEFT); 64 cell11.setHorizontalAlignment(Element.ALIGN_LEFT); 65 cell11.setLeading(0, (float) 1.4); 66 table.addCell(cell11); 67 68 PdfPCell cell12 = new PdfPCell(p12); 69 //cell12.setFixedHeight(rect.getTop() - rect.getBottom() - 1); 70 cell12.setBorderWidth(0); 71 cell12.setVerticalAlignment(Element.ALIGN_LEFT); 72 cell12.setHorizontalAlignment(Element.ALIGN_LEFT); 73 cell12.setLeading(0, (float) 1.4); 74 table.addCell(cell12); 75 76 PdfPCell cell21 = new PdfPCell(p21); 77 //cell11.setFixedHeight(rect.getTop() - rect.getBottom() - 1); 78 cell21.setBorderWidth(0); 79 cell21.setVerticalAlignment(Element.ALIGN_LEFT); 80 cell21.setHorizontalAlignment(Element.ALIGN_LEFT); 81 cell21.setLeading(0, (float) 1.4); 82 table.addCell(cell21); 83 84 PdfPCell cell22 = new PdfPCell(p22); 85 //cell12.setFixedHeight(rect.getTop() - rect.getBottom() - 1); 86 cell22.setBorderWidth(0); 87 cell22.setVerticalAlignment(Element.ALIGN_LEFT); 88 cell22.setHorizontalAlignment(Element.ALIGN_LEFT); 89 cell22.setLeading(0, (float) 1.4); 90 table.addCell(cell22); 91 92 table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), pcb); 93 94 // for (String key : dataParamMap.keySet()) { 95 // form.setField(key,dataParamMap.get(key).toString()); 96 // } 97 98 for (String key : dataParamMap.keySet()) { 99 int ilen = dataParamMap.get(key).toString().length(); 100 if(key == "學校" || key == "隊伍"){ 101 if(ilen >= 20 && ilen < 40){ 102 form.setFieldProperty(key, "textsize", new Float(10), null); 103 }else if(ilen >= 40 && ilen <= 55){ 104 form.setFieldProperty(key, "textsize", new Float(7), null); 105 }else if(ilen > 55){ 106 form.setFieldProperty(key, "textsize", new Float(6), null); 107 } 108 } 109 110 form.setField(key,dataParamMap.get(key).toString()); 111 } 112 113 form.setField("成員", ""); 114 stamper.setFormFlattening(true);// 如果為false那么生成的PDF文件還能編輯,一定要設為true 115 stamper.close(); 116 117 Document doc = new Document(); 118 PdfCopy copy = new PdfCopy(doc, out); 119 doc.open(); 120 PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1); 121 copy.addPage(importPage); 122 doc.close(); 123 124 } 125 126 public static void replaceFormFdfModeForPdf(Map<String, String> dataParamMap, String savePdfPath, 127 String wordModelPathCup) throws DocumentException, IOException { 128 129 BaseFont fzcsjtChinese = BaseFont.createFont(MyProperties.getKey("fzcs"),BaseFont.IDENTITY_H,BaseFont.EMBEDDED); 130 PdfReader reader; 131 FileOutputStream out; 132 ByteArrayOutputStream bos; 133 PdfStamper stamper; 134 135 out = new FileOutputStream(savePdfPath);// 輸出流 136 reader = new PdfReader(wordModelPathCup);// 讀取pdf模板 137 bos = new ByteArrayOutputStream(); 138 stamper = new PdfStamper(reader, bos); 139 AcroFields form = stamper.getAcroFields(); 140 PdfContentByte pcb = stamper.getOverContent(1); 141 142 String prove = form.getField("證明"); 143 //獲取表單域坐標 144 List<AcroFields.FieldPosition> list = form.getFieldPositions("證明"); 145 Rectangle rect = list.get(0).position; 146 //創建PDF表格 147 PdfPTable table = new PdfPTable(1); 148 float tatalWidth = rect.getRight() - rect.getLeft() - 1; 149 //計算表格寬度 150 table.setTotalWidth(tatalWidth); 151 Font FontProve = new Font(fzcsjtChinese, 18, 0); 152 Font FontProveUnderLine = new Font(fzcsjtChinese, 18,Font.UNDERLINE ); 153 Paragraph p1 = new Paragraph(); 154 int indexStart=0; 155 int indexEnd=0; 156 while(prove.length()>0){ 157 158 indexStart = prove.indexOf("<"); 159 indexEnd = prove.indexOf(">"); 160 if(indexStart==-1){ 161 p1.add(new Chunk(prove, FontProve)); 162 break; 163 }else{ 164 String key = prove.substring(indexStart+1, indexEnd); 165 String str = prove.substring(0,indexStart); 166 prove = prove.substring(indexEnd+1); 167 p1.add(new Chunk(str, FontProve)); 168 p1.add(new Chunk(dataParamMap.get(key), FontProveUnderLine)); 169 } 170 } 171 172 173 PdfPCell cell = new PdfPCell(p1); 174 cell.setFixedHeight(rect.getTop() - rect.getBottom() - 1); 175 cell.setBorderWidth(0); 176 cell.setVerticalAlignment(Element.ALIGN_LEFT); 177 cell.setHorizontalAlignment(Element.ALIGN_LEFT); 178 cell.setLeading(0, (float) 1.7); 179 table.addCell(cell); 180 table.writeSelectedRows(0, -1, rect.getLeft(), rect.getTop(), pcb); 181 182 form.setField("編號", dataParamMap.get("編號").toString()); 183 form.setField("證明", ""); 184 185 stamper.setFormFlattening(true);// 如果為false那么生成的PDF文件還能編輯,一定要設為true 186 stamper.close(); 187 188 Document doc = new Document(); 189 PdfCopy copy = new PdfCopy(doc, out); 190 doc.open(); 191 PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1); 192 copy.addPage(importPage); 193 doc.close(); 194 195 } 196 }
有PDF模板的情況下,重新繪制PDF
1- 使用 Adobe Acrobat DC
2- 編輯PDF > 准備表單
代碼中讀取PDF模板,按照排版好的域,順序寫入內容
1 AcroFields form = stamper.getAcroFields();
for (String key : dataParamMap.keySet()) { int ilen = dataParamMap.get(key).toString().length(); if(key == "學校" || key == "隊伍"){ if(ilen >= 20 && ilen < 40){ form.setFieldProperty(key, "textsize", new Float(10), null); }else if(ilen >= 40 && ilen <= 55){ form.setFieldProperty(key, "textsize", new Float(7), null); }else if(ilen > 55){ form.setFieldProperty(key, "textsize", new Float(6), null); } } form.setField(key,dataParamMap.get(key).toString()); }
reference:
https://itextpdf.com/