itext表格(转载)

9 篇文章 0 订阅
3 篇文章 0 订阅
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);

代码举例
  1. PdfPTable table = new PdfPTable(3);
  2. table.setTotalWidth(300);
  3. table.setLockedWidth(true);
  4.  
  5. table.setTotalWidth(new float[]{ 144, 72, 72 });
  6. table.setLockedWidth(true);

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代表从当前单元格的位置开始,合并的单元格数
代码举例
  1.            //创建一个有3列的表格 
  2.             PdfPTable table = new PdfPTable(3); 
  3.             //定义一个表格单元 
  4.             PdfPCellcell = new PdfPCell(new Paragraph("headerwith colspan 3")); 
  5.             //定义一个表格单元的跨度 
  6.             cell.setColspan(3); 
  7.             //把单元加到表格中 
  8.             table.addCell(cell);

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

代码举例
  1. import ;">;
  2. import ;">;
  3.  
  4. import com.itextpdf.text.Document;
  5. import com.itextpdf.text.DocumentException;
  6. import com.itextpdf.text.Phrase;
  7. import com.itextpdf.text.Rectangle;
  8. import com.itextpdf.text.pdf.PdfPCell;
  9. import com.itextpdf.text.pdf.PdfPTable;
  10. import com.itextpdf.text.pdf.PdfWriter;
  11.  
  12. public class TableDemo2 {
  13.  
  14.     public static final String RESULT = "c:\TableDemo2.pdf";
  15.  
  16.     public static void createPdf(String filename)
  17.         throws IOException, DocumentException {
  18.         //step 1
  19.         Document document = new Document();
  20.         //step 2
  21.         PdfWriter.getInstance(document, new FileOutputStream(filename));
  22.         //step 3
  23.         document.open();
  24.         //step 4
  25.         PdfPTable table = createTable1();
  26.         document.add(table);
  27.         table = createTable2();
  28.         table.setSpacingBefore(5);
  29.         table.setSpacingAfter(5);
  30.         document.add(table);
  31.         table = createTable3();
  32.         document.add(table);
  33.         table = createTable4();
  34.         table.setSpacingBefore(5);
  35.         table.setSpacingAfter(5);
  36.         document.add(table);
  37.         table = createTable5();
  38.         document.add(table);
  39.         //step 5
  40.         document.close();
  41.     }
  42.  
  43.     
  44.     public static PdfPTablecreateTable1() throws DocumentException {
  45.         PdfPTable table = new PdfPTable(3);
  46.         table.setWidthPercentage(288 / 5.23f);
  47.         table.setWidths(new int[]{2, 1, 1});
  48.         PdfPCellcell;
  49.         cell = new PdfPCell(new Phrase("Table1"));
  50.         cell.setColspan(3);
  51.         table.addCell(cell);
  52.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  53.         cell.setRowspan(2);
  54.         table.addCell(cell);
  55.         table.addCell("row1; cell 1");
  56.         table.addCell("row1; cell 2");
  57.         table.addCell("row2; cell 1");
  58.         table.addCell("row2; cell 2");
  59.         return table;
  60.     }
  61.  
  62.     
  63.     public static PdfPTablecreateTable2() throws DocumentException {
  64.         PdfPTable table = new PdfPTable(3);
  65.         table.setTotalWidth(288);
  66.         table.setLockedWidth(true);
  67.         table.setWidths(new float[]{2, 1, 1});
  68.         PdfPCellcell;
  69.         cell = new PdfPCell(new Phrase("Table2"));
  70.         cell.setColspan(3);
  71.         table.addCell(cell);
  72.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  73.         cell.setRowspan(2);
  74.         table.addCell(cell);
  75.         table.addCell("row1; cell 1");
  76.         table.addCell("row1; cell 2");
  77.         table.addCell("row2; cell 1");
  78.         table.addCell("row2; cell 2");
  79.         return table;
  80.     }
  81.  
  82.     
  83.     public static PdfPTablecreateTable3() throws DocumentException {
  84.         PdfPTable table = new PdfPTable(new float[]{ 2, 1, 1 });
  85.         table.setWidthPercentage(85f);
  86.         PdfPCellcell;
  87.         cell = new PdfPCell(new Phrase("Table3"));
  88.         cell.setColspan(3);
  89.         table.addCell(cell);
  90.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  91.         cell.setRowspan(2);
  92.         table.addCell(cell);
  93.         table.addCell("row1; cell 1");
  94.         table.addCell("row1; cell 2");
  95.         table.addCell("row2; cell 1");
  96.         table.addCell("row2; cell 2");
  97.         return table;
  98.     }
  99.  
  100.     
  101.     public static PdfPTablecreateTable4() throws DocumentException {
  102.         PdfPTable table = new PdfPTable(3);
  103.         Rectangle rect = new Rectangle(523, 770);
  104.         //rect表示PageSize页面的大小,主要用于检测各列宽度之各是否超过边界,如果超过,则按比例重新赋值
  105.         table.setWidthPercentage(new float[]{ 144, 72, 72 }, rect);
  106.         PdfPCellcell;
  107.         cell = new PdfPCell(new Phrase("Table4"));
  108.         cell.setColspan(3);
  109.         table.addCell(cell);
  110.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  111.         cell.setRowspan(2);
  112.         table.addCell(cell);
  113.         table.addCell("row1; cell 1");
  114.         table.addCell("row1; cell 2");
  115.         table.addCell("row2; cell 1");
  116.         table.addCell("row2; cell 2");
  117.         return table;
  118.     }
  119.  
  120.     
  121.     public static PdfPTablecreateTable5() throws DocumentException {
  122.         PdfPTable table = new PdfPTable(3);
  123.         table.setTotalWidth(new float[]{ 144, 72, 72 });
  124.         table.setLockedWidth(true);
  125.         PdfPCellcell;
  126.         cell = new PdfPCell(new Phrase("Table5"));
  127.         cell.setColspan(3);
  128.         table.addCell(cell);
  129.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  130.         cell.setRowspan(2);
  131.         table.addCell(cell);
  132.         table.addCell("row1; cell 1");
  133.         table.addCell("row1; cell 2");
  134.         table.addCell("row2; cell 1");
  135.         table.addCell("row2; cell 2");
  136.         return table;
  137.     }
  138.  
  139.     public static void main(String[] args) throws IOException, DocumentException {
  140.         TableDemo2.createPdf(RESULT);
  141.     }
  142. }

2.4 表格的嵌套
      表格的嵌套是通过将表格作为一个cell添加到上一个表格中来完成的。方法如下
                 public void addCell(PdfPTable table);
代码举例
  1.    
  2.     public static PdfPTablecreateTable6() throws DocumentException {
  3.         PdfPTable table = new PdfPTable(3);
  4.         table.setTotalWidth(new float[]{ 144, 72, 72 });
  5.         table.setLockedWidth(true);
  6.         
  7.         PdfPCellcell;
  8.         cell = new PdfPCell(new Phrase("Table5"));
  9.         cell.setColspan(3);
  10.         cell.setBorderWidth(0);//设置表格的边框宽度为0
  11.         table.addCell(cell);
  12.         
  13.         //加入嵌套表格
  14.         PdfPTablecelltable = new PdfPTable(2);
  15.         celltable.addCell("qiantao11");
  16.         celltable.addCell("qiantao12");
  17.         
  18.         cell = new PdfPCell(celltable);
  19.         cell.setRowspan(2);
  20.         cell.setBorderWidth(1);//设置表格的边框宽度为1
  21.         cell.setPadding(10);//设置表格与上一个表格的填充为10
  22.         table.addCell(cell);
  23.         
  24.         table.addCell("row1; cell 1");
  25.         table.addCell("row1; cell 2");
  26.         table.addCell("row2; cell 1");
  27.         table.addCell("row2; cell 2");
  28.         return table;
  29.     }


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.     
  2.     public static PdfPTablecreateTable5() throws DocumentException {
  3.         PdfPTable table = new PdfPTable(3);
  4.         table.setTotalWidth(new float[]{ 144, 72, 72 });
  5.         table.setLockedWidth(true);
  6.         PdfPCellcell;
  7.         cell = new PdfPCell(new Phrase("Table5"));
  8.         cell.setColspan(3);
  9.         cell.setBorderWidth(0);
  10.         table.addCell(cell);
  11.         cell = new PdfPCell(new Phrase("Cellwith rowspan 2"));
  12.         cell.setRowspan(2);
  13.         table.addCell(cell);
  14.        
  15.         table.addCell("row1; cell 1");
  16.         table.addCell("row1; cell 2");
  17.         table.addCell("row2; cell 1");
  18.         table.addCell("row2; cell 2");
  19.         return table;
  20.     }


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



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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值