ComponentOne Preview Classic for WinForms: Preview Classic for WinForms Quick Start > Creating Page Headers in C1PrintDocument > Creating a Background Color for the Page Header

Creating a Background Color for the Page Header

Creating a background color for the page header demonstrates how to create a background color for your page header using the C1PrintDocument, C1DocStyle, and RenderTable objects.

1.   Create a new Windows Forms application. Drag and drop a C1PrintPreview control onto your form. Drag and drop 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. Set the value of the Document property of c1PrintPreview1 control to c1PrintDocument1, so that the preview will show the document when the application runs. Double click on the form to create a handler for the Form_Load event – this is where all code shown below will be written.

2.   Call the StartDoc method to start generating the document. This is where we will set up the page settings and page header for the C1PrintDocument, c1PrintDocument1. We'll need to create a table with one row and one column. We'll assign the table to the RenderTable class.

      Visual Basic

Me.C1PrintDocument1.StartDoc()

Me.C1PrintDocument1.PageSettings.Margins.Top = 50

Me.C1PrintDocument1.PageSettings.Margins.Bottom = 25

Me.C1PrintDocument1.PageSettings.Margins.Left = 50

Me.C1PrintDocument1.PageSettings.Margins.Right = 50

Dim theader As New C1.C1PrintDocument.RenderTable(Me.C1PrintDocument1)

 

' Make a table for the page header.

theader.Body.Rows.AddSome(1)

 

' Add a column to the table.

theader.Columns.AddSome(1)

      C#

this.c1PrintDocument1.StartDoc();

this.c1PrintDocument1.PageSettings.Margins.Top = 50;

this.c1PrintDocument1.PageSettings.Margins.Bottom = 25;

this.c1PrintDocument1.PageSettings.Margins.Left = 50;

this.c1PrintDocument1.PageSettings.Margins.Right = 50;

C1.C1PrintDocument.RenderTable theader = new C1.C1PrintDocument.RenderTable(this.c1PrintDocument1);

 

// Make a table for the page header.

theader.Body.Rows.AddSome(1);

 

// Add a column to the table.

theader.Columns.AddSome(1);

3.   The default unit type of the WidthUnit property is inches (in this example, we assign the columns' width unit type to percentage, therefore the column size is dependant on c1PrintDocument1's page width). Set the AllEmpty and Empty properties to True for the table's borders so we can clear the borders default lines.

      Visual Basic

theader.Columns(0).WidthUnit.Type = C1.C1PrintDocument.UnitTypeEnum.Percentage

theader.Columns(0).WidthStr = "100%"

 

' Clear borders.

theader.Style.Borders.AllEmpty = True

theader.StyleTableCell.BorderTableVert.Empty = True

      C#

theader.Columns[0].WidthUnit.Type = C1.C1PrintDocument.UnitTypeEnum.Percentage;

theader.Columns[0].WidthStr = "100%";

 

// Clear borders.

theader.Style.Borders.AllEmpty = true;

theader.StyleTableCell.BorderTableVert.Empty = true;

4.   In this example, we will assign a gold color to our page header in our table. We will use the Style property of the TableRow class to apply the background color to the table, theader.

      Visual Basic

theader.Style.BackColor = Color.Gold

      C#

theader.Style.BackColor = Color.Gold;

5.   We will draw some text onto our table for our page header. We will make our text right-aligned. We create a new font type of Arial and font size of 14 points, for our text.

      Visual Basic

theader.Body.Cell(0, 0).RenderText.Style.TextAlignHorz = C1.C1PrintDocument.AlignHorzEnum.Right

 

' Set up some styles.

theader.StyleTableCell.Font = New Font("Arial", 14)

 

' Assign text.

theader.Body.Cell(0, 0).RenderText.Text = "Swim Team Practice Schedule"

 

' Set the table as the page header.

Me.C1PrintDocument1.RenderBlock(theader)

Me.C1PrintDocument1.EndDoc()

      C#

theader.Body.Cell(0, 0).RenderText.Style.TextAlignHorz = C1.C1PrintDocument.AlignHorzEnum.Right;

 

// Set up some styles.

theader.StyleTableCell.Font = new Font("Arial", 14);

 

// Assign text.

theader.Body.Cell(0, 0).RenderText.Text = "Swim Team Practice Schedule";

 

// Set the table as the page header.

this.c1PrintDocument1.RenderBlock(theader);

this.c1PrintDocument1.EndDoc();

Run the program and observe the following:

Your header should look similar to the following:

 


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.