Reports for WinForms | ComponentOne
Getting Started with Reports for WinForms / Getting Started with Printing and Previewing / Adding Text
In This Topic
    Adding Text
    In This Topic

    This topic describes how to add paragraphs and add text below a table. The following topic describes how to modify the font and style of text.

    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:

    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();
        
    }
    

    Adding Text Below the Table:

    Here you will learn how to use the C1.C1PrintDocument.RenderTable object to render the text into the block flow. It also demonstrates how to use the Padding property to advance the block position so that the next render object (in this case, text) is rendered there. We will use the Padding property to put the text 1 cm below the table in your document. This topic assumes you already have a table created.

    1. Use the C1.C1PrintDocument.RenderTable object to create the text to be displayed:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      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."
      

      To write code in C#

      C#
      Copy Code
      C1.C1Preview.RenderText caption = new C1.C1Preview.RenderText(this.c1PrintDocument1);    
      caption.Text = "In the table above, there are three rows and three columns.";
      
    2. Use the Padding property to position the text 1 cm below the table:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      caption.Style.Padding.Top = New C1.C1Preview.Unit(1, C1.C1Preview.UnitTypeEnum.Cm)
      

      To write code in C#

      C#
      Copy Code
      caption.Style.Padding.Top = new C1.C1Preview.Unit(1, C1.C1Preview.UnitTypeEnum.Cm);
      
    3. Add the text below the table using the Add method. Insert the Add method for the text below the Add method for the table, as shown in the following code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1PrintDocument1.Body.Children.Add(table)    
      Me.C1PrintDocument1.Body.Children.Add(caption)
      
      

      To write code in C#

      C#
      Copy Code
      this.c1PrintDocument1.Body.Children.Add(table);    
      this.c1PrintDocument1.Body.Children.Add(caption);
      
      

      Note: Placing the Add method for the text below the Add method for the table inserts the text below the table. If it is placed above the Add method for the table, the text will appear above the table.

    Run the program and observe the following:

    The following image shows the paragraph that is added to the document, generated by the code given in the section above:

     

    The following image shows how the document looks, when you add text below a table:

    See Also