本文展示了如何创建一个带有两列内容的页脚。本文的关键点如下:
- 在C1PrintDocument中给一个带有多行多列的表格添加页脚
- 为每一页的底部设置页脚。
- TableVectorCollection 类的Count 属性是用来给每一页的表格底部插入页脚的。
- 设置页脚每个部分中的行和列的合并跨度。
TableCell类的 SpanRows 和SpanCols属性用来指定行和列的合并跨度值。
- 为页脚的每个部分设置文本对齐方式。Style 类的TextAlignHorz 和TextAlignVert属性是用来指定文本的水平和垂直对齐方式的。你可以给TextAlignHorz 属性赋一个AlignHorzEnum 类型的枚举值(左对齐(left),右对齐(right) 两端对齐(justify)或居中对齐(center)),或者给TextAlignVert 属性赋一个AlignVertEnum 类型的枚举值(居下对齐(bottom),居中对齐( center),上下对齐( justify), 或居上对齐( top))。
 |
注意:文章中的范例代码片段都是假设已经在代码文件中使用了"using C1.C1Preview;"(这是C#语法,其他语言也有等效的写法)指令,因此我们可以只使用类名(例如RenderText)而不必使用完全限定类型名(C1.C1Preview.RenderText)。
|
完成如下步骤来创建一个由两部分构成的页脚
- 创建一个新的Windows窗体应用程序
- 从工具栏中添加一个C1PrintPreview 控件到你的窗体上。在窗体上添加一个C1PrintDocument 组件--他会显示在窗体下方的组件托盘中。预览控件的默认名为C1PrintPreview1,文档则是C1PrintDocument1。
- 将C1PrintPreview1 控件的Document 属性设置为C1PrintDocument1,随后程序运行时预览控件就会显示文档的内容

- 双击窗体创建一个Form_Load 事件的处理程序。在这里,我们会建立我们的文档对象。
- 将下面的代码添加到Form_Load事件处理程序中,创建一个用于页脚的RenderTable 对象,然后添加一个4列100行的表格,示例如下:
Visual Basic
Visual Basic |
拷贝代码
|
Dim rt1 As New C1.C1Preview.RenderTable(Me.C1PrintDocument1)
' Create a table with 100 rows, 4 columns, and text.
Dim r As Integer = 100
Dim c As Integer = 4
Dim row As Integer
Dim col As Integer
For row = 0 To r - 1 Step +1
For col = 0 To c - 1 Step +1
Dim celltext As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1)
celltext.Text = String.Format("Cell ({0},{1})", row, col)
rt1.Cells(row, col).RenderObject = celltext
Next
Next
' Add the table to the document.
Me.C1PrintDocument1.Body.Children.Add(rt1)
|
C#
C# |
拷贝代码
|
C1.C1Preview.RenderTable rt1 = new C1.C1Preview.RenderTable(this.c1PrintDocument1);
// Create a table with 100 rows, 4 columns, and text.
const int r = 100;
const int c = 4;
for (int row = 0; row < r; ++row)
{
for (int col = 0; col < c; ++col)
{
C1.C1Preview.RenderText celltext = new C1.C1Preview.RenderText(this.c1PrintDocument1);
celltext.Text = string.Format("Cell ({0},{1})", row, col);
rt1.Cells[row, col].RenderObject = celltext;
}
}
// Add the table to the document.
this.c1PrintDocument1.Body.Children.Add(rt1);
|
- 添加如下代码,将字体设为Arial,字号10pt,然后将背景色设为 柠檬透明色:
Visual Basic
Visual Basic |
拷贝代码
|
' Set up the table footer.
rt1.RowGroups(rt1.Rows.Count - 2, 2).PageFooter = True
rt1.RowGroups(rt1.Rows.Count - 2, 2).Style.BackColor = Color.LemonChiffon
rt1.RowGroups(rt1.Rows.Count - 2, 2).Style.Font = New Font("Arial", 10, FontStyle.Bold)
|
C#
C# |
拷贝代码
|
// Set up the table footer.
rt1.RowGroups[rt1.Rows.Count - 2, 2].PageFooter = true;
rt1.RowGroups[rt1.Rows.Count - 2, 2].Style.BackColor = Color.LemonChiffon;
rt1.RowGroups[rt1.Rows.Count - 2, 2].Style.Font = new Font("Arial", 10, FontStyle.Bold);
|
这里我们利用Count 属性将页面的最后两行保留下来作为页脚使用,同时利用RowGroups 属性将这两行合并到了一起。随后,我们给页脚上的文本赋上了新的字体样式,给页脚上的单元格赋上新的背景色。
- 接下来,我们会利用TextAlignHorz 和TextAlignVert属性来设置页脚上每一列文本的对齐方式。我们会利用 SpanRows 和 SpanCols 属性将最后两行合并,将列合并为两列。最后调用Generate 方法来创建文档。
Visual Basic
Visual Basic |
拷贝代码
|
' Add table footer text.
rt1.Cells(rt1.Rows.Count - 2, 0).SpanRows = 2
rt1.Cells(rt1.Rows.Count - 2, 0).SpanCols = rt1.Cols.Count - 1
rt1.Cells(rt1.Rows.Count - 2, 0).Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Left
rt1.Cells(rt1.Rows.Count - 2, 0).Style.TextAlignVert = C1.C1Preview.AlignVertEnum.Center
Dim tf As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1)
tf = CType(rt1.Cells(rt1.Rows.Count - 2, 0).RenderObject, C1.C1Preview.RenderText)
tf.Text = "This is a table footer."
' Add page numbers.
rt1.Cells(rt1.Rows.Count - 2, 3).SpanRows = 2
rt1.Cells(rt1.Rows.Count - 2, 3).Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Right
rt1.Cells(rt1.Rows.Count - 2, 3).Style.TextAlignVert = C1.C1Preview.AlignVertEnum.Center
' Tags (such as page no/page count) can be inserted anywhere in the document.
Dim pn As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1)
pn = CType(rt1.Cells(rt1.Rows.Count - 2, 3).RenderObject, C1.C1Preview.RenderText)
pn.Text = "Page [PageNo] of [PageCount]"
Me.C1PrintDocument1.Generate()
|
C#
C# |
拷贝代码
|
// Add table footer text.
rt1.Cells[rt1.Rows.Count - 2, 0].SpanRows = 2;
rt1.Cells[rt1.Rows.Count - 2, 0].SpanCols = rt1.Cols.Count - 1;
rt1.Cells[rt1.Rows.Count - 2, 0].Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Center;
rt1.Cells[rt1.Rows.Count - 2, 0].Style.TextAlignVert = C1.C1Preview.AlignVertEnum.Center;
((C1.C1Preview.RenderText)rt1.Cells[rt1.Rows.Count - 2, 0].RenderObject).Text = "This is a table footer.";
// Add page numbers.
rt1.Cells[rt1.Rows.Count - 2, 3].SpanRows = 2;
rt1.Cells[rt1.Rows.Count - 2, 3].Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Right;
rt1.Cells[rt1.Rows.Count - 2, 3].Style.TextAlignVert = C1.C1Preview.AlignVertEnum.Center;
// Tags (such as page no/page count) can be inserted anywhere in the document.
((C1.C1Preview.RenderText)rt1.Cells[rt1.Rows.Count - 2, 3].RenderObject).Text = "Page [PageNo] of [PageCount]";
this.c1PrintDocument1.Generate();
|
运行程序看一下:
你新建的由两部分构成的页脚看起来应该跟下图相似
