利用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格式下可自定义一行有几个单元格,可合并、可跨行、可跨列,还可以插入文本水印及图片,感兴趣的可自己尝试。
阅读全文
0 0
- 利用Itext中table动态生成PDF模板
- 利用velocity模板以及itext生成pdf
- 在Android中利用iText生成PDF
- itext pdf模板生成pdf
- 利用iText生成PDF文档
- 利用iText生成PDF报表
- 利用iText生成pdf报表
- 利用itext生成pdf文档
- Itext操作pdf模板生成pdf
- 如何利用iText在JSP中生成PDF报表- -
- 利用iText在JSP中生成PDF报表
- 如何利用iText在JSP中生成PDF报表
- 利用iText在JSP中生成PDF报表
- 利用iText在JSP中生成PDF报表
- 利用iText在JSP中生成PDF报表
- 利用iText在JSP中生成PDF报表
- 如何利用iText在JSP中生成PDF报表
- 利用iText在JSP中生成PDF报表
- 维护采购订单审批的特性Characteristic和类Class
- mvc中大类和小类二级联动下拉框使用 select2 插件的初始加载方法
- HttpClient 4.3详细教程之Http状态管理
- 第一章 初识hadoop
- plx驱动升级
- 利用Itext中table动态生成PDF模板
- .a和.framework区别
- libevent
- Linux系统IO分析工具之iotop参数详解(查看IO占用)
- jQuery 属性方法 总结
- 判断单张,多张图片加载完成
- 如何理解并正确使用 MySQL 索引
- Android安卓ECLIPSE导致工程到AndroidStudio里 报错 Failed to open zip file 解决办法
- deepin系统配置swap分区