<<iText in Action 2nd>>4.1节(Constructing tables)读书笔记

前言

iText已经有10年的历史了,如果有人问在这10年中iText中那个对象是最重要的话,本书的作者会毫不犹豫的说是PdfPTable和PdfPCell。因为作者大部分的工作(相信也是大多数人的工作)就是从数据中取出数据然后在文档中以表格的形式呈现出来。因为两个类实在是太重要,这里我们会花费整个第二章的内容来详细说明。

Constructing tables

在iText中处理Table的有很多老版本的类如Table和PdfTable,但由于有些缺点,而且在处理多页的情况分割表格,控制表格单元格等情况下力不从心,因此有了PdfPTable和PdfPCell的出现。iText最新的版本已经移出了PdfPTable和Table,只剩下PdfPable和PdfPCell。

Your first PdfPTable

假设我们想创建一个下图的表格:

FirstTable

那么代码如下,而且比较简单:

listing 4.1 MyFirstTable.cs

PdfPTable table = new PdfPTable(3);
PdfPCell cell;
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
cell = new PdfPCell(new Phrase("cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
table.AddCell("row 1; cell 1");
table.AddCell("row 1;cell 2");
table.AddCell("row 2;cell 1");
table.AddCell("row 2; cell 2");

当我们创建PdfPTable的实例时,一般都要传入列的个数,如果创建表格的列为零的话就会抛出RunTimeException异常。然后我们可以通过PdfPTable的AddCell方法往表格中添加内容。

AUTOMATIC CREATION OF ROWS

以上代码大家要注意的是:我们没有使用PdfPRow对象,iText会在内部使用这个类来存储同一列的单元格。下面我们对listing 4.1进行一些说明:

一开始的创建的PdfPTable有三列,当一个跨三列的单元格被添加之后第一行就满了,接下来的单元格就会被添加到iText内部创建的第二列,但由于第二个单元格跨两行所以第三列也被创建了。接下来的四个单元格中的前二个就被添加到第二列,后两个被添加到第三列。

大家要注意的是当我们将PdfPTable添加到Document时,只有完整的列被添记进去,如果想添加不完整的列我们需要调用PdfPTable的CompleteRow方法。因为iText会为我们处理表格的列,我们要做的是确保添加了正确个数的单元格。

PdfPTable properties

接下来我们会讨论单元格的一些属性:对齐,边框等。但首先我们还是先看下PdfPTable的一些属性:PdfPTable的宽度和列,表格之前或之后的空间以及对齐选项。

表格的宽度是可用空间的80%,对于listing 4.1创建的表格宽度我们就需要做一些数学计算:(595-(36*2))*0.80=418.4pt,其中36是左右页边距的宽度。

表格宽度

每一个PdfPTable有两个关于宽度的属性:

  • WidthPercentage----可用宽度的百分比
  • TotalWidth-----绝对宽度,但单位为user unit

当我们将PdfPTable添加到Document时,iText会根据LockedWidth属性来决定使用其中一个:LockedWidth为true时表格就有一个固定的宽度(使用TotalWidth属性)。默认情况下LockedWidth为false,表格的精确宽度就依赖页面的可用宽度或者被添加到ColumnText对象的宽度。默认每一列的宽度就是表格的宽度除以列数,这个时候可以说每一列的相对宽度为1,但我们可以修改这个默认设置项。

列的相对宽度

TableWithDifferWidth

上图和通过listing 4.1创建的表格看起来是一样的,但表格的宽度和列由五种不同的属性绝对,以下为代码:

listing 4.2 ColumnWidths.cs

public PdfPTable CreateTable1()
{
    PdfPTable table = new PdfPTable(3);

    table.WidthPercentage = 288 / 5.23f;
    table.SetWidths(new int[] { 2, 1, 1 });
    ……
    return table;
}

public PdfPTable CreateTable2()
{
    PdfPTable table = new PdfPTable(3);

    table.TotalWidth = 288;
    table.LockedWidth = true;
    table.SetWidths(new float[] { 2, 1, 1 });
   ………
    return table;
}


public PdfPTable CreateTable3()
{
    PdfPTable table = new PdfPTable(new float[] { 2, 1, 1 });

    table.WidthPercentage = 55.067f;
    ……..
    return table;
}

在以上的三个创建表格的方法中,列都使用了相对宽度,代码中是通过new int[] {2, 1, 1} 或者new float{2, 1, 1}设置,这些代码的意思是我们希望将表格分成2+1+1=4等份:第一列占有2等份,其它的都占有1等份。

假设我们希望创建的表格宽度为288pt而不是523pt,那可以先计算宽度的百分比:(288/523)*100=55.067,或者直接通过TotalWidth 和LockedWidth 属性来设置宽度(这种方法可以避免计算)。

ABSOLUTE COLUMN WIDTHS

在上面的代码中我们是通过定义整个表格的宽度实现的,但还可以定义每一列的宽度来实现,以下为代码:

listing 4.3 ColumnWidths.cs (contiuned)

public PdfPTable CreateTable4()
{
    PdfPTable table = new PdfPTable(3);

    Rectangle rect = new Rectangle(523, 770);
    table.SetWidthPercentage(new float[] { 144, 72, 72 }, rect);
    ……
    return table;
}


public PdfPTable CreateTable5()
{
    PdfPTable table = new PdfPTable(3);

    table.SetTotalWidth(new float[] { 144, 72, 72 });
    table.LockedWidth = true;

     ……
    return table;
}

以上代码中的第一个方法需要还需要计算可用空间,因此直接设置SetTotalWidth方法并且将属性LockedWidth设置为true效果会更好一点。

表格之前和之后的空间

以上代码创建的五个表格是通过以下代码添加到文档中:

listing 4.4 ColumnWidths.cs (contiuned)

PdfPTable table = CreateTable1();
document.Add(table);
table = CreateTable2();
table.SpacingBefore = 5;
table.SpacingAfter = 5;
document.Add(table);
table = CreateTable3();
document.Add(table);
table = CreateTable4();
table.SpacingAfter = 5;
table.SpacingBefore = 5;
document.Add(table);
table = CreateTable5();
document.Add(table);

以上代码通过SpacingBefore和SpacingAfter属性设置了表格之前和之后的空间,这样两个表格不至于挤在一起。如果我们没有设置这些属性,那么就很难分辨不同的表格,但这有时候也有好处:我们可以创建一系列小而且不同的表格来组成一个大的表格,但看起来却是一个整体。

TABLE ALIGNMENT

好了直接上图:

TableAligned

上图中三个表格就没有设置SpacingBefore和SpacingAfter属性,但设置了HorizontalAlignment设置,具体的代码如下:

listing 4.5 TableAlignment.cs

PdfPTable table = CreateFirestTable();
table.WidthPercentage = 50;
table.HorizontalAlignment = Element.ALIGN_LEFT;
document.Add(table);
table.HorizontalAlignment = Element.ALIGN_CENTER;
document.Add(table);
table.HorizontalAlignment = Element.ALIGN_RIGHT;
document.Add(table);

总结

这一节的内容主要是介绍了PdfPTable的一些常用属性,内容比较简单,接下来会介绍PdfPCell的具体使用,最后是代码下载

同步

此文章已同步到目录索引:iText in Action 2nd 读书笔记。

原文地址:https://www.cnblogs.com/julyluo/p/2573827.html