Column Widths in Tables

Hi,

I would like to insert a table into an Aspose.Words document. I would like the table width to format (adjust) itself according to number of columns and the text in the cells. If I insert an html table via DocBuilder.InsertHtml, it does what I want. However, this mangles the formatting of the document. If I build the table via DocBuilder.StartTable, DocBuilder.InsertCell method, the proper formatting is preserved in the document but I do not see a way to not specify the column width.

Please help.

Hi

Thanks for your request. Actually there is no “column” concept in the Word table. By Microsoft Word design rows in a table in a Microsoft Word document are completely independent. It means each row can have any number of cells of any width. So you can specify only width of cell. You can use CellFormat.Width to set width of table cell.
Please see the following code example:

// Create Document and DocumentBuilder
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Build table
builder.InsertCell();
builder.Write("first column");
// specify width of cell
builder.CellFormat.Width = 50;
builder.InsertCell();
builder.Write("Second column");
builder.CellFormat.Width = 200;
builder.EndRow();
// note that in the next row you should also specify width of each cell
// Build table
builder.InsertCell();
builder.Write("first column");
// specify width of cell
builder.CellFormat.Width = 50;
builder.InsertCell();
builder.Write("Second column");
builder.CellFormat.Width = 200;
builder.EndRow();
builder.EndTable();
doc.Save(@"Test115\out.doc");

Hope this helps.
Best regards.