利用Itext中table动态生成PDF模板

来源:互联网 发布:医学答案软件 编辑:程序博客网 时间:2024/06/16 05:46

之前项目中一直采用的PDF固定模板,在模板中填充值的方式在线生成PDF供用户下载,这种方法在使用过程中有一弊端,就是当填充数据较少但模板的文本框过大时将导致留白过多,影响美观,故上网查阅相关资料,采用Itext中table的格式可有效解决这一问题,部分代码如下,希望可以帮到跟我遇到同样问题的你。

web项目前端采用的Extjs后台采用的是Java,前端数据传到后台,调用toPDF()方法:

public String toPDF() {String rootPath  = ServletActionContext.getServletContext().getRealPath("/");try {   ViewKtZds[] zdsArr = new ViewKtZds[(datas.size())];   for(int i=0; i < datas.size(); i++){   JSONObject zdsJson = JSONObject.fromObject(datas.get(i));   ViewKtZds zds = (ViewKtZds) JSONObject.toBean(zdsJson,ViewKtZds.class);   zdsArr[i] = zds;   }String url = this.createPDF(rootPath,zdsArr);    this.setSuccess(true);       this.setMsg(url);} catch (SecurityException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}return "pdf";}

其中rootPath是目录,zdsArr是前台传来的数据。
//pdf动态生成table方法public String createPDF(String rootPath, ViewKtZds[] zdsArr) throws DocumentException, IOException{   //设置字体格式   BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//   com.itextpdf.text.Font FontChinese24 = new com.itextpdf.text.Font(bfChinese,24,com.itextpdf.text.Font.BOLD);   com.itextpdf.text.Font FontChinese18 = new com.itextpdf.text.Font(bfChinese,18,com.itextpdf.text.Font.BOLD);//   com.itextpdf.text.Font FontChinese16= new com.itextpdf.text.Font(bfChinese,16,com.itextpdf.text.Font.BOLD);   com.itextpdf.text.Font FontChinese12Bold = new com.itextpdf.text.Font(bfChinese,12,com.itextpdf.text.Font.BOLD);   com.itextpdf.text.Font FontChinese11Bold = new com.itextpdf.text.Font(bfChinese,11,com.itextpdf.text.Font.BOLD);//   com.itextpdf.text.Font FontChinese11 = new com.itextpdf.text.Font(bfChinese,11,com.itextpdf.text.Font.ITALIC);//   com.itextpdf.text.Font FontChinese11Normal = new com.itextpdf.text.Font(bfChinese,11,com.itextpdf.text.Font.NORMAL);//   com.itextpdf.text.Font FontChinese10Bold = new com.itextpdf.text.Font(bfChinese,10,com.itextpdf.text.Font.BOLD);//   com.itextpdf.text.Font FontChinese9Bold = new com.itextpdf.text.Font(bfChinese,9,com.itextpdf.text.Font.BOLD);      //数据获取   StringBuffer sb = new StringBuffer();   sb.append(rootPath + "/"+"temps/");   sb.append("zds/");   sb.append("ZdsTemp.pdf");   String resultPath=sb.substring(0,sb.lastIndexOf("/"));   File ft = new File(resultPath);   if (!ft.exists()) {ft.mkdirs();   }      String titles = "";   for(int  j = 0; j < zdsArr.length; j++){   FileOutputStream fileOutputStream = null; PdfWriter write = null;       String title =zdsArr[j].getPch() + "_" +zdsArr[j].getXh() + ".pdf";   fileOutputStream = new FileOutputStream(resultPath+"/"+title );    Document document  =   new  Document(PageSize.A4,  20 ,  20 ,  40 ,  40 );//左右上下   write = PdfWriter.getInstance(document, fileOutputStream);     document.open();      Paragraph zds_tm = new Paragraph("这里是题目!!",FontChinese18);   zds_tm.setAlignment(Element.ALIGN_CENTER);//   zds_tm.setLeading(0,1);   document.add(zds_tm);      Paragraph zds_kg1 = new Paragraph("                                    ",FontChinese18);   zds_kg1.setAlignment(Element.ALIGN_CENTER);   document.add(zds_kg1);   //   Paragraph zds_kg2 = new Paragraph("                                    ",FontChinese18);//   zds_kg2.setAlignment(Element.ALIGN_CENTER);//   document.add(zds_kg2);      //table第一行
   //动态填充数据   PdfPTable zdsPt01= new PdfPTable(3);   int widthzdspt01[] = {105,80,85};   zdsPt01.setWidths(widthzdspt01);   PdfPCell cell01 = new PdfPCell(new Paragraph("学    号: "+zdsArr[j].getXh(),FontChinese12Bold));     PdfPCell cell02 = new PdfPCell(new Paragraph("姓    名: "+zdsArr[j].getXm(),FontChinese12Bold));     PdfPCell cell03 = new PdfPCell(new Paragraph("指导老师: "+zdsArr[j].getTjr(),FontChinese12Bold));      //垂直居中  //   cell01.setHorizontalAlignment(Element.ALIGN_CENTER); 水平居中             cell01.setVerticalAlignment(Element.ALIGN_MIDDLE);             cell02.setVerticalAlignment(Element.ALIGN_MIDDLE);             cell03.setVerticalAlignment(Element.ALIGN_MIDDLE);                       //表格高度           cell01.setFixedHeight(25);           cell02.setFixedHeight(25);           cell03.setFixedHeight(25);           //添加单元格           zdsPt01.addCell(cell01);             zdsPt01.addCell(cell02);             zdsPt01.addCell(cell03);           document.add(zdsPt01);                      //table第二行   PdfPTable zdsPt02= new PdfPTable(3);   int widthzdspt02[] = {105,80,85};   zdsPt02.setWidths(widthzdspt02);   PdfPCell cell04 = new PdfPCell(new Paragraph("学    院: "+zdsArr[j].getXy(),FontChinese12Bold));     PdfPCell cell05 = new PdfPCell(new Paragraph("专    业: "+zdsArr[j].getZymc(),FontChinese12Bold));     PdfPCell cell06 = new PdfPCell(new Paragraph("班          级: "+zdsArr[j].getBjmc(),FontChinese12Bold));      //垂直居中             cell04.setVerticalAlignment(Element.ALIGN_MIDDLE);             cell05.setVerticalAlignment(Element.ALIGN_MIDDLE);             cell06.setVerticalAlignment(Element.ALIGN_MIDDLE);                        //表格高度           cell04.setFixedHeight(25);           cell05.setFixedHeight(25);           cell06.setFixedHeight(25);                   //添加单元格           zdsPt02.addCell(cell04);             zdsPt02.addCell(cell05);             zdsPt02.addCell(cell06);           document.add(zdsPt02);                      //table第三行   PdfPTable zdsPt03= new PdfPTable(1);   int widthzdspt03[] = {180};   zdsPt03.setWidths(widthzdspt03);   PdfPCell cell07 = new PdfPCell(new Paragraph("课题题目: "+zdsArr[j].getKtmc(),FontChinese12Bold));        //垂直居中             cell07.setVerticalAlignment(Element.ALIGN_MIDDLE);             cell07.setUseBorderPadding(true);           cell07.setFixedHeight(23);//           cell07.disableBorderSide(8);               //添加单元格                    zdsPt03.addCell(cell07);                     document.add(zdsPt03);                         //第四行           PdfPTable zdsPt04= new PdfPTable(1);   int widthzdspt04[] = {270};   zdsPt04.setWidths(widthzdspt04);   PdfPCell cell08 = new PdfPCell(new Paragraph("目的意义:",FontChinese12Bold));        //垂直居中     cell08.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中           cell08.setFixedHeight(25);           //添加单元格                    zdsPt04.addCell(cell08);                     document.add(zdsPt04);                      //第五行           PdfPTable zdsPt05= new PdfPTable(1);   int widthzdspt05[] = {180};   zdsPt05.setWidths(widthzdspt05);  Paragraph mdyy = new Paragraph(zdsArr[j].getMdyy()+"\n\r",FontChinese11Bold);//  mdyy.setLeading(0,4);//      Paragraph   mdyy=new Paragraph(zdsArr[j].getMdyy()   PdfPCell cell09 = new PdfPCell(mdyy);  
//设置单元格行间距方法,注意是对cell,而不是对paragraph   cell09.setLeading(3f,1.2f);//   cell.setBorder(Rectangle.NO_BORDER);         设置单元格无边框   //垂直居中             cell09.setHorizontalAlignment(Element.ALIGN_MIDDLE);                 //添加单元格                    zdsPt05.addCell(cell09);                     document.add(zdsPt05);                      //table第六行   PdfPTable zdsPt06= new PdfPTable(1);   int widthzdspt06[] = {180};   zdsPt06.setWidths(widthzdspt06);   PdfPCell cell10 = new PdfPCell(new Paragraph("内容及意义:",FontChinese12Bold));   //垂直居中     cell10.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中           cell10.setFixedHeight(25);           //添加单元格                    zdsPt06.addCell(cell10);                     document.add(zdsPt06);                    //table第七行     PdfPTable zdsPt07= new PdfPTable(1);   int widthzdspt07[] = {180};   zdsPt07.setWidths(widthzdspt07);      PdfPCell cell11 = new PdfPCell(new Paragraph(zdsArr[j].getNryq()+"\n\r",FontChinese11Bold));     cell11.setLeading(3f, 1.2f); //垂直居中             cell11.setHorizontalAlignment(Element.ALIGN_MIDDLE);  //           cell07.disableBorderSide(8);            //添加单元格                    zdsPt07.addCell(cell11);                     document.add(zdsPt07);                                                               document.close();      titles += "temps/zds/" + title + ",";   }   return titles.substring(0,titles.length()-1);    }

在实现过程中发现单元格内设置行间距与段落中实现行间距不同,且文件的路径一定要统一,否则无法正常打印。table格式下可自定义一行有几个单元格,可合并、可跨行、可跨列,还可以插入文本水印及图片,感兴趣的可自己尝试。

原创粉丝点击