I have a table with 2 columns and 4 rows. I have an other table with just one row and one column. Lets say the first table has data as follows;
Table 1
Channel1 Pass
Channel2 Fail
Channel3 Pass
Channel4 Fail
Now table 2 is as follows
Table2
Pass
Now I want table 2 to be taking the same height and width as table1 and it needs to start exactly beside the table 1 so both of them looks like it is one table. Can you please post some pseudo code. Also if You can explain how I can get the "Table 1" and "Table 2" headings that will be great.
How do I align tables one beside the other without any spacing so it looks like its just one table.
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Support · May 13, 2019Hello,
You can achieve your requirement by adding two tables to the PDF. You will need to set the calculated X position to the second table to place it besides the first one. Below is the code sample.
Document document = new Document();
Page page = new Page();
document.Pages.Add(page);
//build first table by adding rows and columns.
Table2 table1 = new Table2(0,0,200,20);
Column2 colT1C1 = table1.Columns.Add(100);
Column2 colT1C2 = table1.Columns.Add(100);
Row2 rowT1Header = table1.Rows.Add();
Cell2 cellT1H1 = rowT1Header.Cells.Add("HEADER 1");
Cell2 cellT1H2 = rowT1Header.Cells.Add("HEADER 2");
for(int i=0;i<4;i++)
{
Row2 rowT1R1 = table1.Rows.Add();
Cell2 cellT1C1 = rowT1R1.Cells.Add("text"+(i+1));
Cell2 cellT1C2 = rowT1R1.Cells.Add("text"+(i+1));
}
table1.Height = table1.GetRequiredHeight();
page.Elements.Add(table1);
//build second table by adding rows and columns.
float xPosition = table1.X+table1.Width;
float yPosition = table1.Y;
Table2 table2 = new Table2(xPosition, yPosition, 100, 20);
Column2 colT2C1 = table2.Columns.Add(100);
Row2 rowT2Header = table2.Rows.Add();
Cell2 cellT2H1 = rowT2Header.Cells.Add("T2 HEADER 1");
for (int i = 0; i < 4; i++)
{
Row2 rowT2R1 = table2.Rows.Add();
Cell2 cellT2C1 = rowT2R1.Cells.Add("Table2 Text" + (i + 1));
}
table2.Height = table2.GetRequiredHeight();
page.Elements.Add(table2);
document.Draw(@"C:\Temp\MyDocument.pdf");
Thanks,
ceTe Software Support Team
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Forum member · May 13, 2019Thank you so much I will try it out.
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Forum member · May 13, 2019Thanks it works great.
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Forum member · Nov 19, 2021How will you handle if frits table rows overflow ? and where will be second table position?
If i want to display another table after table one?
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Support · Nov 19, 2021Hi,
Here is an example of adding a list of tables one below the other. You will need to keep track of Y position and add new pages accordingly.
Here is a code sample.
class AddingGroupOfTables
{
Document document;
Page page;
float availableHeight;
float currentY = 0;
float padding = 10;
public AddingGroupOfTables()
{
}
public void BuildTable()
{
//Create the Document
document = new Document();
//Create a page
page = new Page();
//Create a table
Table2 table;
//Create a list of tables
List<Table2> tableList = new List<Table2>();
//Creating tables and adding them to a list
for (int p = 1; p < 3; p++)
{
table = new Table2(0, 0, 200, 0);
table.Columns.Add(100);
table.Columns.Add(100);
for (int j = 1; j < 40; j++)
{
Row2 row1 = table.Rows.Add();
Cell2 cell1 = row1.Cells.Add("Table:"+p+":Row #" + j);
Cell2 cell2 = row1.Cells.Add("Item");
}
table.Height = table.GetRequiredHeight();
tableList.Add(table);
}
//Add the first page to the document
AddPage();
//Add the all tables to the document in a for loop
for (int k = 0; k < tableList.Count; k++)
{
AddTable(tableList[k]);
}
//save the pdf to disk.
document.Draw(@"C:\Temp\table.pdf");
System.Diagnostics.Process.Start(@"C:\Temp\table.pdf");
}
private void AddTable(Table2 table)
{
do
{
availableHeight = page.Dimensions.Body.Height - currentY;
//Compare the height of the table and the available height on the page.
//set the table height according to it.
table.Y = currentY;
if (table.GetRequiredHeight() > availableHeight)
{
table.Height = availableHeight;
}
else
{
table.Height = table.GetRequiredHeight();
}
page.Elements.Add(table);
currentY = currentY + table.Height + padding;
if (currentY > page.Dimensions.Body.Height)
{
AddPage();
}
table = table.GetOverflowRows();
}
while (table != null);
}
private void AddPage()
{
//create a page
page = new Page();
//reset the available the currentY to original position.
currentY = 0;
//add the page to the document.
document.Pages.Add(page);
}
}
}
Thanks,
ceTe Software Support Team
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Forum member · Nov 21, 2021I need table bside to another table , not table after table.
like
Table 1 Table 2
Col1 Col2 Col1 Col2
Row11 Row21 Row11 Row21
Row21 Row22 Row21 Row22
................................ ................................
RowN1 RowN1 RowN1 RowN1
one line three tables and next line 3 tables .....
And need to handle overflows also.
give right example , if possible..
Re: How do I align tables one beside the other without any spacing so it looks like its just one table.
Support · Nov 22, 2021Hi,
You can add tables one beside the other. You will just need to calculate the X position of next table as per previous table position and width (float XPos=table1.X+table1.Width+Padding).
For handling overflow rows, please refer to the documentation on table continuation
here
.
Thanks,
ceTe Software Support Team