最近做项目用到了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<appealList.size();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;
}
分享到:
相关推荐
itext 生成pdf 目录,itext 生成pdf 目录,itext 生成pdf 目录
Android使用iText生成pdf并读取pdf内容,iText是著名的开放源码的站点sourceforge的一个项目,可用来生成Pdf文件和读取Pdf文件内容.
使用IText生成PDF和WORD文档 包括源码和IText所需jar包
此文档主要讲述了使用Java组件iText来生成PDF,操作PDF
在Java代码中使用itext生成PDF文档,实现PDF文档的导出。
html渲染页面转换为pdf并设置页眉,页码,水印,目录,二维码
用itext生成pdf文档,生成的内容有表格形式,文本形式,设置字体样式,文档页边距。
itext生成PDF图片文档
NULL 博文链接:https://aa00aa00.iteye.com/blog/1124216
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,...
一个使用itext生成pdf的代码例子,支持中文。 将html代码直接转换为pdf文档。方便便捷。
其中还附有学习笔记一份,上述生成的文档包括对字体的选择(本地或者iTextAsian里面的),有对图片的添加,有对样式的调整,有对table的添加,其中生成的文档格式比较正规,是现实用户使用模板。如有需要欢迎大家...
Java iText 生成pdf文档源代码(包括插入文本、表格、图像)
自己整理的 itext 生成的 pdf 的技术!
NULL 博文链接:https://x125858805.iteye.com/blog/2239773
NULL 博文链接:https://yizhaorong.iteye.com/blog/2157848
利用itext 生成pdf 文件,可以自定义生成的格式,字体的大小,颜色
java使用itext实现pdf文件下载小案例,通过系统路径下载,和通过浏览器下载
itextpdf-5.5.10.jar、itext-asian-5.2.0.jar、iText中文帮助、iText英文帮助文档 以及生成PDF文件时生成表格时单元格合并处理方法
itext 生成pdf的中文帮助文档,具体有:图片、块(下划线/删除线、上标/下标)、短句、段落等。