Posted by a ceTe Software moderator
Hello,
You will need to build the table by adding the images to the cell by setting calculated dimensions. You can calculate the available space or occupied space in the page using page dimensions and position and height of the table. Once you completely build the table then add it to the page. Below is the code sample.
Document document = new Document();
Table2 table = new Table2(0, 0, 500, 0);
// Add columns to the table
Page page1 = new Page();
table.Columns.Add(100);
table.Columns.Add(400);
float occupiedHeight = table.Y;
//Add other rows to the Table.
for (int i = 1; i < 12; i++)
{
Row2 row1 = table.Rows.Add();
Cell2 r1c1 = row1.Cells.Add("Row text :" + i);
Cell2 r1c2 = row1.Cells.Add("Row text :" + i);
occupiedHeight = row1.ActualRowHeight + occupiedHeight + (float)table.CellDefault.Border.Width;
if (occupiedHeight > page1.Dimensions.Body.Height - 10)
{
occupiedHeight = 0;
}
}
string imagePath = @"File path of an image";
Image imageObj = new Image(imagePath, 0, 0);
imageObj.Width = table.Width - 6;
// Build the table by adding the image to cell.
Row2 row = table.Rows.Add(20);
if (imageObj.Height < page1.Dimensions.Body.Height - occupiedHeight)
{
imageObj.Height = imageObj.Height;
}
else
{
imageObj.Height = page1.Dimensions.Body.Height - occupiedHeight;
}
Cell2 cell2 = row.Cells.Add(imageObj);
row.Height = imageObj.Height;
cell2.ColumnSpan = 2;
occupiedHeight = table.GetRequiredHeight() + occupiedHeight + (float)table.CellDefault.Border.Width;
if (occupiedHeight > page1.Dimensions.Body.Height - 10)
{
occupiedHeight = 0;
}
//Add other rows to the Table.
for (int i = 1; i < 30; i++)
{
Row2 row1 = table.Rows.Add();
Cell2 r1c1 = row1.Cells.Add("Row text :" + i);
Cell2 r1c2 = row1.Cells.Add("Row text :" + i);
occupiedHeight = row1.ActualRowHeight + occupiedHeight + (float)table.CellDefault.Border.Width;
if (occupiedHeight > page1.Dimensions.Body.Height - 10)
{
occupiedHeight = 0;
}
}
string imagePath1 = @"File path of an image";
Image imageObj1 = new Image(imagePath1, 0, 0);
imageObj1.Width = table.Width - 6;
// Build the table by adding the image to cell.
Row2 row2 = table.Rows.Add(20);
if (imageObj1.Height < page1.Dimensions.Body.Height - occupiedHeight)
{
imageObj1.Height = imageObj1.Height;
}
else
{
imageObj1.Height = page1.Dimensions.Body.Height - occupiedHeight;
}
Cell2 cell21 = row2.Cells.Add(imageObj1);
row2.Height = imageObj1.Height;
cell21.ColumnSpan = 2;
occupiedHeight = table.GetRequiredHeight() + occupiedHeight + (float)table.CellDefault.Border.Width;
if (occupiedHeight > page1.Dimensions.Body.Height - 10)
{
occupiedHeight = 0;
}
//Adding table to the PDF page.
do
{
Page page = new Page();
document.Pages.Add(page);
if (table.GetRequiredHeight() < page.Dimensions.Body.Height)
{
table.Height = table.GetRequiredHeight();
}
else
{
table.Height = page.Dimensions.Body.Height;
}
page.Elements.Add(table);
table = table.GetOverflowRows();
} while (table != null);
// Save the PDF
document.Draw(@"C:\Temp\MyDocument.pdf");
Thanks,
ceTe Software Support Team