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.  
  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. public class TableDemo2 {

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

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.         
  7.         PdfPCell cell;
  8.         cell = new PdfPCell(new Phrase("Table 5"));
  9.         cell.setColspan(3);
  10.         cell.setBorderWidth(0);//设置表格的边框宽度为0
  11.         table.addCell(cell);
  12.         
  13.         //加入嵌套表格
  14.         PdfPTable celltable = 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("row 1; cell 1");
  25.         table.addCell("row 1; cell 2");
  26.         table.addCell("row 2; cell 1");
  27.         table.addCell("row 2; 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.      /**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.        
  15.         table.addCell("row 1; cell 1");
  16.         table.addCell("row 1; cell 2");
  17.         table.addCell("row 2; cell 1");
  18.         table.addCell("row 2; 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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 iTextPDF 中使表格文字居中,你可以使用 `setHorizontalAlignment()` 方法将单元格内的文本设置为居中对齐。以下是一个示例代码片段,展示如何创建一个居中对齐的表格单元格: ```java import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Table; public class TableExample { public static void main(String[] args) throws Exception { String dest = "path/to/output.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document doc = new Document(pdfDoc); // 创建一个包含3列的表格 Table table = new Table(3); // 设置单元格的宽度 float[] columnWidths = {100f, 100f, 100f}; table.setWidths(columnWidths); // 创建居中对齐的单元格 Cell cell = new Cell(); cell.add("居中对齐的文本"); // 设置文本居中 cell.setTextAlignment(com.itextpdf.layout.property.TextAlignment.CENTER); // 将单元格添加到表格中 table.addCell(cell); // 将表格添加到文档中 doc.add(table); doc.close(); } } ``` 在上述示例中,我们创建了一个包含3列的表格,并为表格设置了宽度。然后,我们创建了一个单元格并将文本内容添加到其中。通过调用 `setTextAlignment()` 方法并传递 `TextAlignment.CENTER` 参数,我们将文本内容设置为居中对齐。最后,我们将单元格添加到表格中,并将表格添加到文档中。运行代码并保存生成的 PDF 文件,你将看到表格中的文本居中显示。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值