itext表格(转载)
来源:互联网 发布:搞笑网络诈骗小品剧本 编辑:程序博客网 时间:2024/06/11 10:59
1.概述
对于比较简单的表格处理可以用Table,但是如果要处理复杂的表格,这就需要PDFPTable进行处理。
建立表格之后,可以设定表格的属性,如:边框宽度、边框颜色、衬距(padding space即单元格之间的间距)大小等属性。下面通过一个简单的例子说明如何使用表格,代码如下:
2.表格的操作
2.1 表格的初始化
你可以用3种不同的方法创建PdfTable:
PdfPTable(float[] relativeWidths);
PdfPTable(int numColumns);
PdfPTable(PdfPTable table);
举例:
// 创建一个有3列的表格
PdfPTable table = newPdfPTable(3);
2.2 表格的宽度和高度
设置表格的宽度有两种方法,分别如下
table.setTotalWidth(floattotalWidth); //设置表格的总宽度
table.setTotalWidth(float[]columnWidth);//设置表格的各列宽度
使用以上两个函数,必须使用以下函数,将宽度锁定。
table.setLockedWidth(true);
设置行的高度
cell.setMinimumHeight(60);
代码举例
2.3 添加单元格
把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
PdfPTable table = newPdfPTable(3);
table.addCell("1.1");
table.addCell("1.2");
table.addCell("1.3");
table.addCell("2.1");
table.addCell("2.2");
table.addCell("2.3");
以上程序运行结果将显示三行二列的表格。
添加单元格的内容还可以是以下几种形式。
public void addCell(PdfPCell cell);
public void addCell(PdfPTable table);
public void addCell(Phrase phrase);
public void addCell(String text);
2.3 合并单元格
iText合并单元格的过程如下,首先创建一个cell,设置这个单元格的跨度,
如果是横向合并,则通过
cell.setColspan(n); //n代表从当前单元格的位置开始,合并的单元格数
如果是纵向合并,
cell.setRowspan(n);//n代表从当前单元格的位置开始,合并的单元格数
代码举例
以上代码建立一个表格,具有一行,一行本来有3列,结果经过合并,只有1列。
代码举例
2.4 表格的嵌套
表格的嵌套是通过将表格作为一个cell添加到上一个表格中来完成的。方法如下
public void addCell(PdfPTable table);
代码举例
2.5 设置表格的边框
边框的线必须通过以下代码来完成
cell.setBorderWidth(borderwidth)
cell.setBorderWidthBottom(borderwidth)
cell.setBorderWidthTop(borderwidth)
cell.setBorderWidthBottom(borderwidth)
cell.setBorderWidthRight(borderwidth)
cell.setBorderWidthLeft(borderwidth)
嵌套时如果想在两个表格之间留一定的间隔,可以通过以下方法来完成
cell.setPadding(padding);
cell.setPaddingBottom(padding);
cell.setPaddingTop(padding);
cell.setPaddingRight(padding);
cell.setPaddingLeft(padding);
代码举例如下
建立表格之后,可以设定表格的属性,如:边框宽度、边框颜色、衬距(padding space即单元格之间的间距)大小等属性。下面通过一个简单的例子说明如何使用表格,代码如下:
2.表格的操作
2.1 表格的初始化
2.2 表格的宽度和高度
代码举例
- PdfPTable table = new PdfPTable(3);
- table.setTotalWidth(300);
- table.setLockedWidth(true);
- table.setTotalWidth(new
float[]{ 144, 72, 72 }); - table.setLockedWidth(true);
2.3 添加单元格
以上程序运行结果将显示三行二列的表格。
2.3 合并单元格
代码举例
-
//创建一个有3列的表格 -
PdfPTable table = new PdfPTable(3); -
//定义一个表格单元 -
PdfPCellcell = new PdfPCell(new Paragraph("headerwith colspan 3")); -
//定义一个表格单元的跨度 -
cell.setColspan(3); -
//把单元加到表格中 -
table.addCell(cell);
以上代码建立一个表格,具有一行,一行本来有3列,结果经过合并,只有1列。
代码举例
- import
;">; - import
;">; -
- import
com.itextpdf.text.Document; - import
com.itextpdf.text.DocumentException; - import
com.itextpdf.text.Phrase; - import
com.itextpdf.text.Rectangle; - import
com.itextpdf.text.pdf.PdfPCell; - import
com.itextpdf.text.pdf.PdfPTable; - import
com.itextpdf.text.pdf.PdfWriter; - public
class TableDemo2 { -
public static final String RESULT = "c:\TableDemo2.pdf"; -
-
public static void createPdf(String filename) -
throws IOException, DocumentException { -
//step 1 -
Document document = new Document(); -
//step 2 -
PdfWriter.getInstance(document, new FileOutputStream(filename)); -
//step 3 -
document.open(); -
//step 4 -
PdfPTable table = createTable1(); -
document.add(table); -
table = createTable2(); -
table.setSpacingBefore(5); -
table.setSpacingAfter(5); -
document.add(table); -
table = createTable3(); -
document.add(table); -
table = createTable4(); -
table.setSpacingBefore(5); -
table.setSpacingAfter(5); -
document.add(table); -
table = createTable5(); -
document.add(table); -
//step 5 -
document.close(); -
} -
-
-
public static PdfPTablecreateTable1() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
table.setWidthPercentage(288 / 5.23f); -
table.setWidths(new int[]{2, 1, 1}); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table1")); -
cell.setColspan(3); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
} -
-
-
public static PdfPTablecreateTable2() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
table.setTotalWidth(288); -
table.setLockedWidth(true); -
table.setWidths(new float[]{2, 1, 1}); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table2")); -
cell.setColspan(3); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
} -
-
-
public static PdfPTablecreateTable3() throws DocumentException { -
PdfPTable table = new PdfPTable(new float[]{ 2, 1, 1 }); -
table.setWidthPercentage(85f); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table3")); -
cell.setColspan(3); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
} -
-
-
public static PdfPTablecreateTable4() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
Rectangle rect = new Rectangle(523, 770); -
//rect表示PageSize页面的大小,主要用于检测各列宽度之各是否超过边界,如果超过,则按比例重新赋值 -
table.setWidthPercentage(new float[]{ 144, 72, 72 }, rect); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table4")); -
cell.setColspan(3); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
} -
-
-
public static PdfPTablecreateTable5() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
table.setTotalWidth(new float[]{ 144, 72, 72 }); -
table.setLockedWidth(true); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table5")); -
cell.setColspan(3); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
} -
-
public static void main(String[] args) throws IOException, DocumentException { -
TableDemo2.createPdf(RESULT); -
} - }
2.4 表格的嵌套
代码举例
-
-
public static PdfPTablecreateTable6() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
table.setTotalWidth(new float[]{ 144, 72, 72 }); -
table.setLockedWidth(true); -
-
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table5")); -
cell.setColspan(3); -
cell.setBorderWidth(0);//设置表格的边框宽度为0 -
table.addCell(cell); -
-
//加入嵌套表格 -
PdfPTablecelltable = new PdfPTable(2); -
celltable.addCell("qiantao11"); -
celltable.addCell("qiantao12"); -
-
cell = new PdfPCell(celltable); -
cell.setRowspan(2); -
cell.setBorderWidth(1);//设置表格的边框宽度为1 -
cell.setPadding(10);//设置表格与上一个表格的填充为10 -
table.addCell(cell); -
-
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
}
2.5 设置表格的边框
代码举例如下
-
-
public static PdfPTablecreateTable5() throws DocumentException { -
PdfPTable table = new PdfPTable(3); -
table.setTotalWidth(new float[]{ 144, 72, 72 }); -
table.setLockedWidth(true); -
PdfPCellcell; -
cell = new PdfPCell(new Phrase("Table5")); -
cell.setColspan(3); -
cell.setBorderWidth(0); -
table.addCell(cell); -
cell = new PdfPCell(new Phrase("Cellwith rowspan 2")); -
cell.setRowspan(2); -
table.addCell(cell); -
-
table.addCell("row1; cell 1"); -
table.addCell("row1; cell 2"); -
table.addCell("row2; cell 1"); -
table.addCell("row2; cell 2"); -
return table; -
}
2.6定义边框的颜色
参考文献
1.解决itext生成嵌套PdfPtable时,格式,字体方面的一些问题. http://blog.csdn.NET/flyfeifei66/article/details/6730139
2.iText 绘制表格的诸多缺陷.
转自:http://blog.chinaunix.net/uid-122937-id-3052666.html
阅读全文
0 0
- itext表格(转载)
- iText系列(二) iText---表格
- itext表格控制
- iText 生成复杂表格
- iText输出表格
- iText制作表格
- Struts2利用iText导出word文档(包含表格)
- 表格(转载)
- iText的简单应用-表格
- 利用itext创建PDF表格
- itext设置默认NO_BORDER表格
- Java iText 生成PDF表格
- java IText 导出word表格
- Ajax+Css表格(转载)
- 【Itext】itext5添加表格背景颜色,itext pdf行变色
- 利用itext操作pdf从数据库导出大量数据--创建PDF表格(三)
- iText整合图片和自带表格生成PDF文件(很好用的框架)
- 利用itext操作pdf从数据库导出大量数据--创建PDF表格(三)
- 日语语法整理四
- net use错误解释
- Java 8 新特性001_Java 8 Optional 类
- 在Web应用中动态创建PDF文件(转载…
- 从 Java 应用程序动态生…
- itext表格(转载)
- linux下oracle创建用户(转)
- linux下启动tomcat(转)
- linux oracle导入表
- 关于war包部署到tomcat 404
- Oracle建立表空间和用户(转)
- ROS 在ubuntu 12.04 下的安装及测试
- spring事务嵌套容易产生重复记录
- apizza开发工具