您好,登錄后才能下訂單哦!
怎么在java項(xiàng)目中生成一個(gè)pdf表格?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
1.第一步 導(dǎo)包
<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>
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:\\pdf\\simhei.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); } } }
simhei.ttf 這是字體,對(duì)中文有效的;然后如果導(dǎo)了com.lowagie包可能在引包的時(shí)候會(huì)出現(xiàn)問題,所以看我代碼上的import導(dǎo)的什么包就行了。
補(bǔ)充:java生成PDF表格的一次優(yōu)化
在優(yōu)化一個(gè)pdf的發(fā)票打印的時(shí)候如果發(fā)票的發(fā)票明細(xì)超過1000行的時(shí)候就會(huì)變得很慢.需要20分鐘才能把數(shù)據(jù)加載出來.之后就開始查詢耗時(shí)的原因,在打印了每個(gè)方法的執(zhí)行時(shí)間之后,發(fā)現(xiàn)在打印方法執(zhí)行的時(shí)候sql取數(shù)據(jù)的時(shí)候很快,那么就是itext的轉(zhuǎn)換PDF的時(shí)候?qū)е潞苈?
最后找到原因是因?yàn)榘l(fā)票明細(xì)行中的行合并導(dǎo)致效率低下,比如一個(gè)合同下有1000條明細(xì)數(shù)據(jù),那么合同名稱這一列就需要合同1000行,這個(gè)合并會(huì)導(dǎo)致打印效率低下(cell.setColspan(colspan);)方法;
因?yàn)槊總€(gè)發(fā)票只有一個(gè)合同我們可以把整個(gè)明細(xì)行看成一個(gè)整體.(思路如下圖)
這樣在調(diào)一下樣式就可以了
float[] widths = {110, 110, 330}; PdfPTable contractTable = new PdfPTable(widths);//這個(gè)表格三列的長度 contractTable.setTotalWidth(width);//這個(gè)屬性要加上 contractTable.setLockedWidth(true);//這個(gè)屬性要加上 cellDetail.setPadding(0f); cellDetail.setBorderWidth(0f);//去除表格的邊框
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。