This project has moved and is read-only. For the latest updates, please go here.

Problem creating tables

Sep 23, 2012 at 6:59 AM
Edited Sep 23, 2012 at 7:02 AM

I am trying to create a table with 17 columns in a landscape orientation document. If I create a 15 column table, it shows up fine. If I go to 17 columns, I get nothing. Is there something I can do to get all 17 columns on the page?

var documentName = string.Format("xxx.docx");
var document = DocX.Create(documentName);
document.PageLayout.Orientation = Novacode.Orientation.Landscape;
var convergenceCyclesTable = document.InsertTable(17,numberOfRows);
convergenceCyclesTable.Alignment =;
convergenceCyclesTable.AutoFit = AutoFit.Contents;
May 28, 2014 at 9:28 AM
Edited May 28, 2014 at 9:31 AM
There is something like a bug in the library. When you create a table with many columns it becomes unusable. Reason is, that each column is created with its initial width. When you create table (independatly of the used method: InsertTable, AddTable, InsertTableBeforeSelf etc.) with many columns and the sum of their widths exceeds the document width - the table becomes unusable.

I solved this problem by adding columns in cycle and decreasing their width.
My code is similar to this:
private void CreateSampleTable(DocX document)
    int rowsCount = 10;
    int columnsCount = 20;
    int columnWidth = 30;

    //There is a bug in DocX library: can not create tables with
    //many columns with AddTable method because their initial
    //width is 154 and table becomes too wide.
    //That's why columns are inserted in cycle.

    Table sampleTable = document.AddTable(rowsCount, 1);
    foreach (Row row in sampleTable.Rows)
        row.Cells[0].Width = columnWidth;

    for (int colIndex = 1; colIndex < columnsCount; colIndex++)
        foreach (Row row in sampleTable.Rows)
            row.Cells[colIndex].Width = columnWidth;

    Paragraph par = document.InsertParagraph();