Adding Two Images to Specific Cells of the Table
Adding two images to specific cells of the table demonstrates how to add two different images to specific cells in an existing table by using the RenderImage class. It also shows how to align images in cells using the ImageAlignHorzEnum. The sample below uses the empty 3 by 3 table which was built in Creating a Table with Three Columns and Rows in C1PrintDocument.
1. The following code should already exist in your source file:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Make a table with 3 columns and 3 rows.
Dim table As C1.C1PrintDocument.RenderTable = New C1.C1PrintDocument.RenderTable(Me.C1PrintDocument1)
table.Columns.AddSome(3)
table.Body.Rows.AddSome(3)
' Generate the document.
Me.C1PrintDocument1.StartDoc()
Me.C1PrintDocument1.RenderBlock(table)
Me.C1PrintDocument1.EndDoc()
End Sub
• C#
private void Form1_Load(object sender, System.EventArgs e)
{
// Make a table with 3 columns and 3 rows.
C1.C1PrintDocument.RenderTable table = new C1.C1PrintDocument.RenderTable(this.c1PrintDocument1);
table.Columns.AddSome(3);
table.Body.Rows.AddSome(3);
// Generate the document.
this.c1PrintDocument1.StartDoc();
this.c1PrintDocument1.RenderBlock(table);
this.c1PrintDocument1.EndDoc();
}
2. Add the following code after the line adding the rows (the new code will fix the center cell's size in the table):
' Fix the center cell's size.
table.Columns(1).WidthStr = "10cm"
table.Body.Rows(1).HeightStr = "8cm"
• C#
// Fix the center cell's size.
table.Columns[1].WidthStr = "10cm";
table.Body.Rows[1].HeightStr = "8cm";
3. Create two JPEG or GIF images or use images that already exist.
4. Drop two PictureBox controls onto your form. Set their Image properties to the two images created in the previous step. Also, make the two picture boxes invisible (set Visible to False) so that they won't clutter the form (those controls are used only as storage for the images. The images will be rendered into the C1PrintDocument).
5. Use the StyleTableCell property to modify the base styles for the cells' content. In this sample we will modify the ImageAlign for the cells. Enter the following code to set up the image alignment:
' Set up image alignment.
table.StyleTableCell.ImageAlign.BestFit = False
table.StyleTableCell.ImageAlign.StretchHorz = False
table.StyleTableCell.ImageAlign.StretchVert = False
table.StyleTableCell.ImageAlign.AlignHorz = C1.C1PrintDocument.ImageAlignHorzEnum.Centertable.StyleTableCell.ImageAlign.AlignVert = C1.C1PrintDocument.ImageAlignVertEnum.Center
• C#
// Set up image alignment.
table.StyleTableCell.ImageAlign.BestFit = false;
table.StyleTableCell.ImageAlign.StretchHorz = false;
table.StyleTableCell.ImageAlign.StretchVert = false;
table.StyleTableCell.ImageAlign.AlignHorz = C1.C1PrintDocument.ImageAlignHorzEnum.Center;table.StyleTableCell.ImageAlign.AlignVert = C1.C1PrintDocument.ImageAlignVertEnum.Center;
6. In C1PrintDocument, images are rendered using the RenderImage class (which subclasses the RenderObject). Create two new RenderImage objects for the two images as follows:
Dim img1 As C1.C1PrintDocument.RenderImage = New C1.C1PrintDocument.RenderImage(Me.C1PrintDocument1)
Dim img2 As C1.C1PrintDocument.RenderImage = New C1.C1PrintDocument.RenderImage(Me.C1PrintDocument1)
• C#
C1.C1PrintDocument.RenderImage img1 = new C1.C1PrintDocument.RenderImage(this.c1PrintDocument1);
C1.C1PrintDocument.RenderImage img2 = new C1.C1PrintDocument.RenderImage(this.c1PrintDocument1);
7. Now, set the RenderImage's Image properties to the images stored in the picture boxes:
img1.Image = Me.PictureBox1.Image
img2.Image = Me.PictureBox2.Image
• C#
img1.Image = this.pictureBox1.Image;
img2.Image = this.pictureBox2.Image;
8. Assign the RenderImage objects to the RenderObject properties of the cells so that the images will render in those cells:
table.Body.Cell(1, 1).RenderObject = img1
table.Body.Cell(1, 2).RenderObject = img2
• C#
table.Body.Cell(1, 1).RenderObject = img1;
table.Body.Cell(1, 2).RenderObject = img2;
Note: The top left cell of the table is at row 0, column 0.
Run the program and observe the following:
Your table should look similar to the table below:
|