itextpdf添加表格元素_java使用iText生成pdf表格

packagepoi.zr.com.pojo;importjava.awt.Color;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importcom.lowagie.text.BadElementException;importcom.lowagie.text.Cell;importcom.lowagie.text.Document;importcom.lowagie.text.DocumentException;importcom.lowagie.text.Element;importcom.lowagie.text.Font;importcom.lowagie.text.PageSize;importcom.lowagie.text.Paragraph;importcom.lowagie.text.Phrase;importcom.lowagie.text.Table;importcom.lowagie.text.pdf.BaseFont;importcom.lowagie.text.pdf.PdfWriter;public classPOI {public static void main(String[] args) throwsDocumentException, IOException {//创建Document对象(页面的大小为A4,左、右、上、下的页边距为10)

Document document = new Document(PageSize.A4, 10, 10, 10, 10);//建立书写器

PdfWriter.getInstance(document, new FileOutputStream("/Users/apple/Desktop/poi.PDF"));//设置相关的参数

POI.setParameters(document, "开发者测试", "测试", "测试 开发者 调试", "甘雨路", "甘雨路");//打开文档

document.open();//使用iTextAsian.jar中的字体

BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

Font font= newFont(baseFont);

List users = new ArrayList();//循环添加对象

for (int i = 0; i < 5; i++) {

User user= newUser();

user.setId(""+i);

user.setInfo("开发者测试"+i);

user.setName("测试"+i);

users.add(user);

}

Table table=POI.setTable(users);

document.add(new Paragraph("用户信息如下:",POI.setFont()));

document.add(table);//关闭文档

document.close();

}public static Table setTable(List users) throwsBadElementException{//创建一个有3列的表格

Table table = new Table(3);

table.setBorderWidth(1);

table.setBorderColor(new Color(0, 0, 255));

table.setPadding(5);

table.setSpacing(5);//创建表头

Cell cell1 = POI.setTableHeader("编号ID");

Cell cell2= POI.setTableHeader("基本信息");

Cell cell3= POI.setTableHeader("姓名");

table.addCell(cell1);

table.addCell(cell2);

table.addCell(cell3);//添加此代码后每页都会显示表头

table.endHeaders();for (int i = 0; i < users.size(); i++) {

Cell celli1=POI.setTableHeader(users.get(i).getId());

Cell celli2=POI.setTableHeader(users.get(i).getInfo());

Cell celli3=POI.setTableHeader(users.get(i).getName());

table.addCell(celli1);

table.addCell(celli2);

table.addCell(celli3);

}returntable;

}/*** 设置字体编码格式

*@return

*/

public staticFont setFont(){

BaseFont baseFont= null;try{

baseFont= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

}catch(DocumentException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}

Font font= new Font(baseFont, 8, Font.NORMAL,Color.BLUE);returnfont;

}/*** 设置cell

*@paramname

*@return*@throwsBadElementException*/

public static Cell setTableHeader(String name) throwsBadElementException{

Cell cell= new Cell(newPhrase(name,POI.setFont()));//单元格水平对齐方式

cell.setHorizontalAlignment(Element.ALIGN_CENTER);//单元格垂直对齐方式

cell.setVerticalAlignment(Element.ALIGN_CENTER);//cell.setHeader(true);

cell.setBackgroundColor(Color.RED);returncell;

}/*** 设置相关参数

*@paramdocument

*@return

*/

public staticDocument setParameters(Document document,String title,String subject,String keywords,String author,

String creator){//设置标题

document.addTitle(title);//设置主题

document.addSubject(subject);//设置作者

document.addKeywords(keywords);//设置作者

document.addAuthor(author);//设置创建者

document.addCreator(creator);//设置生产者

document.addProducer();//设置创建日期

document.addCreationDate();returndocument;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值