All content of a C1PrintDocument is represented by render objects. The Reports for WinForms assembly provides a hierarchy of classes derived from RenderObject, designed to represent content of various types, such as text, images and so on. For instance, above we used the RenderText class to add a line of text to the document. In this section we will show how to create paragraphs of text (which may combine fragments of text drawn with different styles, inline images, and hyperlinks) using the RenderParagraph class.
A RenderParagraph can be created with the following line of code:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Dim rp As New RenderParagraph() |
To write code in C#
C# |
Copy Code
|
---|---|
RenderParagraph rp = new RenderParagraph(); |
Paragraphs should be used rather than RenderText in any of the following cases:
The content of a paragraph is comprised of ParagraphObject objects. ParagraphObject is the abstract base class; the two inherited classes are ParagraphText and ParagraphImage, representing fragments of text and inline images, correspondingly. You can fill a paragraph with content by creating objects of those two types and adding them to the RenderParagraph.Content collection. Various constructor overloads and properties are provided for convenient creation/setup of those objects. In-paragraph hyperlinks can be created by specifying the Hyperlink property of the paragraph object which should be a hyperlink. An alternative approach is to use various overloads of the shortcut methods AddText, AddImage and AddHyperlink, as shown in the following example:
To write code in Visual Basic
Visual Basic |
Copy Code
|
---|---|
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Create a paragraph. Dim rpar As New RenderParagraph() Dim f As New Font(rpar.Style.Font, FontStyle.Bold) rpar.Content.AddText("This is a paragraph. This is normal text. ") rpar.Content.AddText("This text is bold. ", f) rpar.Content.AddText("This text is red. ", Color.Red) rpar.Content.AddText("This text is superscript. ", TextPositionEnum.Superscript) rpar.Content.AddText("This text is bold and red. ", f, Color.Red) rpar.Content.AddText("This text is bold and red and subscript. ", f, Color.Red, TextPositionEnum.Subscript) rpar.Content.AddText("This is normal text again. ") rpar.Content.AddHyperlink("This is a link to the start of this paragraph.", rpar.Content(0)) rpar.Content.AddText("Finally, here is an inline image: ") rpar.Content.AddImage(Me.Icon.ToBitmap()) rpar.Content.AddText(".") ' Add the paragraph to the document. Me.C1PrintDocument1.Body.Children.Add(rpar) Me.C1PrintDocument1.Generate() End Sub |
To write code in C#
C# |
Copy Code
|
---|---|
private void Form1_Load(object sender, EventArgs e) { // Create a paragraph. RenderParagraph rpar = new RenderParagraph(); Font f = new Font(rpar.Style.Font, FontStyle.Bold); rpar.Content.AddText("This is a paragraph. This is normal text. "); rpar.Content.AddText("This text is bold. ", f); rpar.Content.AddText("This text is red. ", Color.Red); rpar.Content.AddText("This text is superscript. ", TextPositionEnum.Superscript); rpar.Content.AddText("This text is bold and red. ", f, Color.Red); rpar.Content.AddText("This text is bold and red and subscript. ", f, Color.Red, TextPositionEnum.Subscript); rpar.Content.AddText("This is normal text again. "); rpar.Content.AddHyperlink("This is a link to the start of this paragraph.", rpar.Content[0]); rpar.Content.AddText("Finally, here is an inline image: "); rpar.Content.AddImage(this.Icon.ToBitmap()); rpar.Content.AddText("."); // Add the paragraph to the document. this.c1PrintDocument1.Body.Children.Add(rpar); this.c1PrintDocument1.Generate(); } |
The following image shows the document generated by this code: