在这个快速入门的指导教程中,我们会按照惯例创建一个简单的"Hello World!"文档。在后续的步骤里,你需要在项目中添加Reports for WinForms的打印和预览控件,设置好预览并深入了解下预览控件所支持的一些运行时交互功能。
步骤1/4:在窗体上添加预览控件
在本步骤中,你需要在窗体上添加一个Reports for WinForms控件,并且设置好窗体来创建一个简单的"Hello World!"文
档预览。最简单的文档应该是只打印一句"Hello World!"的文档了。创建这样一份文档,需要完成如下步骤来配置窗体:
注意:注意:文章中的范例代码片段都是假设已经在代码文件中使用了"using C1.C1Preview;" 指令(这是C#语法,其他语言也有等效的写法),因此我们可以只使用类名(例如RenderText)而不必使用完全限定类型 |
C1PrintPreviewControl 是一个包含预览框、导航、文本搜索面板和带有预定义按钮工具栏的复合控件。此外,项目的引用中还会出现两个额外的对象:C1.C1Report.2 and C1.Win.C1Report.2.
C1PrintDocument1,并且出现在窗体下方的组件托盘中。
你已经创建了一个项目,并在项目中添加了Reports for WinForms的预览控件,完成了快速入门向导教程的第一步。下一步,你将要设置窗体和控件。
步骤2/4:设置窗体和控件
现在,你已经在窗体上添加了Reports for WinForms 控件,你将要设置窗体和控件。完成如下步骤:
窗体设计器现在应该看起来跟下图相似:
C1PrintDocument1 组件(它会出现在文档属性的下拉列表中)
这个被选中的名为C1PrintDocument1的文档,将会在程序运行时显示在预览窗体中。
你现在已经设置好了窗体和控件,并且完成了打印和预览快速入门教程的第二步。在下一步中,你需要在项目中添加代码。
步骤3/4:在项目中添加代码
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'!
现在,你已经在窗体上添加了Reports for WinForms控件,并且对窗体和控件进行了个性化设置。在运行项目之前,还有最后一个步骤要做。在这个步骤中,你需要在项目中通过添加代码来设置出现在该项目中的文本。
Visual Basic
Visual Basic |
Me.C1PrintDocument1.Body.Children.Add(New C1.C1Preview.RenderText("Hello, |
C#
C# |
this.c1PrintDocument1.Body.Children.Add(new RenderText("Hello, World!")); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.Generate() |
C#
C# |
this.c1PrintDocument1.Generate(); |
步骤4/4:运行项目
你已经建立了窗体和Reports for WinForms控件,为控件设置了属性,同时也为项目添加了代码。接下来是运行这个项目,看一下下Reports for WinForms预览控件在运行时支持的一些交互操作。
该预览会显示只有一个页面的文档,文档的左上角显示"Hello World"。
恭喜你已经创建了一个"Hello world"文档,并完成了打印和预览快速入门的向导教程!保存你的项目,在接下来的入门教程中,你还会继续往里添加内容。制作一个简单的表格
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'! 在文档中,表格是最有用的功能之一。表格既可以将数据以表格的形式展现出来,也可以用来为文档中其他元素提供布局。
C1PrintDocument 控件提供了全功能的表格。在本节,你会学习如何开始使用表格。我们会以C1PrintDocument 控件快速入门中创建的"Hello World"范例应用程序为基础,在其中添加一个表格。
制作简单的表格制作简单的表格:
注意:注意:文章中的范例代码片段都是假设已经在使代码文件中用了"using C1.C1Preview;" 指令(这是C#语法,其他语言也有等效的写法),因此我们可以只使用类名(例如RenderText)而不必使用完全限定类型名 |
Generate方法的代码之前:
Visual Basic
Visual Basic |
Dim rt As New RenderTable() |
C#
C# |
RenderTable rt = new RenderTable(); this.c1PrintDocument1.Body.Children.Add(rt); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.Generate() |
C#
C# |
this.c1PrintDocument1.Generate(); |
预览中显示的文档会看起来跟下图很相似
这个简单的例子展示了在C1PrintDocument控件中使用表格的几个重要方面的内容:
表格是由RenderObject类派生的RenderTable 类来表示的。
表格遵循在Microsoft Excel中使用的模型:它们的尺寸在初始化的时候是不限的,表格在渲染时的实际尺寸是由拥有最大行和列号的有值单元格所确定。在我们的例子中,由于这个表格高度是10行,宽度是6列,因此拥有最大行和列索引值的单元格的位置是在(9,5)(索引值
从0开始计数)。如果你修改代码,例如在(10,7)位置的单元格上添加文本,表格就会变成11行8列。
Visual Basic
Visual Basic |
rt.Cells(10, 7).Text = "text at row 10, column 7" |
C#
C# |
rt.Cells[10, 7].Text = "text at row 10, column 7"; |
默认情况下,表格没有可见的网格线(Report for Winforms 中的网格线术语是指用于画表格的线,跟画在渲染对象周围的边框不同)。在Form Load事件处理中添加下行代码来添加网格线(用0.5 pt的钢笔线画):
Visual Basic
Visual Basic |
rt.Style.GridLines.All = LineDef.Default |
C#
C# |
rt.Style.GridLines.All = LineDef.Default; |
• 默认情况下,表格的宽度与它父级对象在客户端呈现的宽度一致(这个例子中是整个页面),并且每列的宽度等分。行的高度则是自动分配的。因此,如果你添加一行,并在其中任意一个单元格中添加一串长文本,你会发现单元格所属的行会自动向下扩展来容纳全部的文本内容。例如,在我们的例子中添加下方的代码就会生成如下图一样的表格(这个表格包含了上文描述的两个改动)
Visual Basic
Visual Basic |
rt.Cells(3, 4).Text = "A long line of text showing that table rows stretch " + "to accommodate all content." |
C#
C# |
rt.Cells[3, 4].Text = "A long line of text showing that table rows stretch " + "to accommodate all content."; |
你可以参考创建上述文档的完整form load事件处理代码:
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load col += 1 Loop row += 1 Loop rt.Cells(3, 4).Text = "A long line of text showing that table rows " + "stretch to accommodate all content." rt.Cells(10, 7).Text = "text at row 10, column 7" rt.Style.GridLines.All = LineDef.Default Me.C1PrintDocument1.Generate() |
C#
C# |
private void Form1_Load(object sender, EventArgs e) { |
创建一个三行三列的表格
本文演示了设置一个三行三列表格的基本内容。完成如下步骤:
C1PrintPreviewControl控件的文档属性设置为如下图所示的C1PrintDocument:
这样就能使C1PrintPreviewControl控件显示C1PrintDocument了
Visual Basic
Visual Basic |
Dim table As C1.C1Preview.RenderTable = New C1.C1Preview.RenderTable(Me.C1PrintDocument1) |
C#
C# |
C1.C1Preview.RenderTable table = new C1.C1Preview.RenderTable(this.c1PrintDocument1); |
Visual Basic
Visual Basic |
' Add 3 rows. |
Next
Next
C#
C# |
// Add 3 rows. const int r = 3; |
此外注意,当我们直接将列添加到表格时,行也被添加到了表格主体(Body)中。这是因为RenderTable 对象总
是由三个区域组成:头部(Header),主体(Body)和尾部(Footer)。在表格中这三者都是有可能为空的。如果你只是想创建一个简单的表格,那么你可以像我们这个例子中的做法一样,直接将行添加到主体(Body)中即可。
Visual Basic
Visual Basic |
table.Height = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) table.Width = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) |
C#
C# |
table.Height = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm); table.Width = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm); |
Visual Basic
Visual Basic |
table.Style.GridLines.All = New C1.C1Preview.LineDef(Color.DarkGray) |
C#
C# |
table.Style.GridLines.All = new C1.C1Preview.LineDef(Color.DarkGray); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.Body.Children.Add(table) |
C#
C# |
this.c1PrintDocument1.Body.Children.Add(table); this.c1PrintDocument1.Generate(); |
运行程序看一下::
你的应用程序运行时将会看起来跟下图很相似:
向单元格添加文本
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'!
本文展示了如何使用RenderText类向表格中的特定单元格添加文本。
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As |
C#
C# |
private void Form1_Load(object sender, System.EventArgs e) { |
环。如下方所示,在嵌套循环的主体中将Text属性设置为需要的文本内容(由于这个范例的原因,我们将(1,1) 和
(1,2)单元格的值留空)
Visual Basic
Visual Basic |
' Add 3 rows. |
Dim row As Integer |
C#
C# |
// Add 3 rows. const int r = 3; |
运行程序看一下:你的表格应该看起来跟下面相似:
本文展示了如何利用RenderImage 类向当前表格的特定单元格中添加两幅不同的图片。同时也展示了在单元格中如何使用
ImageAlignHorzEnum来排列图片。注意,下面的范例使用了在"创建一个三行三列的表格"章节中创建的3x3尺寸的空表格,此外你还需要准备两幅GIF或JPEG格式的图片来完成本文中的各个步骤。完成如下步骤:
1. 如下代码应该已经存在于你的源代码文件中:
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) |
Dim celltext As C1.C1Preview.RenderText = New
C1.C1Preview.RenderText(Me.C1PrintDocument1)
' Add empty cells.
celltext.Text = String.Format("", row, col) table.Cells(row, col).RenderObject = celltext Next
Next
' Generate the document.
Me.C1PrintDocument1.Body.Children.Add(table)
Me.C1PrintDocument1.Generate()
End Sub
C#
C# |
private void Form1_Load(object sender, System.EventArgs e) { |
Visual Basic
Visual Basic |
' Fix the center cell's size. table.Rows(1).Height = New C1.C1Preview.Unit(5, C1.C1Preview.UnitTypeEnum.Cm) table.Cols(1).Width = New C1.C1Preview.Unit(8, C1.C1Preview.UnitTypeEnum.Cm) |
C#
C# |
// Fix the center cell's size. table.Rows[1].Height = new C1.C1Preview.Unit(5, C1.C1Preview.UnitTypeEnum.Cm); table.Cols[1].Width = new C1.C1Preview.Unit(8, C1.C1Preview.UnitTypeEnum.Cm); |
Visual Basic
Visual Basic |
' Set up image alignment. |
C#
C# |
// Set up image alignment. |
如下所示,为两幅图片创建两个新的RenderImage 对象。
Visual Basic
Visual Basic |
Dim img1 As C1.C1Preview.RenderImage = New |
C#
C# |
C1.C1Preview.RenderImage img1 = new |
Visual Basic
Visual Basic |
img1.Image = Me.PictureBox1.Image img2.Image = Me.PictureBox2.Image |
C# |
img1.Image = this.pictureBox1.Image; img2.Image = this.pictureBox2.Image; |
Visual Basic
Visual Basic |
table.Cells(1, 1).RenderObject = img1 table.Cells(1, 2).RenderObject = img2 |
C#
C# |
table.Cells[1, 1].RenderObject = img1; table.Cells[1, 2].RenderObject = img2; |
注意:注意:表格中左上角单元格的行列号都是0 |
运行程序看一下
你的表格应该看起来你下面的表格相似
创建表格中行和列的外边框
!MISSING PHRASE 'Show All'! !MISSING PHRASE 'Hide All'!
本文展示了如何利用LineDef 类对行和列添加实线外边框。本文假设你已经有了一个三行三列的表格。
1. 在你的源代码文件中应该已经存在如下代码:
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As |
' Add empty cells.
celltext.Text = String.Format("", row, col) table.Cells(row, col).RenderObject = celltext Next
Next
' Generate the document.
Me.C1PrintDocument1.Body.Children.Add(table)
Me.C1PrintDocument1.Generate()
End Sub
C#
C# |
private void Form1_Load(object sender, System.EventArgs e) { |
Visual Basic
Visual Basic |
table.Height = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) table.Width = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) |
C#
C# |
table.Height = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm); table.Width = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm); |
Visual Basic
Visual Basic |
table.Rows(2).Style.Borders.All = New C1.C1Preview.LineDef("2pt", Color.Red) |
C#
C# |
table.Rows[2].Style.Borders.All = new C1.C1Preview.LineDef("2pt", Color.Red); |
Visual Basic
Visual Basic |
table.Cols(0).Style.Borders.All = New C1.C1Preview.LineDef("6pt", Color.Blue) |
C#
C# |
table.Cols[0].Style.Borders.All = new C1.C1Preview.LineDef("6pt", Color.Blue); |
运行程序看一下:运行时,你的边框将会跟下面的表格相似
为表格中的特定单元格创建背景色
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'!
本文展示了如何为表格中的特定单元格添加背景色。同时,也展示了如何利用TableCell.CellStyle属性来设置用来渲染表格的样式。本文假设你已经有了一个三行三列的表格。
1. 在你的源代码文件中应该已经存在了如下代码
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As |
C1.C1Preview.RenderText(Me.C1PrintDocument1)
' Add empty cells.
celltext.Text = String.Format("", row, col) table.Cells(row, col).RenderObject = celltext Next
Next
' Generate the document.
Me.C1PrintDocument1.Body.Children.Add(table)
Me.C1PrintDocument1.Generate()
End Sub
C#
C# |
private void Form1_Load(object sender, System.EventArgs e) { |
Visual Basic
Visual Basic |
table.Height = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) table.Width = New C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm) |
C#
C# |
table.Width = new C1.C1Preview.Unit(15, C1.C1Preview.UnitTypeEnum.Cm); |
Visual Basic
Visual Basic |
table.Cells(1, 2).CellStyle.BackColor = Color.Crimson |
C#
C# |
table.Cells[1, 2].CellStyle.BackColor = Color.Crimson; |
注意:注意:这里的行和列都是从0开始计数。上述代码使用TableCell.CellStyle 属性来设置单元格的样式 |
Visual Basic
Visual Basic |
table.Cells(0, 1).CellStyle.BackColor = Color.BlueViolet |
C#
C# |
table.Cells[0, 1].CellStyle.BackColor = Color.BlueViolet; |
运行程序看一下:
你的表格应该看起来跟下面的表格类似:
添加文本
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'!
下面的文章描述了如何添加段落、在表格下方添加文本、修改字体和文本的样式。
C1PrintDocument 组件的所有内容都是由各种渲染对象来呈现的。Reports for WinForms程序集提供了多个派生于
RenderObject类的层次化的类,它们被设计用来呈现各种类型的内容,比如文本、图片等等。例如,上文中我们就利用
RenderText 类往文档中添加了一行文本。在本文中,我们会展示如何利用RenderParagraph类来创建一个文本段落(这些段落可能包含了不同样式的文本段、内联的图片和超链接)
注意:注意:文章中的范例代码片段都是假设已经在使代码文件中用了"using C1.C1Preview;" 指令(这是C#语法,其他语言也有等效的写法),因此我们可以只使用类名(例如RenderText)而不必使用完全限定类型名 |
用下面代码创建RenderParagraph:
Visual Basic
Visual Basic |
Dim rp As New RenderParagraph() |
C#
C# |
RenderParagraph rp = new RenderParagraph(); |
在以下情况下应该使用Paragraphs 来代替RenderText
你需要在同一个段落中显示不同样式的文本。
文本中必须要插入内联的图片(通常是像图标一样的小图片)
超链接需要被添加到部分文本上(例如,某个单词),而不是整个文本(请参阅Anchors and Hyperlinks章节)
段落的内容由各种ParagraphObject 对象构成。ParagraphObject 是一个抽象基类,它的两个派生类分别是ParagraphText 和ParagraphImage,分别用于呈现分段的文本和内联图片。你可以通过创建这两个类型的对象来填充段落中的内容,然后把它们添加到RenderParagraph.Content集合中。为了便于创建和设置这些对象,它们提供各种构造函数的重载和属性。向段落中添加超链接,可以通过指定段落对象的Hyperlink 属性为hyperlink来实现。此外,你还可以像下方示例中所示的,使用AddText, AddImage和AddHyperlink这类快捷方法的重载方法。
Visual Basic
Visual Basic |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) |
在文档的表格下方添加文本
!MISSING PHRASE 'Show All'!
!MISSING PHRASE 'Hide All'!
本文展示了如何利用C1.C1PrintDocument.RenderTable对象将文本渲染到块状浮动布局中。本文也展示了如何使用
Padding 属性来将块的位置上移,使得后一个渲染对象(这里指的是text)能够显示在那里。
在本文中,我们会使用Padding 属性将文本放置在距离表格下方1厘米的位置处。本文假设表格已经创建好了。
Visual Basic
Visual Basic |
Dim caption As C1.C1Preview.RenderText = New C1.C1Preview.RenderText(Me.C1PrintDocument1) caption.Text = "In the table above, there are three rows and three columns." |
C#
C# |
C1.C1Preview.RenderText caption = new |
Visual Basic
Visual Basic |
caption.Style.Padding.Top = New C1.C1Preview.Unit(1, |
C#
C# |
caption.Style.Padding.Top = new C1.C1Preview.Unit(1, |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.Body.Children.Add(table) |
C#
C# |
this.c1PrintDocument1.Body.Children.Add(table); this.c1PrintDocument1.Body.Children.Add(caption); |
注意:注意:要将添加文本的Add方法放在添加表格的Add方法下面。如果放在上面,则文本会出现在表格上方。 |
你的文档应该看起来跟下方的文档相似:
修改文本的字体和样式
!MISSING PHRASE 'Show All'! !MISSING PHRASE 'Hide All'!
C1PrintDocument 组件包含了一个RenderInLineText 方法用于渲染指定的字符串将其放在块状流动布局中(block flow),而无需创建一个新的段落。RenderInLineText 方法会自动将文本换行。本文展示了如何使用RenderInLineText 方法。
C1PrintDocument1。将C1PrintPreview1 控件的Document 属性设置为C1PrintDocument1,随后程序运行时预览控件就会显示文档的内容。
Visual Basic
Visual Basic |
Me.C1PrintDocument1.StartDoc() |
C#
C# |
this.c1PrintDocument1.StartDoc(); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.RenderInlineText("Line by Line", New Font("Times New Roman", |
C#
C# |
this.c1PrintDocument1.RenderInlineText("Line by Line", new Font("Times New Roman", 30, FontStyle.Bold), Color.FromArgb(0, 0, 125)); this.c1PrintDocument1.RenderInlineText(" and modify text attributes as you go."); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.RenderInlineText(" The text wraps automatically, so your life becomes easier.", Color.Green) |
C#
C# |
this.c1PrintDocument1.RenderInlineText(" The text wraps automatically, so your life becomes easier.", Color.Green); |
Visual Basic
Visual Basic |
Me.C1PrintDocument1.EndDoc() |
C#
C# |
this.c1PrintDocument1.EndDoc(); |
运行程序看一下:
你的文本应该看起来和下图中的文本相似: