Table fit window

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.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)

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.  

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)
       tableRow.Cells.Add(new TableCell(document, paragraph));


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;

Hi Siew,

Can you send me the output DOCX file that you get?
I would like to investigate it.
In the code, I see that you’re specifying the table’s width to 100%, so currently, I’m not sure why the problem occurs.


I’ve responded via the email with the document zipped up and attached.

Looking forward to hear from you.

Try this:

table.TableFormat.Style.TableFormat.PreferredWidth = TableWidth.Auto;
table.TableFormat.PreferredWidth = new TableWidth(100, TableWidthUnit.Percentage);

Does this solve your issue?

1 Like

Thank you, Mario, this works perfectly!