昨天花了很长的时间去找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:\\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);
}
}
}
3.注意事项:simhei.ttf 这是字体,对中文有效的;然后如果导了com.lowagie包可能在引包的时候会出现问题,所以看我代码上的import导的什么包就行了。最后,记得点赞,需要大家的鼓励,谢谢。