Nested table with Left/Right Cell Border Breaks AutoFitToWindow Layout

When a single column table is created with ColumnAdjustment,AutoFitToWindow, and a child table is added to one of its rows that contains columns with left/right borders, then the layout of the parent table breaks and content is rendered outside the page boundaries (i.e. rendered into the page margin area). Removing the nested table borders seems to fix the issue, however, that will not work in our scenario.


Code to reproduce:

var document = new Document();
var page = document.Pages.Add();
page.PageInfo.Margin = new MarginInfo(35, 60, 35, 90);

var normalTable = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow, Margin = { Bottom = 5 } };
var normalTableFirstRowCell = normalTable.Rows.Add().Cells.Add();
normalTableFirstRowCell.BackgroundColor = Color.LightBlue;
normalTableFirstRowCell.Paragraphs.Add(“Normal table with AutoFitToWindow (notice the page margin on the right)”);
page.Paragraphs.Add(normalTable);

var parentTableWithNestedTableWithBorders = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow, Margin = { Bottom = 5 } };
var firstRowCell = parentTableWithNestedTableWithBorders.Rows.Add().Cells.Add();
firstRowCell.BackgroundColor = Color.LightBlue;
firstRowCell.Paragraphs.Add(“Parent table with nested table with borders (notice the page margin on the right)”);
var nestedRowTable = new Table
{
ColumnAdjustment = ColumnAdjustment.Customized,
ColumnWidths = “100 110 250”,
DefaultCellBorder = new BorderInfo(BorderSide.All, Color.Black)
};
var firstNestedTableRow = nestedRowTable.Rows.Add();
firstNestedTableRow.Cells.Add(“A”);
firstNestedTableRow.Cells.Add(“B”);
firstNestedTableRow.Cells.Add(“C”);
parentTableWithNestedTableWithBorders.Rows.Add().Cells.Add().Paragraphs.Add(nestedRowTable);
page.Paragraphs.Add(parentTableWithNestedTableWithBorders);

var parentTableWithNestedTableWithoutBorders = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow };
var firstRowCellWithoutBorders = parentTableWithNestedTableWithoutBorders.Rows.Add().Cells.Add();
firstRowCellWithoutBorders.BackgroundColor = Color.LightBlue;
firstRowCellWithoutBorders.Paragraphs.Add(“Parent table with nested table with NO borders (notice the page margin on the right)”);
var nestedRowTableWithoutBorders = new Table
{
ColumnAdjustment = ColumnAdjustment.Customized,
ColumnWidths = “100 110 250”,
DefaultCellBorder = new BorderInfo(BorderSide.None)
};
var firstNestedTableWithoutBordersRow = nestedRowTableWithoutBorders.Rows.Add();
firstNestedTableWithoutBordersRow.Cells.Add(“A”);
firstNestedTableWithoutBordersRow.Cells.Add(“B”);
firstNestedTableWithoutBordersRow.Cells.Add(“C”);
parentTableWithNestedTableWithoutBorders.Rows.Add().Cells.Add().Paragraphs.Add(nestedRowTableWithoutBorders);
page.Paragraphs.Add(parentTableWithNestedTableWithoutBorders);

Hi Greg,

Thanks for your inquiry and sharing code snippet. I used your code snippet to add nested table and observed that parent table width increased when we added a child table with border(s). Actually it was adding width of borders into the table width.
By setting the “IsBordersIncluded” property of the parent table to true, it will adjust border width within table width/margins. I have generated the correct output after a little modification into your code snippet. Please check the following code snippet and attached file generated by the code.

var document = new Document();
var page = document.Pages.Add();
page.PageInfo.Margin = new MarginInfo(35, 60, 35, 90);

var normalTable = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow, Margin = { Bottom = 5 } };
var normalTableFirstRowCell = normalTable.Rows.Add().Cells.Add();
normalTableFirstRowCell.BackgroundColor = Color.LightBlue;
TextFragment txtfrag = new TextFragment("Normal table with AutoFitToWindow (notice the page margin on the right)");
normalTableFirstRowCell.Paragraphs.Add(txtfrag);
page.Paragraphs.Add(normalTable);

var parentTableWithNestedTableWithBorders = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow, Margin = { Bottom = 5 }, IsBordersIncluded = true };
var firstRowCell = parentTableWithNestedTableWithBorders.Rows.Add().Cells.Add();
firstRowCell.BackgroundColor = Color.LightBlue;
txtfrag = new TextFragment("Parent table with nested table with borders (notice the page margin on the right)");
firstRowCell.Paragraphs.Add(txtfrag);
var nestedRowTable = new Table
{
    ColumnAdjustment = ColumnAdjustment.Customized,
    ColumnWidths = "100 110 250",
    DefaultCellBorder = new BorderInfo(BorderSide.All, Color.Black)
};
var firstNestedTableRow = nestedRowTable.Rows.Add();
firstNestedTableRow.Cells.Add("A");
firstNestedTableRow.Cells.Add("B");
firstNestedTableRow.Cells.Add("C");
parentTableWithNestedTableWithBorders.Rows.Add().Cells.Add().Paragraphs.Add(nestedRowTable);
page.Paragraphs.Add(parentTableWithNestedTableWithBorders);

var parentTableWithNestedTableWithoutBorders = new Table { ColumnAdjustment = ColumnAdjustment.AutoFitToWindow };
var firstRowCellWithoutBorders = parentTableWithNestedTableWithoutBorders.Rows.Add().Cells.Add();
firstRowCellWithoutBorders.BackgroundColor = Color.LightBlue;
txtfrag = new TextFragment("Parent table with nested table with NO borders (notice the page margin on the right)");
firstRowCellWithoutBorders.Paragraphs.Add(txtfrag);
var nestedRowTableWithoutBorders = new Table
{
    ColumnAdjustment = ColumnAdjustment.Customized,
    ColumnWidths = "100 110 250",
    DefaultCellBorder = new BorderInfo(BorderSide.None)
};
var firstNestedTableWithoutBordersRow = nestedRowTableWithoutBorders.Rows.Add();
firstNestedTableWithoutBordersRow.Cells.Add("A");
firstNestedTableWithoutBordersRow.Cells.Add("B");
firstNestedTableWithoutBordersRow.Cells.Add("C");
parentTableWithNestedTableWithoutBorders.Rows.Add().Cells.Add().Paragraphs.Add(nestedRowTableWithoutBorders);
page.Paragraphs.Add(parentTableWithNestedTableWithoutBorders);

document.Save("NestedTables_out.pdf");