您的位置:首页技术文章
文章详情页

java生成pdf表格,调用itext创建的实例

【字号: 日期:2022-08-17 14:16:47浏览:17作者:猪猪

昨天花了很长的时间去找pdf生成表格的代码,发现网上大家写的代码太多了,而且又没有注释,让我一个小白是完全看不懂,这就很过分了,所以秉着我们代码界共享的原则,我要把我昨天的收获分享给大家,好了废话不多说,贴代码了。

1.第一步 导包

<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.3</version> </dependency>2.第二步看代码

import com.itextpdf.text.*;import com.itextpdf.text.pdf.*;import java.io.File;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List; public class PDFXXX { public static void main(String[] args) throws Exception, DocumentException { List<String> ponum = new ArrayList<String>(); add(ponum, 26); List<String> line = new ArrayList<String>(); add(line, 26); List<String> part = new ArrayList<String>(); add(part, 26); List<String> description = new ArrayList<String>(); add(description, 26); List<String> origin = new ArrayList<String>(); add(origin, 26); //Create Document Instance Document document = new Document(); //add Chinese font BaseFont bfChinese = BaseFont.createFont('d:pdfsimhei.ttf', BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //Font headfont=new Font(bfChinese,10,Font.BOLD); Font keyfont = new Font(bfChinese, 8, Font.BOLD); Font textfont = new Font(bfChinese, 8, Font.NORMAL); //Create Writer associated with document PdfWriter.getInstance(document, new FileOutputStream(new File('D:POReceiveReport.pdf'))); document.open(); //Seperate Page controller int recordPerPage = 10; int fullPageRequired = ponum.size() / recordPerPage; int remainPage = ponum.size() % recordPerPage > 1 ? 1 : 0; int totalPage = 1; for (int j = 0; j < totalPage; j++) { document.newPage(); String company = '等待'; //record header field PdfPTable t = new PdfPTable(5); float[] widths = {1.5f, 1f, 1f, 1.5f, 1f}; t.setWidths(widths); t.setTotalWidth(100); t.getDefaultCell().setBorder(PdfPCell.NO_BORDER); PdfPCell c1 = new PdfPCell(new Paragraph('PO#', keyfont)); t.addCell(c1); c1 = new PdfPCell(new Paragraph('Line', keyfont)); t.addCell(c1); c1 = new PdfPCell(new Paragraph('Part#', keyfont)); t.addCell(c1); c1 = new PdfPCell(new Paragraph('Description', keyfont)); t.addCell(c1); c1 = new PdfPCell(new Paragraph('Origin', keyfont)); t.addCell(c1); //calculate the real records within a page ,to calculate the last record number of every page int maxRecordInPage = j + 1 == totalPage ? (remainPage == 0 ? recordPerPage : (ponum.size() % recordPerPage)) : recordPerPage; for (int i = j * recordPerPage; i < ((j * recordPerPage) + maxRecordInPage); i++) { PdfPCell c2 = new PdfPCell(new Paragraph(ponum.get(i), textfont)); t.addCell(c2); c2 = new PdfPCell(new Paragraph(line.get(i), textfont)); t.addCell(c2); c2 = new PdfPCell(new Paragraph(part.get(i), textfont)); t.addCell(c2); c2 = new PdfPCell(new Paragraph(description.get(i), textfont)); t.addCell(c2); c2 = new PdfPCell(new Paragraph(origin.get(i), textfont)); t.addCell(c2); } document.add(t); } document.close(); } public static String leftPad(String str, int i) { int addSpaceNo = i - str.length(); String space = ''; for (int k = 0; k < addSpaceNo; k++) { space = ' ' + space; } ; String result = space + str; return result; } public static String printBlank(int tmp) { String space = ''; for (int m = 0; m < tmp; m++) { space = space + ' '; } return space; } public static void add(List<String> list, int num) { for (int i = 0; i < num; i++) { list.add('test老葛-' + i); } } }3.注意事项:

simhei.ttf 这是字体,对中文有效的;然后如果导了com.lowagie包可能在引包的时候会出现问题,所以看我代码上的import导的什么包就行了。

补充:java生成PDF表格的一次优化

在优化一个pdf的发票打印的时候如果发票的发票明细超过1000行的时候就会变得很慢.需要20分钟才能把数据加载出来.之后就开始查询耗时的原因,在打印了每个方法的执行时间之后,发现在打印方法执行的时候sql取数据的时候很快,那么就是itext的转换PDF的时候导致很慢.

java生成pdf表格,调用itext创建的实例

最后找到原因是因为发票明细行中的行合并导致效率低下,比如一个合同下有1000条明细数据,那么合同名称这一列就需要合同1000行,这个合并会导致打印效率低下(cell.setColspan(colspan);)方法;

优化思路:

因为每个发票只有一个合同我们可以把整个明细行看成一个整体.(思路如下图)

java生成pdf表格,调用itext创建的实例

这样在调一下样式就可以了

相关的方法:

float[] widths = {110, 110, 330};PdfPTable contractTable = new PdfPTable(widths);//这个表格三列的长度contractTable.setTotalWidth(width);//这个属性要加上contractTable.setLockedWidth(true);//这个属性要加上cellDetail.setPadding(0f);cellDetail.setBorderWidth(0f);//去除表格的边框

以上为个人经验,希望能给大家一个参考,也希望大家多多支持好吧啦网。如有错误或未考虑完全的地方,望不吝赐教。

标签: Java