I have a table when its content is greater than the page it fits the page, this is good.
However, the table fits the content when there is not enough content in the columns/cells of the table. How can I make the table width to fit the width of the page?
NOTE: setting tableStyle.TableFormat.AutomaticallyResizeToFitContents = false; did not resolve the issue.
Following is a snippet of code to illustrate the basic code structure that we have.
var document = new DocumentModel();
document.Styles.Add(tableStyle);
document.Sections.Add(new Section(document));
section.PageSetup.Orientation = Orientation.Landscape;
section.PageSetup.PaperType = PaperType.A4;
section.HeaderFooters.Add(new HeaderFooter(....));
section.HeaderFooters.Add(new HeaderFooter(....));
var table = new Table(document)
{
TableFormat =
{
Style = tableStyle,
PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage)
}
};
section.Blocks.Add(table);
var headerRow = new TableRow(document);
// ... add cells with a paragraph into header row by iterating through our columns collection. Each paragraph consists of a piece of text, with occasional setting of alignment, font size, family and font style.
table.Rows.Add(headerRow);
foreach (var row in ExportData.Rows)
{
var tableRow = new TableRow(document);
foreach (var column in ExportData.Columns)
{
// ... iterating through data, adding cells with a paragraph for each row of data. Each paragraph consists of a piece of text, with occasional setting of alignment, font size, family and font style.
var paragraph = new Paragraph(document)
{
ParagraphFormat =
{
Style = cellStyle
}
};
if (stringValue != null)
{
paragraph.Content.LoadText(stringValue);
}
tableRow.Cells.Add(new TableCell(document, paragraph));
}
table.Rows.Add(tableRow);
}
Table Style
var tableStyle = new TableStyle("xxx");
tableStyle.TableFormat.Borders.SetBorders(MultipleBorderTypes.All, BorderStyle.Single, config.BorderColor(), 1);
tableStyle.TableFormat.StyleOptions = TableStyleOptions.FirstRow | TableStyleOptions.BandedRows;
tableStyle.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);
tableStyle.TableFormat.DefaultCellPadding = new Padding(2, 0, 2, 0);
tableStyle.TableFormat.AutomaticallyResizeToFitContents = true;
tableStyle.RowFormat.AllowBreakAcrossPages = true;
tableStyle.CellFormat.FitText = false;
tableStyle.ParagraphFormat.LeftIndentation = 0.2;
tableStyle.ParagraphFormat.RightIndentation = 0.2;
tableStyle.ParagraphFormat.SpaceAfter = 0.5;
tableStyle.ParagraphFormat.Alignment = HorizontalAlignment.Left;
tableStyle.CharacterFormat = new CharacterFormat
{
FontName = config.FontFamily,
Size = config.FontSize,
FontColor = config.TitleColor()
};
var columnHeaderRowFormatting = tableStyle.ConditionalFormats[TableStyleFormatType.FirstRow];
columnHeaderRowFormatting.CellFormat.BackgroundColor = config.ColumnHeaderBackgroundColor();
columnHeaderRowFormatting.RowFormat.RepeatOnEachPage = true;
columnHeaderRowFormatting.CharacterFormat.Size = config.FontSize + 1;
columnHeaderRowFormatting.CharacterFormat.Bold = true;
var rowConditionalFormatting = tableStyle.ConditionalFormats[TableStyleFormatType.OddBandedRows];
rowConditionalFormatting.CellFormat.BackgroundColor = config.RowBackgroundColor();
var rowAlternateConditionalFormatting = tableStyle.ConditionalFormats[TableStyleFormatType.EvenBandedRows];
rowAlternateConditionalFormatting.CellFormat.BackgroundColor = config.RowAlternateBackgroundColor();
return tableStyle;