Adding Paragraphs to the Document
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.
Note: The sample code fragments in this topic assume that "using C1.C1Preview;" directive (in C# syntax; or an equivalent for other languages) has been inserted into the file, so that instead of fully qualified type names (such as C1.C1Preview.RenderText) we can use just the class name part (RenderText).
A RenderParagraph can be created with the following line of code:
Dim rp As New RenderParagraph()
• C#
RenderParagraph rp = new RenderParagraph();
Paragraphs should be used rather than RenderText in any of the following cases:
• You need to show text in different styles within the same paragraph.
• Inline images (usually small icon-like images) must be inserted in the text flow.
• A hyperlink must be associated with a portion of the text (for example, a word) rather than with the whole text (please see the Anchors and Hyperlinks section).
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:
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
• C#
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();
}
Run the program and observe the following:
The following image shows the document generated by this code:
|