Reports for WinForms | ComponentOne
Getting Started with Reports for WinForms / Getting Started with Printing and Previewing / Creating Page Headers in C1PrintDocument
In This Topic
    Creating Page Headers in C1PrintDocument
    In This Topic

    This topic describes how to create a page header with three parts, and add a background color to the page header.

    Creating a Page Header with Three Parts and Adding Background Color to the Page Header:

    Here you will learn how to create a header that is divided into three columns. The following key points are shown in this topic:

    The following detailed steps demonstrate how to create a header with three parts.

    1. Create a new Windows Forms application.
    2. Add a C1PrintPreview control onto your form.
    3. Add a C1PrintDocument component onto your form. It will appear in the components' tray below the form. The preview will have the default name C1PrintPreview1, the document C1PrintDocument1.
    4. Set the value of the Document property of the C1PrintPreview1 control to C1PrintDocument1, so that the preview will show the document when the application runs.
    5. Double click the form to create a handler for the Form_Load event this is where all code shown below will be written. In the Form_Load event, we will set up our document. Create a RenderTable for the page header:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Me.C1PrintDocument1.StartDoc()    
      Dim theader As New C1.C1Preview.RenderTable(Me.C1PrintDocument1)
      

      To write code in C#

      C#
      Copy Code
      this.c1PrintDocument1.StartDoc();    
      C1.C1Preview.RenderTable theader = new C1.C1Preview.RenderTable(this.c1PrintDocument1);
      
    6. Add one row to its body, and 3 columns for the left, middle, and right parts of the header. We will use the TextAlignHorz property to set the alignment of the text in each column of the page header. We will also assign a new font style for the text in our page header. Note, in this example the font type will be Arial and it will be 14 points in size.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      ' Set up alignment for the parts of the header.    
      theader.Cells(0, 0).Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Left    
      theader.Cells(0, 1).Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Center     
      theader.Cells(0, 2).Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Right    
      theader.CellStyle.Font = New Font("Arial", 14)
      

      To write code in C#

      C#
      Copy Code
      // Set up alignment for the columns of the header.    
      theader.Cells[0, 0].Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Left;    
      theader.Cells[0, 1].Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Center;    
      theader.Cells[0, 2].Style.TextAlignHorz = C1.C1Preview.AlignHorzEnum.Right;    
      theader.CellStyle.Font = new Font("Arial", 14);
      
    7. Add the following code, to assign a gold color to the page header. We will use the Style property of the TableRow class to apply the background color to the table, theader.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      theader.Style.BackColor = Color.Gold
      

      To write code in C#

      C#
      Copy Code
      theader.Style.BackColor = Color.Gold;
      
    8. We will draw the text into each column of the table for the page header. Set the RenderObject property of the document's PageHeader to theader. Finish generating the document by calling the EndDoc method.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      theader.Cells(0, 0).Text = "Left part"    
      theader.Cells(0, 1).Text = "Center part"    
      theader.Cells(0, 2).Text = "Right part"    
      Me.C1PrintDocument1.RenderBlock(theader)    
      Me.C1PrintDocument1.EndDoc()
      

      To write code in C#

      C#
      Copy Code
      theader.Cells[0, 0].Text = "Left part";    
      theader.Cells[0, 1].Text = "Center part";    
      theader.Cells[0, 2].Text = "Right part";   
      this.c1PrintDocument1.RenderBlock(theader);    
      this.c1PrintDocument1.EndDoc();
      

    Run the program and observe the following:

    Your new page header with three parts will appear similar to the header below at run time:

    Your header should look similar to the following header, when you add a background color: