使用iText生成PDF文档点滴

最近做项目用到了iText工具包生成PDF文档,就使用中的技巧做个留痕。
因为用到的最多的是关于在PDF中生成表格,所以只记录了一些表格的操作。


//List中放的是需要导出的信息对象,这个可以根据自己的情况不同
public File exportPDFforPayment(List appealList)
{

Document document = new Document(PageSize.A4,10,10,10,10); //设置生成的文档的大小,以及边距
float totalAmount = 0;
String filepath = "c:\\test.pdf";
try {
PdfWriter.getInstance(document, new FileOutputStream(filepath));
document.addSubject("Payment Infomation");//设置主题
document.addTitle("title");//设置标题
/*这些必须在文档打开前调用*/
document.open();
Table containtTable = new Table(1);//创建一个一列的表格
containtTable.setPadding(3);
containtTable.setWidth(100);
containtTable.setBorderWidth(0);
containtTable.setDefaultCellBorder(0);//设置如果某个cell没有填充值的时候边框的宽度

/*这里循环list,每个对象输出一行,并且实现隔行变色*/
for (int i=0;i {
Cell cell = new Cell();//创建一个单元格
if (i%2==0)
cell.addElement(this.makeInnerTable(false,appealList.get(i)));//为单元格增加元素,这里实现的是单元格中再套一个表格
else
cell.addElement(this.makeInnerTable(true,appealList.get(i)));
containtTable.addCell(cell);//表格加入该单元格
totalAmount+=Float.parseFloat(appealList.get(i).getAmount());
}

Cell cell = new Cell(new Chunk("Total Amount:$"+totalAmount, FontFactory.getFont("Verdana,Arial", 8,Font.UNDERLINE)));//设置单元格的元素,这次是个单元格中直接加入文字
cell.setBorderWidth(0);
cell.setHorizontalAlignment(Cell.ALIGN_RIGHT);//设置单元格对其方式
containtTable.addCell(cell);

document.add(containtTable);//文档加入表格
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return new File(filepath);
}

/*该方法实现嵌套的表格*/
private Table makeInnerTable(boolean isodd,Appeal appeal) throws BadElementException
{
Font titleFont = FontFactory.getFont("Verdana,Arial", 8,Font.BOLD);
Font contextFont = FontFactory.getFont("Verdana,Arial", 8);
Color oddColor = new Color(215,228,219);

Color finalColor = new Color(255,255,255);

if (isodd)
finalColor = oddColor;


float widths[] = {15f,13f,10f,14f,12f,13f,9f,15f};//设置表格的列宽
Table mytable = new Table(8);//一个8列的表格
mytable.setWidths(widths);//设置表格的宽度
mytable.setPadding(0);
mytable.setSpacing(0);
mytable.setWidth(97);
mytable.setBorderWidth(1);
mytable.setAutoFillEmptyCells(true);
mytable.setDefaultCellBorderWidth(0);
mytable.setBackgroundColor(finalColor);//设置背景色

Cell cell1_1 = new Cell(new Chunk("Payment Number:", titleFont));
cell1_1.setBorderWidth(0);
cell1_1.setBackgroundColor(finalColor);
mytable.addCell(cell1_1);

Cell cell1_2 = new Cell(new Chunk(appeal.getPaymentNum(), contextFont));
cell1_2.setBorderWidth(0);
cell1_2.setBackgroundColor(finalColor);
mytable.addCell(cell1_2);

Cell cell1_3 = new Cell(new Chunk("Full Name:", titleFont));
cell1_3.setBorderWidth(0);
cell1_3.setBackgroundColor(finalColor);
mytable.addCell(cell1_3);

Cell cell1_4 = new Cell(new Chunk(appeal.getFullName(), contextFont));
cell1_4.setBorderWidth(0);
cell1_4.setBackgroundColor(finalColor);
mytable.addCell(cell1_4);

Cell cell1_5 = new Cell(new Chunk("Street Address:", titleFont));
cell1_5.setBorderWidth(0);
cell1_5.setBackgroundColor(finalColor);
mytable.addCell(cell1_5);

Cell cell1_6 = new Cell(new Chunk(appeal.getApt()+" "+appeal.getStreet(), contextFont));
cell1_6.setBorderWidth(0);
cell1_6.setBackgroundColor(finalColor);
mytable.addCell(cell1_6);

Cell cell1_7 = new Cell(new Chunk("Amount:", titleFont));
cell1_7.setBorderWidth(0);
cell1_7.setBackgroundColor(finalColor);
mytable.addCell(cell1_7);

Cell cell1_8 = new Cell(new Chunk("$"+appeal.getAmount(), contextFont));
cell1_8.setBorderWidth(0);
cell1_8.setBackgroundColor(finalColor);
mytable.addCell(cell1_8);

Cell cell2_1 = new Cell(new Chunk("TMK Appealing On:", titleFont));
cell2_1.setBorderWidth(0);
cell2_1.setBackgroundColor(finalColor);
mytable.addCell(cell2_1);

Cell cell2_2 = new Cell(new Chunk(appeal.getTmk(), contextFont));
cell2_2.setBorderWidth(0);
cell2_2.setBackgroundColor(finalColor);
mytable.addCell(cell2_2);

Cell cell2_3 = new Cell(new Chunk(" ", titleFont));
cell2_3.setBorderWidth(0);
cell2_3.setBackgroundColor(finalColor);
mytable.addCell(cell2_3);


Cell cell2_4 = new Cell(new Chunk(" ", contextFont));
cell2_4.setBorderWidth(0);
cell2_4.setBackgroundColor(finalColor);
mytable.addCell(cell2_4);

Cell cell2_5 = new Cell(new Chunk("City:", titleFont));
cell2_5.setBorderWidth(0);
cell2_5.setBackgroundColor(finalColor);
mytable.addCell(cell2_5);

Cell cell2_6 = new Cell(new Chunk(appeal.getCity(), contextFont));
cell2_6.setBorderWidth(0);
cell2_6.setBackgroundColor(finalColor);
mytable.addCell(cell2_6);

Cell cell2_7 = new Cell(new Chunk("Submitted:", titleFont));
cell2_7.setBorderWidth(0);
cell2_7.setBackgroundColor(finalColor);
mytable.addCell(cell2_7);

Cell cell2_8 = new Cell(new Chunk(appeal.getSubmitTime(), contextFont));
cell2_8.setBorderWidth(0);
cell2_8.setBackgroundColor(finalColor);
mytable.addCell(cell2_8);

Cell cell3_1 = new Cell(new Chunk("Title:", titleFont));
cell3_1.setBorderWidth(0);
cell3_1.setBackgroundColor(finalColor);
mytable.addCell(cell3_1);

Cell cell3_2 = new Cell(new Chunk(appeal.getTitle(), contextFont));
cell3_2.setBorderWidth(0);
cell3_2.setBackgroundColor(finalColor);
mytable.addCell(cell3_2);

Cell cell3_3 = new Cell(new Chunk("Home Phone:", titleFont));
cell3_3.setBorderWidth(0);
cell3_3.setBackgroundColor(finalColor);
mytable.addCell(cell3_3);

Cell cell3_4 = new Cell(new Chunk(appeal.getTelephone(), contextFont));
cell3_4.setBorderWidth(0);
cell3_4.setBackgroundColor(finalColor);
mytable.addCell(cell3_4);

Cell cell3_5 = new Cell(new Chunk("Zip:", titleFont));
cell3_5.setBorderWidth(0);
cell3_5.setBackgroundColor(finalColor);
mytable.addCell(cell3_5);

Cell cell3_6 = new Cell(new Chunk(appeal.getZip(), contextFont));
cell3_6.setBorderWidth(0);
cell3_6.setBackgroundColor(finalColor);
mytable.addCell(cell3_6);

Cell cell3_7 = new Cell(new Chunk(" ", titleFont));
cell3_7.setBorderWidth(0);
cell3_7.setBackgroundColor(finalColor);
mytable.addCell(cell3_7);

Cell cell3_8 = new Cell(new Chunk(" ", contextFont));
cell3_8.setBorderWidth(0);
cell3_8.setBackgroundColor(finalColor);
mytable.addCell(cell3_8);
return mytable;
}

你可能感兴趣的:(JAVA)