Table: Header Row column span

The Table layout that I am trying to achieve is displayed in Table 2 within the attached document. The code I am using is listed below and the result of this code is displayed in Table 1 within the attached document. Note I am not using the DocumentBuilder option to insert tables instead I am inserting tables directly into the Document Object Model. I have tried various solutions found in the Forms but none have worked. Please advise.

public void Process()
{
    …
    …
    var table = SetTableHeader();
    SetSubHeader(table,SubHeader);
    InsertData(table,MainData);
    _document.Save("C:\temp\test.docx");
}
private Table SetTableHeader()
{
    var table = new Table(_document);
    var row = new Row(_document);

    row.RowFormat.AllowBreakAcrossPages = false;

    row.RowFormat.HeadingFormat = true;
    var cell = new Cell(_document);
    var cellparagraph = new Paragraph(_document);

    cell.AppendChild(cellparagraph);
    cell.FirstParagraph.AppendChild(new Run(_document, "Header Table Row"));

    row.AppendChild(cell);

    table.AppendChild(row);
    _document.FirstSection.Body.AppendChild(table);

    return table;

}

private void SetSubHeader(Table table, SubHeader subHeader)
{
    Row row = new Row(_document);

    row.RowFormat.AllowBreakAcrossPages = false;
    var cell = new Cell(_document);

    var cellparagraph = new Paragraph(_document);

    cell.AppendChild(cellparagraph);

    cell.FirstParagraph.AppendChild(new Run(_document, SubHeader.Description));

    row.AppendChild(cell);

    foreach (var col in subHeader.Columns)

    {
        cell = new Cell(_document);
        cellparagraph = new Paragraph(_document);

        cellparagraph.AppendChild(new Run(_document, col.Description));

        cell.AppendChild(cellparagraph);

        row.AppendChild(cell);

    }

    table.Rows.Add(row);

}

private void InsertData(Table tabel, Data mainData)
{

    foreach (var data in mainData.Data)

    {

        var row = new Row(_document);

        var cell = new Cell(_document);

        var cellparagraph = new Paragraph(_document);

        cell.AppendChild(cellparagraph);

        cell.FirstParagraph.AppendChild(new Run(_document, data.Description));

        row.AppendChild(cell);

        foreach (var dataDetail in data.Details)

        {
            cell = new Cell(_document);
            cellparagraph = new Paragraph(_document);

            cell.AppendChild(cellparagraph);

            cell.FirstParagraph.AppendChild(new Run(_document, dataDetail.Description));

            row.AppendChild(cell);

        }

        tabel.Rows.Add(row);

    }

}

Hi there,

Thanks for your inquiry. Please refer to the following article:
Specifying Table and Cell Widths

In your case, we suggest you please set the width of table and cells to get the desired output. Please check the following modified code example. Hope this helps you.

var _document = new Document();
var table = SetTableHeader(_document);
Row row = new Row(_document);
row.RowFormat.AllowBreakAcrossPages = false;
var cell = new Cell(_document);
var cellparagraph = new Paragraph(_document);
cell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(180);
cell.AppendChild(cellparagraph);
cell.FirstParagraph.AppendChild(new Run(_document, "Data1"));
row.AppendChild(cell);
for (int i = 0; i < 8; i++)
{
    cell = new Cell(_document);
    cell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);
    cellparagraph = new Paragraph(_document);
    cellparagraph.AppendChild(new Run(_document, "Data2"));
    cell.AppendChild(cellparagraph);
    row.AppendChild(cell);
}
table.Rows.Add(row);
table.PreferredWidth = PreferredWidth.FromPoints(500);
table.AutoFit(AutoFitBehavior.FixedColumnWidths);
_document.Save(MyDir + "Out.docx");
public static Table SetTableHeader(Document _document)
{
    var table = new Table(_document);
    var row = new Row(_document);
    row.RowFormat.AllowBreakAcrossPages = false;
    row.RowFormat.HeadingFormat = true;
    var cell = new Cell(_document);
    cell.CellFormat.PreferredWidth = PreferredWidth.FromPoints(500);
    var cellparagraph = new Paragraph(_document);
    cell.AppendChild(cellparagraph);
    cell.FirstParagraph.AppendChild(new Run(_document, "Header Table Row"));
    row.AppendChild(cell);
    table.AppendChild(row);
    _document.FirstSection.Body.AppendChild(table);
    return table;
}