最近公司项目有个需求,批量打印荣誉证书,一开始尝试过传统的网络打印,控件打印,JS调用浏览器打印方法,遇到各种问题,比如定位不准,分页问题,缩放问题等。然后就自己研究,整理了一套打印方案,项目已测试通过,现在把这一成果分享给大家。
下面,我会提供一个工具类,这个工具类里,会有两种打印方案:
一。把要打印的内容,放到HTML代码中,然后解析HTML(虽然支持解析样式,由于支持的样式非常少,字体字号样式都不支持,所以这种方式并没有太大意义),解析后生成PDF文件,然后下载到客户端。
二,先生成多页的空白PDF,然后将文字通过添加水印(可设置透明度)的方式,输出指定位置。
注意事项:在这个过程中,会产生一些临时文件,注意临时文件存放目录,由于项目运行时必然有并发问题,临时文件名一定要取时间戳或UUID这种不会重复的名字,还有临时文件使用完成后要及时删除,删除前要关闭IO流对象,PDF格式书写器对象(关闭要由外到里,一定要关闭掉,因为JVM的GC无法回收这些资源,否则是不能删除这些被占用的临时文件的)
代码里使用的字体文件xxx.TTF,到windows电脑字体库里拿“C:\ Windows \ Fonts”
相关jar下载链接:https://download.youkuaiyun.com/download/qq_29062045/10519043
包com.chinauip.contract.service.contract; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.FontProvider; import com.itextpdf.text.PageSize; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfContentByte; import com.itextpdf.text.pdf.PdfCopy; import com.itextpdf.text.pdf.PdfGState; import com.itextpdf.text.pdf.PdfImportedPage; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; / ** * * @author YuGongWen 2018年6月27日上午八点20分31秒packagepath的: * com.chinauip.contract.service.contract.pdfPrintUtil.java * * / 公共类pdfPrintUtil { / ** *生成打印PDF文件(多个企业)* * @参数printParam * @返回* @throws 异常* / 公共静态字符串creatPdf(列表<地图<字符串,对象>> printParam,字符串modeltype,字符串路径)抛出异常{ 列表<字符串>文件=新ArrayList <字符串>(); for(int i = 0; 我<printParam.size(); i ++){ String filePath = new pdfPrintUtil()。createSinglePdf(path,printParam.get(i),modeltype); files.add(文件路径); } String savepath = path + String.valueOf(new Date()。getTime())+“。pdf”; savepath = mergePdfFiles(files,savepath); for(int i = 0; 我<files.size(); i ++){ forceDelete(files.get(i)); } 的System.out.println(“合成PDF的路径:”+ savepath); return savepath; } / ** *生成的HTML模板代码(模板1)* * @参数parMap * @返回* @throws UnsupportedEncodingException * / 公共静态字符串getHtml1(地图<字符串,对象> parMap)抛出UnsupportedEncodingException { 字符串certificate_number = parMap.get( “certificate_number”)!= null?parMap.get(“certificate_number”)。toString():“”; String year = parMap.get(“year”)! = null?parMap.get(“year”)。toString():“”; String enterprise_name = parMap.get(“enterprise_name”)!= null?parMap.get(“enterprise_name”)。toString():“”; String month = parMap.get(“month”)!= null?parMap.get(“month”)。toString():“”; String day = parMap.get(“day”)!= null?parMap.get(“day”)。toString():“”; String first_time = parMap.get(“first_time”)!= null?parMap.get(“first_time”)。toString():“”; StringBuffer html = new StringBuffer(); html.append(“<div id ='model1'align ='center'>”); html.append(“<div style ='margin-left:60px'align ='center'>”); html.append(“<table border ='1'style ='width:100%; 高度: html.append(“<td style ='width:40%'>”); html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”+ year); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<tr style ='height:100px;'>”); html.append(“<td style ='font-family:楷体';>”); html.append(“&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&quot;”); html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& “&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;”); html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; ;“); html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;” + certificate_number +“ <br />“); html.append(“& html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<tr style ='height:100px;'>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<tr style ='height:100px;'>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<tr style ='height:100px;'>”); html.append(“<TD>”); html.append(&& NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; html.append(“&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&quot;”); html.append(“&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;”); html.append(年+“&nbsp;&nbsp;&nbsp;”+月+“&nbsp;&nbsp;&nbsp;”+ day); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“<tr style ='height:100px;'>”); html.append(“<TD>”); html.append(“&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;”); html.append(“&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; html.append(“</ DIV>”); return html.toString(); } / ** *生成的HTML模板代码(模板2)* * @参数parMap * @返回* @throws UnsupportedEncodingException * / 公共静态字符串getHtml2(地图<字符串,对象> parMap)抛出UnsupportedEncodingException { 字符串年= parMap.get( “年”)!= null?parMap.get(“year”)。toString():“”; String enterprise_name = parMap.get(“enterprise_name”)!= null?parMap.get(“enterprise_name”)。toString():“”; String month = parMap.get(“month”)!= null?parMap.get(“month”)。toString():“”; String day = parMap.get(“day”)!= null?parMap.get(“天”)。 String first_time = parMap.get(“first_time”)!= null?parMap.get(“first_time”)。toString():“”; StringBuffer html = new StringBuffer(); html.append(“<div id ='model2'align ='center'>”); html.append(“<div align ='center'>”); html.append(“<table border ='0'style ='width:80%; 高度:80%“>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<td width ='70%'>”); html.append(“</ TD>”); html.append(“<TD>”); HTML。 html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“< return html.toString(); } / ** *生成的HTML模板代码(模板3)* * @参数parMap * @返回* @throws UnsupportedEncodingException * / 公共静态字符串getHtml3(地图<字符串,对象> parMap)抛出UnsupportedEncodingException { 字符串年= parMap.get( “年”)!= null?parMap.get(“year”)。toString():“”; String enterprise_name = parMap.get(“enterprise_name”)!= null?parMap.get(“enterprise_name”)。toString():“”; String month = parMap.get(“month”)!= null?parMap.get(“month”)。toString():“”; String day = parMap.get(“day”)!= null?parMap.get(“day”)。toString():“”; String first_time = parMap.get(“first_time”)!= null?parMap.get(“first_time”)。toString():“”; StringBuffer html = new StringBuffer(); html.append(“<div id ='model3'align ='center'>”); html.append(“<table border ='0'style ='width:80%; 高度:80%“>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“< html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); HTML。附加(“</ TR>”); html.append(“</ TABLE>”); html.append(“</ DIV>”); return html.toString(); } / ** *生成html模板代码(模板4) * * @param parMap * @return * @throws UnsupportedEncodingException * / public static String getHtml4(Map <String,Object> parMap)抛出UnsupportedEncodingException { String year = parMap.get(“year”)!= null?parMap.get( “年”)的toString():“”。String enterprise_name = parMap.get(“enterprise_name”)!= null?parMap.get(“enterprise_name”)。toString():“”; String month = parMap.get(“month”)!= null?parMap.get(“month”)。toString():“”; String day = parMap.get(“day”)!= null?parMap.get(“day”)。toString():“”; String first_time = parMap.get(“first_time”)!= null?parMap.get(“first_time”)。toString():“”; StringBuffer html = new StringBuffer(); html.append(“<div id ='model4'align ='center'>”); html.append(“<table border ='0'style ='width:80%; 高度:80%“>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“< html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”+ year); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“</ TABLE>”); html.append(“</ DIV>”); return html.toString(); UnsupportedEncodingException * / 公共静态字符串getHtml5(地图<字符串,对象> parMap)抛出UnsupportedEncodingException { !?字符串年= parMap.get(“年”)= NULL 。parMap.get(“年”)的toString():“” ; String enterprise_name = parMap.get(“enterprise_name”)!= null?parMap.get(“enterprise_name”)。toString():“”; String month = parMap.get(“month”)!= null?parMap.get(“month”)。toString():“”; String day = parMap.get(“day”)!= null?parMap.get(“day”)。toString():“”; String first_time = parMap.get(“first_time”)!= null?parMap.get(“first_time”)。toString():“”; StringBuffer html = new StringBuffer(); html.append(“<div id ='model5' html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<td width ='70%'>”); html.append(“</ TD>”); html.append(“<TD>”); html.append(“&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;”+ year); html.append(“</ TD>”); html.append(“</ TR>”); html.append(“<TR>”); html.append(“<TD>”); html.append(“</ TD>”); html.append(“<TD> </ TD>”); html.append(“</ TR>”); html.append(“</ TABLE>”); html.append(“</ DIV>”); return html.toString(); BASEFONT bfChinese = NULL; 尝试{ bfChinese = BaseFont.createFont(“STSong-Light”,“UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED); catch(例外e){ e.printStackTrace(); } Font FontChinese = new Font(bfChinese,12,Font.NORMAL); 返回FontChinese; } @覆盖公共布尔isRegistered(字符串为arg0){ 返回false; } } / ** *合并PDF文件* * @参数文件* @参数savepath * @throws异常* / 公共静态字符串mergePdfFiles(列表<字符串>文件,字符串savepath)抛出异常{ 文献文档= NULL; FileOutputStream outfile = null; PdfReader reader = null;尝试{ document = new Document(new PdfReader(files.get(0))。getPageSize(1)); outfile = new FileOutputStream(savepath); PdfCopy copy = new PdfCopy(document,outfile); document.open(); for(int i = 0; 我<files.size(); i ++){ reader = new PdfReader(files.get(i)); int n = reader.getNumberOfPages(); for(int j = 1; j <= n; j ++){ document.newPage(); PdfImportedPage page = copy.getImportedPage(reader,j); copy.addPage(页); } reader.close(); } } catch(Exception e){ e.printStackTrace(); 抛出新的例外(“merge -----生成pdf异常”,e); } finally { document.close(); try { outfile.close(); } catch(IOException e){ e.printStackTrace(); } reader.close(); } return savepath; } / ** *获取当前字符串的字符编码* * @param str * @return * / public static String getEncoding(String str){ String encode =“GB2312”; try { if(str.equals(new String(str.getBytes(encode),encode))){ String s = encode; 回归; }上上(例外的例外){ } 编码=“ISO-8859-1”; try { if(str.equals(new String(str.getBytes(encode),encode))){ String s1 = encode; 返回s1; }上上(例外exception1){ } encode =“UTF-8”; try { if(str.equals(new String(str.getBytes(encode),encode))){ String s2 = encode; 返回s2; }上上(例外exception2){ } 编码=“GBK”; try { if(str.equals(new String(str.getBytes(encode),encode))){ String s3 = encode; 返回s3; } } catch(Exception exception3){ } return“”; } / ** *删除文件* * @param pathname * @return * @throws IOException * / public static boolean deleteFile(String pathname){ boolean result = false; 文件文件=新文件(路径名); 如果(file.exists()){ result = file.delete(); if(result){ System.out.println(“文件已经被删除成功!!”); } else { System.out.println(“文件已经被删除失败!!”); } } 返回结果; } / ** *强制删除文件* * @param文件* @return * / public static boolean forceDelete(String filePath){ File file = new File(filePath); boolean result = file.delete(); int tryCount = 0; while(!result && tryCount ++ <2){ System.gc(); //回收资源result = file.delete(); // System.out.println(“删除”+ tryCount +“次!”); } 返回结果; * * @param文件* @return * / public static byte [] File2byte(文件文件){ byte [] buffer = null; 尝试{ FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte [] b = new byte [1024]; int n; while((n = fis.read(b))!= -1){ bos.write(b,0,n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch(FileNotFoundException e){ e.printStackTrace(); } catch(IOException e){ e.printStackTrace(); } 返回缓冲区; } / ** *生成多页pdf * @throws Exception * / public static String createPrintPdfFile(String path,List <Map <String,String >> paramMapList,String modeltype)throws Exception { //生成空白pdf 文档document = new Document(PageSize.A4.rotate(),0,0,0, 0); String filepath = path + UUID.randomUUID()+“。pdf”; FileOutputStream fileoutput = new FileOutputStream(filepath); PdfWriter.getInstance(文件,fileoutput); document.open(); for(int i = 0; 我<paramMapList.size(); i ++){ document.newPage(); //不能为空,否则无法添加页面document.add(新段落(“”)); } document.close(); fileoutput.close(); // 在pdf中添加文字String outputPath = path + UUID.randomUUID()+“。pdf”; 如果(“1”。 addWatermark(路径,文件路径,outputPath,paramMapList); } else { addWatermark2_5(path,filepath,outputPath,paramMapList,modeltype); } deleteFile(filepath); return outputPath; } / ** *在PDF上添加文字* @参数路径* @参数inputPath * @参数outputPath * @参数paramMapList * @throws 异常* / 私有静态无效addWatermark(字符串路径,字符串inputPath,字符串outputPath,列表<地图< String,String >> paramMapList)抛出异常{ PdfReader pdfReader = new PdfReader(inputPath); FileOutputStream fileoutputStream = new FileOutputStream(outputPath); PdfStamper pdfStamper = new PdfStamper(pdfReader,fileoutputStream); PdfContentByte content_company_name = null; PdfContentByte content_certificate_number = null; PdfContentByte content_month = null; PdfContentByte content_day = null; PdfContentByte content_continue_year = null; PdfContentByte content_year = null; PdfContentByte content_certificate_number_wenzi = null; PdfContentByte content_yeardu = null; PdfContentByte content_first_time = null; BaseFont basefont_kaiti = null; BaseFont basefont_songti = null; PdfGState pdfGstate = new PdfGState(); basefont_kaiti = BaseFont.createFont(path +“font / kaiti.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); basefont_songti = BaseFont.createFont(path +“font / songti.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); // BaseFont bfChinese = BaseFont.createFont(“STSong-Light”,“UniGB-UCS2-H”,BaseFont.NOT_EMBEDDED); 尝试{ if(basefont_songti == null || basefont_kaiti == null){ throw new Exception(“生成打印pdf时,字体加载异常!!”); } //设置透明度为1.0不透明pdfGstate.setFillOpacity(1.0f); int toPage = pdfStamper.getReader()。getNumberOfPages(); for(int i = 1; 我<= toPage; i ++){ Map <String,String> paremMap = paramMapList.get(i-1); //遍历,检查参数是否有空或null,有则抛出异常Iterator <Map.Entry <String,String >> entries = paremMap.entrySet()。iterator(); int count = 0; count ++; Map.Entry <String,String> entry = entries.next(); // System.out.println(“Key =”+ entry.getKey()+“,Value =”+ entry.getValue()); if(StringUtils.isBlank(entry.getValue())){ throw new Exception(“生成打印pdf时,解析传入参数时,发生异常,原因:参数”+ entry.getKey()+“为空值或者null ,请检查!!“); } } 如果(计数<8){ 抛出新的异常(“生成打印PDF格式时,解析传入参数时,发生异常,原因:缺少参数,请检查!!”); / enterprise解析传入的参数String enterprise_name = paremMap.get(“enterprise_name”); String certificate_number = paremMap.get(“certificate_number”); String year = paremMap.get(“year”); String month = paremMap.get(“month”); String first_time = paremMap.get(“first_time”); String year_nd = paremMap.get(“year_nd”); String continue_year = paremMap.get(“continue_year”); //设置整体调整参数int yyy = 10 + 10 + 8; int xxx = -30 + 11 + 6; / * 参数说明:以纸张的左下角为坐标原点,纸张左边框为正Y轴,纸张底部为正X轴,把输出文字看作一个方块(具有左 - 右 - 上 - 下边框)xxx参数代表文字的左边框到Y轴的像素yyy参数代表文字的下边框到X轴的像素 * / //企业名称content_company_name = pdfStamper.getOverContent(i); content_company_name.saveState(); content_company_name.setGState(pdfGstate); content_company_name.beginText(); content_company_name.setColorFill(BaseColor.BLACK); content_company_name.setFontAndSize(basefont_kaiti,23); // content_company_name.setFontAndSize(FontKaitiJiaCu,20); // size()横坐标位置// 20 x-270 // 23 x-280 content_company_name.showTextAligned(Element.ALIGN_CENTER,enterprise_name,295 + xxx,375 + yyy,0); content_company_name.endText(); //证书编号前的文字content_certificate_number_wenzi = pdfStamper.getOverContent(i); content_certificate_number_wenzi.saveState(); content_certificate_number_wenzi.setGState(pdfGstate); content_certificate_number_wenzi.beginText(); content_certificate_number_wenzi.setColorFill(BaseColor.BLACK); content_certificate_number_wenzi.setFontAndSize(basefont_kaiti,14); content_certificate_number_wenzi.showTextAligned(Element.ALIGN_CENTER,“证书编号:”,300 + xxx,420 + yyy,0); content_certificate_number_wenzi.endText(); //证书编号content_certificate_number = pdfStamper.getOverContent(i); content_certificate_number.saveState(); content_certificate_number.setGState(pdfGstate); content_certificate_number.beginText(); content_certificate_number.setColorFill(BaseColor.BLACK); content_certificate_number.setFontAndSize(basefont_kaiti,14); content_certificate_number.showTextAligned(Element.ALIGN_CENTER,certificate_number,380 + xxx,418 + yyy,0); content_certificate_number.endText(); //整体上移年月日int date_y = 4; //日期 - 年 content_year = pdfStamper.getOverContent(i); content_year.saveState(); content_year.setGState(pdfGstate); content_year.beginText(); content_year.setColorFill(BaseColor.BLACK); content_year.setFontAndSize(basefont_kaiti,14); content_year.showTextAligned(Element.ALIGN_CENTER,year,215 + xxx,112 + yyy + date_y,0); content_year.endText(); //日期 - 月content_month = pdfStamper.getOverContent(i); content_month.saveState(); content_month.setGState(pdfGstate); content_month.beginText(); content_month.setColorFill(BaseColor.BLACK); content_month.setFontAndSize(basefont_kaiti,14); content_month.showTextAligned(Element.ALIGN_CENTER,month,280 + xxx,112 + yyy + date_y,0); content_month.endText(); //日期 - 天content_day = pdfStamper.getOverContent(i); content_day.saveState(); content_day.setGState(pdfGstate); content_day.beginText(); content_day.setColorFill(BaseColor.BLACK); content_day.setFontAndSize(basefont_kaiti,14); content_day.showTextAligned(Element.ALIGN_CENTER,day,335 + xxx,112 + yyy + date_y,0); content_day.endText(); //首次公示年度content_first_time = pdfStamper.getOverContent(i); content_first_time.saveState(); content_first_time.setGState(pdfGstate); content_first_time.beginText(); content_first_time.setColorFill(BaseColor.BLACK); content_first_time.setFontAndSize(basefont_kaiti,16); content_first_time.showTextAligned(Element.ALIGN_CENTER,first_time,220 + 6 + xxx,72-8 + yyy,0); content_first_time.endText(); //公示年度content_yeardu = pdfStamper.getOverContent(i); content_yeardu.saveState(); content_yeardu.setGState(pdfGstate); content_yeardu.beginText(); content_yeardu.setColorFill(BaseColor.BLACK); content_yeardu.setFontAndSize(basefont_kaiti,16); content_yeardu.showTextAligned(Element.ALIGN_CENTER,year_nd,510 + xxx,418-8 + yyy,0); content_yeardu.endText(); //连续年数content_continue_year = pdfStamper.getOverContent(i); content_continue_year.saveState(); content_continue_year.setGState(pdfGstate); content_continue_year.beginText(); content_continue_year.setColorFill(BaseColor.BLACK); content_continue_year.setFontAndSize(basefont_kaiti,18); content_continue_year.showTextAligned(Element.ALIGN_CENTER,“连续”+ continue_year +“年”,650 + xxx,418-8 + yyy,0); content_continue_year.endText(); } }上的(例外){ ex.printStackTrace(); 抛出新的例外(“生成打印pdf时,添加文字异常,请检查!!”,ex); } finally { content_company_name = null; content_certificate_number = null; content_day = null; content_year = null; content_first_time = null; content_certificate_number_wenzi = null; content_continue_year = null; content_yeardu = null; basefont_kaiti = null; pdfStamper.close(); pdfReader.close(); fileoutputStream.close(); } } / ** *在PDF上添加文字* @参数路径* @参数inputPath * @参数outputPath * @参数paramMapList *抛出:异常* / 私有静态无效addWatermark2_5(字符串路径,字符串inputPath,字符串outputPath,列表<地图<String,String >> paramMapList,String modeltype)抛出异常{ PdfReader pdfReader = new PdfReader(inputPath); FileOutputStream fileoutputStream = new FileOutputStream(outputPath); PdfStamper pdfStamper = new PdfStamper(pdfReader,fileoutputStream); PdfContentByte content_yeardu = null; PdfContentByte content_continue_year = null; BaseFont basefont_kaiti = null; BaseFont basefont_songti = null; PdfGState pdfGstate = new PdfGState(); basefont_kaiti = BaseFont.createFont(path +“font / kaiti.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); basefont_songti = BaseFont.createFont(path +“font / songti.TTF”,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); 尝试{ if(basefont_songti == null || basefont_kaiti == null){ throw new Exception(“生成打印pdf时,字体加载异常!!”); } //设置透明度为1.0不透明pdfGstate.setFillOpacity(1.0f); int toPage = pdfStamper.getReader()。getNumberOfPages(); for(int i = 1; 我<= toPage; i ++){ Map <String,String> paremMap = paramMapList.get(i-1); if(StringUtils.isBlank(paremMap.get(“year_nd”))){ throw new Exception(“生成打印pdf时,解析传入参数时,发生异常,原因:参数year_nd为空值或者null,请检查!! “); } if(StringUtils.isBlank(paremMap.get(“continue_year”))){ throw new Exception(“生成打印pdf时,解析传入参数时,发生异常,原因:参数continue_year为空值或者null,请检查! !“); } //解析传入的参数String year_nd = paremMap.get(“year_nd”); String continue_year = paremMap.get(“continue_year”); content_yeardu = pdfStamper.getOverContent(i); content_yeardu.saveState(); content_yeardu.setGState(pdfGstate); content_yeardu.beginText(); content_yeardu。 content_yeardu.setFontAndSize(basefont_kaiti,16); // // int yyy = 10 + 10 + 8; // int xxx = -30 + 11 + 6; // // int year_nd_x = 484 + 11 + 12 - 12; int continue_year_x = 624 + 11 + 12 - 12; int year_nd_y_And_continue_year_y = 0 + 10 + 17 + 17; //整体上下移动 if(“2”.equals(modeltype)){ content_yeardu.showTextAligned(Element.ALIGN_CENTER,year_nd,year_nd_x,355 + year_nd_y_And_continue_year_y,0); } else if(“3”.equals(modeltype)){ content_yeardu.showTextAligned(Element.ALIGN_CENTER,year_nd,year_nd_x,290 + year_nd_y_And_continue_year_y,0); } else if(“4”.equals(modeltype)){ content_yeardu.showTextAligned(Element.ALIGN_CENTER,year_nd,year_nd_x,225 + year_nd_y_And_continue_year_y,0); } else if(“5”.equals(modeltype)){ content_yeardu.showTextAligned(Element.ALIGN_CENTER,year_nd,year_nd_x,160 + year_nd_y_And_continue_year_y,0); } content_yeardu.endText(); content_continue_year = pdfStamper.getOverContent(i); content_continue_year.saveState(); content_continue_year.setGState(pdfGstate); content_continue_year.beginText(); content_continue_year.setColorFill(BaseColor.BLACK); content_continue_year.setFontAndSize(basefont_kaiti,18); 如果(“2” .equals(modeltype)){ content_continue_year.showTextAligned(Element.ALIGN_CENTER,“连续”+ continue_year +“年”,continue_year_x,355 + year_nd_y_And_continue_year_y,0); } else if(“3”.equals(modeltype)){ content_continue_year.showTextAligned(Element.ALIGN_CENTER,“连续”+ continue_year +“年”,continue_year_x,290 + year_nd_y_And_continue_year_y,0); } else if(“4”.equals(modeltype)){ content_continue_year.showTextAligned(Element.ALIGN_CENTER,“连续”+ continue_year +“年”,continue_year_x,225 + year_nd_y_And_continue_year_y,0); } else if(“5”.equals(modeltype)){ content_continue_year.showTextAligned(Element.ALIGN_CENTER,“连续”+ continue_year +“年”,continue_year_x,160 + year_nd_y_And_continue_year_y,0); } content_continue_year.endText(); } } catch(Exception ex){ ex.printStackTrace(); 抛出新的例外(“生成打印pdf时,添加文字异常,请检查!!”,ex); } finally { content_yeardu = null; content_continue_year = null; basefont_kaiti = null; pdfStamper.close(); pdfReader.close(); fileoutputStream.close(); } } / ** *生成单个PDF文件* * @参数路径* @参数parMap * @参数moType * @返回* @throws异常* / 公共字符串createSinglePdf(字符串路径,地图<字符串,对象> parMap,字符串modeltype)抛出异常{ String pathStr =“”; FileOutputStream outfile = null; 文件文件= null; PdfWriter mPdfWriter = null; try { // path =“C:// aa /”; BaseFont bfChinese = BaseFont.createFont(“STSongStd-Light”,“UniGB-UCS2-H”,false); Font font = new Font(bfChinese,12,Font.NORMAL); //设置字体大小//新文档(pageSize,marginLeft,marginRight,marginTop,marginBottom)document = new Document(PageSize.A4.rotate(),60,60,60,50 ); String filename = String.valueOf(new Date()。getTime()); pathStr = path + filename +“。pdf”; outfile = new FileOutputStream(pathStr); mPdfWriter = PdfWriter.getInstance(document,outfile); document.open(); // document.add(new Paragraph(“创建pdf文件。支持中文......”,字体)); document.add(new Paragraph(“”,font)); if(“1”.equals(modeltype)){ htmlStr = getHtml1(parMap); // htmlStr = getHtml22222(); } else if(“2”.equals(modeltype)){ htmlStr = getHtml2(parMap); } else if(“3”.equals(modeltype)){ htmlStr = getHtml3(parMap); } else if(“4”.equals(modeltype)){ htmlStr = getHtml4(parMap); } else if(“5”.equals(modeltype)){ htmlStr = getHtml5(parMap); } ByteArrayInputStream进行的仓=新ByteArrayInputStream的进行(htmlStr.getBytes(“UTF-8”)); XMLWorkerHelper.getInstance()。parseXHtml(mPdfWriter,document,bin,Charset.forName(“UTF-8”),new ChinaFontProvide()); catch(例外e){ e.printStackTrace(); 抛出新的例外(“ document.close(); mPdfWriter.close(); try { outfile.close(); } catch(IOException e){ e.printStackTrace(); } } 返回pathStr; } //读取请求传递过来的JSON格式数据,返回JSON字符串public static String readJSONData(HttpServletRequest request){ StringBuffer json = new StringBuffer(); String lineString = null; try { BufferedReader reader = request.getReader(); while((lineString = reader.readLine())!= null){ json.append(lineString); }上上 (例外五){ 的System.out.println(e.toString()); } return json.toString(); } } |