2
votes

I'm generating a simple Word (docx) document using the Open XML SDK. One think I'm struggling with is how to stop table cell contents from wrapping. Here's my code for generating a table cell:

private TableCell CreateTableCell(string text, int padding)
{
    string marginWidth = padding.ToString(CultureInfo.InvariantCulture);

    return new TableCell(
        new Paragraph(new Run(new Text(text))),
        new TableCellProperties(
            new TableCellWidth { Type = TableWidthUnitValues.Auto },
            new NoWrap(),
            new TableCellMargin(
                new LeftMargin { Type = TableWidthUnitValues.Dxa, Width = marginWidth },
                new TopMargin { Type = TableWidthUnitValues.Dxa, Width = marginWidth },
                new RightMargin { Type = TableWidthUnitValues.Dxa, Width = marginWidth },
                new BottomMargin { Type = TableWidthUnitValues.Dxa, Width = marginWidth })));
}

This results in the table cell width having a type of "dxa"---rather than "auto"---even though I've specified auto. And, if I replace all these "dxa" values with "auto" manually, in the xml document, then the document is as required. So, clearly my "auto" value is being ignored.

What am I missing here?

2

2 Answers

0
votes

You wont find text wrap for individual cells or the entire table in Word. See here.

What you can do is; set the property of word wherein it resizes columns if the text exceeds column width.

Setting the TableCellWidth property wont help you with text wrap

Add the Table Layout property to the table which houses the cells and set its value to "auto" or you can omit the property entirely.

Hope this helps.

0
votes
var cellProperties = new TableCellProperties(new NoWrap { Val = OnOffOnlyValues.On });
cell.AppendChild(cellProperties);