最近用到了itextpdf,所以就看了一下itextpdf 的使用方法,本人就记录了关于itextpdf把一个集合放入到表格中的操作步骤
一、pom.xml文件,itextpdf的相关依赖
<!--iTextpdf 相关依赖 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
二、测试页面,Testpdf2和所用到的实体类
package com.util;
public class User {
private String id;
private String info;
private String name;
public User() {
}
public User(String id, String info, String name) {
this.id = id;
this.info = info;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class Testpdf2 {
public static void main(String[] args) {
Document document = new Document();
try {
//文件路径
PdfWriter.getInstance(document, new FileOutputStream("d:\\TableCellBorder.pdf"));
document.open();
// 使用iTextAsian.jar中的字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont);
List<User> users = new ArrayList<>();
for (int i = 0; i < 100; i++) {
User u = new User();
u.setId("" + i);
u.setName("张三" + i);
u.setInfo("信息" + i);
users.add(u);
}
//实例化表格
PdfPTable table = new PdfPTable(3);
//格子(用来装表头的数据)
PdfPCell[] cells=new PdfPCell[3];
//用来装userid
PdfPCell cell1;
//用来装name
PdfPCell cell2;
//用来装info
PdfPCell cell3;
//表头
// 集合形式
List<String> li=new ArrayList<>();
li.add("序号");
li.add("名字");
li.add("信息");
//数据放入表头
for (int i=0;i<li.size();i++){
cells[i]=new PdfPCell(new Phrase(li.get(i),font));
cells[i].setUseAscender(true);
cells[i].setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
cells[i].setHorizontalAlignment(Element.ALIGN_CENTER);// 水平居中
cells[i].setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cells[i]);
}
//装主体内容
Phrase p;
Phrase p2;
Phrase p3;
for (int k=0;k<users.size();k++){
//实例化
p=new Phrase(users.get(k).getId(),font);
p2=new Phrase(users.get(k).getName(),font);
p3=new Phrase(users.get(k).getInfo(),font);
//把内容放进格子
cell1 = new PdfPCell(p);
cell2 = new PdfPCell(p2);
cell3 = new PdfPCell(p3);
cell1.setUseAscender(true);
cell2.setUseAscender(true);
cell3.setUseAscender(true);
cell1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);//格子居中
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);// 内容水平居中
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);//内容垂直居中
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);// 内容水平居中
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);// 内容水平居中
//把格子放入表格中
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
}
//组成一行
table.completeRow();
//开启文档分页
table.setSplitLate(false);
table.setSplitRows(true);
//把表格添加进文档
document.add(table);
} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
}
}
}
三、效果展示图,,实现了文字居中和文档分页
1、文字居中效果
2、文档分页效果