表格是我们在制作文档时,经常使用的一个元素。对布局的控制非常精确。在ITextSharp中表格对象是下面两个元素:

PdfTable,PdfCell

下面从ITextSharp In Action截取一段代码:

一步一步 ITextSharp Table使用

 

从代码中,可以看出,PdfTable的构造函数,传入一个列数为参数,表示这个表格有多少列,往表格中加入PdfCell,如果加入的单元格超过一行,自动会进行换行。单元格中有一个setColspan函数(注:C#版本中,是属性Colspan),用于设置一个单元格跨多列。同样,如果要跨越多行,也有一个属性(C#)RolSpan,上面的演示代码执行结果如下:

一步一步 ITextSharp Table使用

 

PdfTable对象有一个设置表格区域和每列宽度的函数,如下:

public void SetTotalWidth(float[] columnWidth);
        public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize);

属性:HorizontalAlignment 设置表格的对齐方式

HeaderRows 表示第几行作为表格头

FooterRows  表示第几行作为表格尾

SplitLate      表示单元格是否跨页显示

SplitRows     表示行是否跨页显示

一步一步 ITextSharp Table使用

一步一步 ITextSharp Table使用

 

示例代码如下:

class PdfPTableDemo : TestBase
   2:     { 
void Opening(Document document, PdfWriter writer) 
   4:         { 
   5:             document.SetPageSize(PageSize.A4.Rotate()); 
base.Opening(document, writer); 
   7:         } 
void WriteDocument(Document document, PdfWriter writer) 
   9:         { 
  10:             PdfPTable table = CreateTable(); 
//table.WidthPercentage = 80;//设置表格占的宽度,百分比 
//table.TotalWidth = 200;//设置表格占的宽度,单位点数 
//table.SetTotalWidth(); 
//table.SetWidthPercentage(); 
  15:             table.HorizontalAlignment = Element.ALIGN_LEFT; 
  16:             document.Add(table);
  17:  
  18:             table = CreateTable(); 
  19:             table.HorizontalAlignment = Element.ALIGN_RIGHT; 
  20:             table.SpacingBefore = (5); 
  21:             table.SpacingAfter = (5); 
new Rectangle(523, 770); 
  23:             table.SetWidthPercentage( 
float[] { 50, 25, 25 }, rect); 
  25:             document.Add(table); 
  26:             table = CreateTable(); 
  27:             table.HorizontalAlignment = Element.ALIGN_CENTER; 
float[] { 144, 72, 72 }); 
true); 
  30:             document.Add(table); 
  31:             table = CreateTable(); 
  32:             table.SpacingBefore = (15); 
  33:             table.SpacingAfter = (15); 
  34:             document.Add(table);
  35:  
new PdfPTable(3); 
  37:             PdfPCell cell 
, Normal)); 
  39:             cell.BackgroundColor = (BaseColor.YELLOW); 
  40:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  41:             cell.Colspan = (7); 
  42:             table.AddCell(cell);
  43:  
  44:             cell 
, Normal)); 
  46:             cell.BackgroundColor = (BaseColor.YELLOW); 
  47:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  48:             cell.Colspan = (7); 
  49:             table.AddCell(cell);
  50:  
  51:             table.DefaultCell.BackgroundColor = (BaseColor.LIGHT_GRAY); 
int i = 0; i < 100; i++) 
  53:             { 
); 
); 
); 
); 
); 
); 
); 
  61:             } 
null); 
  63:             table.HeaderRows = (2); 
  64:             table.FooterRows = (1); 
  65:             document.Add(table);
  66:  
  67:         }
  68:  
private PdfPTable CreateTable() 
  70:         { 
new PdfPTable(3); 
new AlternatingBackground(); 
//table.WidthPercentage = 80;//设置表格占的宽度,百分比 
//table.TotalWidth = 200;//设置表格占的宽度,单位点数 
//table.SetTotalWidth(); 
//table.SetWidthPercentage(); 
  77:             PdfPCell cell; 
)); 
  79:             cell.Colspan = (3); 
  80:             table.AddCell(cell); 
)); 
  82:             cell.Rowspan = (2); 
  83:             table.AddCell(cell); 
); 
); 
); 
);
  88:  
return table; 
  90:         } 
  91:     }
  92:  

相关文章: