openxmlxmltablewordprocessingml

Fix the cell width in Word using Open XML SDK


I have a table with one row in a docx file and I want to add some rows. For the first existing row I set the GridSpan to 3. In the next one I want add a row only with two cell, then I have the result as on the picture. How can I fix the new row width to the table width? The same I want to do, when I add only a one cell in the new row.

enter image description here

My code:

private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
        {
            TableRow newRow = new TableRow();

            var firstCell = tbl.Descendants<TableRow>().First()
                  .Descendants<TableCell>().First();

            firstCell.Append(new TableCellProperties(new GridSpan() { Val = 3 }));

            for (int i = 0, max = cellsQuantity; i < max; i++)
            {
                // TableCellProperties tcp = new TableCellProperties(new TableCellWidth() { Width = firstCell.TableCellProperties.TableCellWidth.Width, Type = firstCell.TableCellProperties.TableCellWidth.Type });
                TableCell cell = new TableCell(new Paragraph(new Run(new Text("test"))));
                newRow.AppendChild(cell);
            }

            tbl.Append(newRow);

            return newRow;
        }

Solution

  • Ok, I thing I got it :)

     private static TableRow AddRow(WordprocessingDocument docx, Table tbl, int cellsQuantity)
            {
                TableRow newRow = new TableRow();
    
                var grid = tbl.Descendants<TableGrid>().First();
    
                var firstCell = tbl.Descendants<TableRow>().First()
                      .Descendants<TableCell>().First();
    
                firstCell.Append(new TableCellProperties(new GridSpan() { Val = 4 }));
    
                int ind = 0;
                int[] widthPerColumn = new int[] { 3070, 1536, 1535, 3071 };
                grid.Descendants<GridColumn>().ToList().ForEach(x =>
                {
                    x.Width = widthPerColumn[ind++].ToString();
                });
    
                int[] gridSpan = null;
                switch (cellsQuantity)
                {
                    case 4:
                        gridSpan = new int[] { 1, 1, 1, 1 };
                        break;
                    case 3:
                        gridSpan = new int[] { 1, 2, 1 };
                        break;
                    case 2:
                        gridSpan = new int[] { 2, 2 };
                        break;
                    case 1:
                        gridSpan = new int[] { 4 };
                        break;
                    default:
                        throw new InvalidOperationException("The cellsQuantity variable must have a value from [1,4] range");
                }
    
                for (int i = 0, max = cellsQuantity; i < max; i++)
                {
                    TableCellProperties tcp = new TableCellProperties(new GridSpan() { Val = gridSpan[i] });
                    TableCell cell = new TableCell(tcp, new Paragraph(new Run(new Text("test"))));
                    newRow.AppendChild(cell);
                }
    
                tbl.Append(newRow);
    
                return newRow;
            }