iText制作表格比较好的文章

1.概述
对于比较简单的表格处理可以用Table,但是如果要处理复杂的表格,这就需要PDFPTable进行处理。
  建立表格之后,可以设定表格的属性,如:边框宽度、边框颜色、衬距(padding space 即单元格之间的间距)大小等属性。下面通过一个简单的例子说明如何使用表格,代码如下:

2.表格的操作
2.1 表格的初始化
你可以用3种不同的方法创建PdfTable:
PdfPTable(float[] relativeWidths);
PdfPTable(int numColumns);
PdfPTable(PdfPTable table);
举例:
// 创建一个有3列的表格
PdfPTable table = new PdfPTable(3);

2.2 表格的宽度和高度
设置表格的宽度有两种方法,分别如下
table.setTotalWidth(float totalWidth);//设置表格的总宽度
table.setTotalWidth(float[] columnWidth);//设置表格的各列宽度

使用以上两个函数,必须使用以下函数,将宽度锁定。
table.setLockedWidth(true);

设置行的高度
cell.setMinimumHeight(60);

代码举例
  1. PdfPTable table = new PdfPTable(3);
  2. table.setTotalWidth(300);
  3. table.setLockedWidth(true);

  4. table.setTotalWidth(new float[]{ 144, 72, 72 });
  5. table.setLockedWidth(true);

2.3 添加单元格
把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
PdfPTable table = new PdfPTable(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代表从当前单元格的位置开始,合并的单元格数
代码举例
  1. //创建一个有3列的表格
  2. PdfPTable table = new PdfPTable(3);
  3. // 定义一个表格单元
  4. PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
  5. // 定义一个表格单元的跨度
  6. cell.setColspan(3);
  7. // 把单元加到表格中
  8. table.addCell(cell);

以上代码建立一个表格,具有一行,一行本来有3列,结果经过合并,只有1列。

代码举例
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;

  3. import com.itextpdf.text.Document;
  4. import com.itextpdf.text.DocumentException;
  5. import com.itextpdf.text.Phrase;
  6. import com.itextpdf.text.Rectangle;
  7. import com.itextpdf.text.pdf.PdfPCell;
  8. import com.itextpdf.text.pdf.PdfPTable;
  9. import com.itextpdf.text.pdf.PdfWriter;

  10. public class TableDemo2 {

  11. public static final String RESULT = "c:\TableDemo2.pdf";

  12. public static void createPdf(String filename)
  13. throws IOException, DocumentException {
  14. // step 1
  15. Document document = new Document();
  16. // step 2
  17. PdfWriter.getInstance(document, new FileOutputStream(filename));
  18. // step 3
  19. document.open();
  20. // step 4
  21. PdfPTable table = createTable1();
  22. document.add(table);
  23. table = createTable2();
  24. table.setSpacingBefore(5);
  25. table.setSpacingAfter(5);
  26. document.add(table);
  27. table = createTable3();
  28. document.add(table);
  29. table = createTable4();
  30. table.setSpacingBefore(5);
  31. table.setSpacingAfter(5);
  32. document.add(table);
  33. table = createTable5();
  34. document.add(table);
  35. // step 5
  36. document.close();
  37. }

  38. /**setWidths()函数举例*/
  39. public static PdfPTable createTable1() throws DocumentException {
  40. PdfPTable table = new PdfPTable(3);
  41. table.setWidthPercentage(288 / 5.23f);
  42. table.setWidths(new int[]{2, 1, 1});
  43. PdfPCell cell;
  44. cell = new PdfPCell(new Phrase("Table 1"));
  45. cell.setColspan(3);
  46. table.addCell(cell);
  47. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  48. cell.setRowspan(2);
  49. table.addCell(cell);
  50. table.addCell("row 1; cell 1");
  51. table.addCell("row 1; cell 2");
  52. table.addCell("row 2; cell 1");
  53. table.addCell("row 2; cell 2");
  54. return table;
  55. }

  56. /**setWidths()函数举例*/
  57. public static PdfPTable createTable2() throws DocumentException {
  58. PdfPTable table = new PdfPTable(3);
  59. table.setTotalWidth(288);
  60. table.setLockedWidth(true);
  61. table.setWidths(new float[]{2, 1, 1});
  62. PdfPCell cell;
  63. cell = new PdfPCell(new Phrase("Table 2"));
  64. cell.setColspan(3);
  65. table.addCell(cell);
  66. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  67. cell.setRowspan(2);
  68. table.addCell(cell);
  69. table.addCell("row 1; cell 1");
  70. table.addCell("row 1; cell 2");
  71. table.addCell("row 2; cell 1");
  72. table.addCell("row 2; cell 2");
  73. return table;
  74. }

  75. /**合并单元格setColspan()函数举例*/
  76. public static PdfPTable createTable3() throws DocumentException {
  77. PdfPTable table = new PdfPTable(new float[]{ 2, 1, 1 });
  78. table.setWidthPercentage(85f);
  79. PdfPCell cell;
  80. cell = new PdfPCell(new Phrase("Table 3"));
  81. cell.setColspan(3);
  82. table.addCell(cell);
  83. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  84. cell.setRowspan(2);
  85. table.addCell(cell);
  86. table.addCell("row 1; cell 1");
  87. table.addCell("row 1; cell 2");
  88. table.addCell("row 2; cell 1");
  89. table.addCell("row 2; cell 2");
  90. return table;
  91. }

  92. /**setWidthPercentage()方法举例*/
  93. public static PdfPTable createTable4() throws DocumentException {
  94. PdfPTable table = new PdfPTable(3);
  95. Rectangle rect = new Rectangle(523, 770);
  96. //rect表示PageSize页面的大小,主要用于检测各列宽度之各是否超过边界,如果超过,则按比例重新赋值
  97. table.setWidthPercentage(new float[]{ 144, 72, 72 }, rect);
  98. PdfPCell cell;
  99. cell = new PdfPCell(new Phrase("Table 4"));
  100. cell.setColspan(3);
  101. table.addCell(cell);
  102. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  103. cell.setRowspan(2);
  104. table.addCell(cell);
  105. table.addCell("row 1; cell 1");
  106. table.addCell("row 1; cell 2");
  107. table.addCell("row 2; cell 1");
  108. table.addCell("row 2; cell 2");
  109. return table;
  110. }

  111. /**setTotalWidth()方法举例*/
  112. public static PdfPTable createTable5() throws DocumentException {
  113. PdfPTable table = new PdfPTable(3);
  114. table.setTotalWidth(new float[]{ 144, 72, 72 });
  115. table.setLockedWidth(true);
  116. PdfPCell cell;
  117. cell = new PdfPCell(new Phrase("Table 5"));
  118. cell.setColspan(3);
  119. table.addCell(cell);
  120. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  121. cell.setRowspan(2);
  122. table.addCell(cell);
  123. table.addCell("row 1; cell 1");
  124. table.addCell("row 1; cell 2");
  125. table.addCell("row 2; cell 1");
  126. table.addCell("row 2; cell 2");
  127. return table;
  128. }

  129. public static void main(String[] args) throws IOException, DocumentException {
  130. TableDemo2.createPdf(RESULT);
  131. }
  132. }

2.4 表格的嵌套
表格的嵌套是通过将表格作为一个cell添加到上一个表格中来完成的。方法如下
public void addCell(PdfPTable table);
代码举例
  1. /**表格嵌套举例*/
  2. public static PdfPTable createTable6() throws DocumentException {
  3. PdfPTable table = new PdfPTable(3);
  4. table.setTotalWidth(new float[]{ 144, 72, 72 });
  5. table.setLockedWidth(true);

  6. PdfPCell cell;
  7. cell = new PdfPCell(new Phrase("Table 5"));
  8. cell.setColspan(3);
  9. cell.setBorderWidth(0);//设置表格的边框宽度为0
  10. table.addCell(cell);

  11. //加入嵌套表格
  12. PdfPTable celltable = new PdfPTable(2);
  13. celltable.addCell("qiantao11");
  14. celltable.addCell("qiantao12");

  15. cell = new PdfPCell(celltable);
  16. cell.setRowspan(2);
  17. cell.setBorderWidth(1);//设置表格的边框宽度为1
  18. cell.setPadding(10);//设置表格与上一个表格的填充为10
  19. table.addCell(cell);

  20. table.addCell("row 1; cell 1");
  21. table.addCell("row 1; cell 2");
  22. table.addCell("row 2; cell 1");
  23. table.addCell("row 2; cell 2");
  24. return table;
  25. }


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);
代码举例如下
  1. /**setTotalWidth(),隐藏边框线举例*/
  2. public static PdfPTable createTable5() throws DocumentException {
  3. PdfPTable table = new PdfPTable(3);
  4. table.setTotalWidth(new float[]{ 144, 72, 72 });
  5. table.setLockedWidth(true);
  6. PdfPCell cell;
  7. cell = new PdfPCell(new Phrase("Table 5"));
  8. cell.setColspan(3);
  9. cell.setBorderWidth(0);
  10. table.addCell(cell);
  11. cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
  12. cell.setRowspan(2);
  13. table.addCell(cell);

  14. table.addCell("row 1; cell 1");
  15. table.addCell("row 1; cell 2");
  16. table.addCell("row 2; cell 1");
  17. table.addCell("row 2; cell 2");
  18. return table;
  19. }


2.6 定义边框的颜色
// 定义单元格的框颜色
cell.setBorderColor(new BaseColor(255, 0, 0));
// 定义单元格的背景颜色
cell.setBackgroundColor(new BaseColor(0xC0, 0xC0, 0xC0));



参考文献
1.解决itext生成嵌套PdfPtable时,格式,字体方面的一些问题. http://blog.youkuaiyun.com/flyfeifei66/article/details/6730139
2.iText 绘制表格的诸多缺陷. http://www.iteye.com/topic/178465

转自:http://blog.chinaunix.net/uid-122937-id-3052666.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值