Creating Rectangles Using the RenderDirectRectangle Method
Creating rectangles using the RenderDirectRectangle method demontrates how to create four rectangles at different locations on the page. The RenderDirectRectangle method makes it simple to render boxes in your current document by specifying the x and y coordinate positions. This topic also shows how create styles for your rectangles by using the C1DocStyle class.
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. Change C1.C1PrintDocument.Unit.DefaultUnit property from inches to pixels at design-time. Double click on the form to create a handler for the Form_Load event. In that handler, add a call to GenerateDoc method (which we will write in step 2), where all code shown in this topic will be written.
2. Create the GenerateDoc method called in the form load handler. In it, create a new C1.C1PrintDocument.RenderTable object and assign it to a variable. Set up two new styles for the rectangles you will be rendering. Set up styles' ShapeLine and ShapeFillColor properties according to your taste.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GenerateDoc()
End Sub
Private Sub GenerateDoc()
Dim Table1 As C1.C1PrintDocument.RenderTable
Dim Style1 As New C1.C1PrintDocument.C1DocStyle(Me.C1PrintDocument1)
Style1.ShapeLine = New C1.C1PrintDocument.LineDef(Color.Orchid, 3)
Style1.ShapeFillColor = Color.AliceBlue
Dim Style2 As New C1.C1PrintDocument.C1DocStyle(Me.C1PrintDocument1)
Style2.ShapeLine = New C1.C1PrintDocument.LineDef(Color.Coral, 7)
Style2.ShapeFillColor = Color.Yellow
• C#
private void Form1_Load(object sender, System.EventArgs e)
{
GenerateDoc();
}
private void GenerateDoc()
{
C1.C1PrintDocument.RenderTable Table1 = new C1.C1PrintDocument.RenderTable(this.c1PrintDocument1);
C1DocStyle Style1 = new C1.C1PrintDocument.C1DocStyle(this.c1PrintDocument1);
Style1.ShapeLine = new C1.C1PrintDocument.LineDef(Color.Orchid, 3);
Style1.ShapeFillColor = Color.AliceBlue;
C1DocStyle Style2 = new C1.C1PrintDocument.C1DocStyle(this.c1PrintDocument1);
Style2.ShapeLine = new C1.C1PrintDocument.LineDef(Color.Coral, 7);
Style2.ShapeFillColor = Color.Yellow;
3. To start the document generation, call the StartDoc method on the document. In order to properly size our rectangles, we use two variables a and b, calculate their values according to the actual page size of the document, and set the rectangles' sizes based on those values. To render the rectangles, we use the RenderDirectRectangle method which accepts the rectangle's coordinates, size and the style to use.
Me.C1PrintDocument1.StartDoc()
Table1 = New C1.C1PrintDocument.RenderTable(Me.C1PrintDocument1)
Dim a, b As Double
a = Me.C1PrintDocument1.BodyAreaSize.Width / 3
b = Me.C1PrintDocument1.BodyAreaSize.Height / 3
' Create a rectangle in the middle of the page with the Style1 type.
Me.C1PrintDocument1.RenderDirectRectangle(a * 2, b * 2, a, b, Style1)
' Create a second rectangle in the top left corner of the document.
Me.C1PrintDocument1.RenderDirectRectangle(0, 0, a, b, Style1)
' Create a third rectangle inside the first left-hand corner of the rectangle.
Me.C1PrintDocument1.RenderDirectRectangle(a + 100, b + 100, a, b, Style2)
'Create a fourth rectangle inside the second rectangle in the bottom right-hand corner.
Me.C1PrintDocument1.RenderDirectRectangle(a - 100, b - 100, a, b, Style2)
Me.C1PrintDocument1.RenderBlock(Table1)
Me.C1PrintDocument1.EndDoc()
End Sub
• C#
this.c1PrintDocument1.StartDoc();
Double a, b;
a = this.c1PrintDocument1.BodyAreaSize.Width / 3;
b = this.c1PrintDocument1.BodyAreaSize.Height / 3;
// Create a rectangle in the middle of the page with the Style1 type.
this.c1PrintDocument1.RenderDirectRectangle(a * 2, b * 2, a, b, Style1);
// Create a second rectangle in the top left corner of the document.
this.c1PrintDocument1.RenderDirectRectangle(0, 0, a, b, Style1);
// Create a third rectangle inside the first left-hand corner of the rectangle.
this.c1PrintDocument1.RenderDirectRectangle(a + 100, b + 100, a, b, Style2);
// Create a fourth rectangle inside the second rectangle in the bottom right-hand corner.
this.c1PrintDocument1.RenderDirectRectangle(a - 100, b - 100, a, b, Style2);
this.c1PrintDocument1.RenderBlock(Table1);
this.c1PrintDocument1.EndDoc();
}
Run the program and observe the following:
The image below illustrates what the rectangles will appear like at run time:
|