开始使用Reports for WinForms > 打印和预览功能入门 > 创建页脚 |
注意:文章中的范例代码片段都是假设已经在代码文件中使用了"using C1.C1Preview;"(这是C#语法,其他语言也有等效的写法)指令,因此我们可以只使用类名(例如RenderText)而不必使用完全限定类型名(C1.C1Preview.RenderText)。 |
完成如下步骤来创建一个由两部分构成的页脚
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);
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 属性将这两行合并到了一起。随后,我们给页脚上的文本赋上了新的字体样式,给页脚上的单元格赋上新的背景色。
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(); |
你新建的由两部分构成的页脚看起来应该跟下图相似