We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

CellFormat.Width and Autofit issues

I would like to have a table where the width of the first column fits the text contained within it and the width of the second column is fixed.

By not touching DocumentBuilder.CellFormat.Width, all columns appear to be auto-fit.

As soon as I touch .CellFormat.Width for the 2nd cell, the first cell seems to get messed up, but only when I have more than 1 row…

DocumentBuilder builder = new DocumentBuilder();

builder.StartTable();
builder.RowFormat.HeadingFormat = true;
builder.RowFormat.Borders.Top.LineWidth = 1.5;
builder.RowFormat.Borders.Bottom.LineWidth = 1.5;

Cell cell = builder.InsertCell();
builder.Write(“Headers”);

builder.InsertCell();
builder.CellFormat.Width = 144.0;
builder.Write(“value”);

builder.EndRow();

Cell cell = builder.InsertCell();

builder.Write(“Some Text”);


builder.InsertCell();

builder.CellFormat.Width = 144.0;

builder.Write(“my value”);

builder.EndRow();
builder.EndTable();

I seem to have some luck as long as:
1) I don’t use the DocumentBuilder.CellFormat property, and
2) I set the width of the cells after I have ended the row.

row = builder.EndRow();
for (int iCell = 1; iCell < row.Cells.Count; iCell++)
{
row.Cells[iCell].CellFormat.Width = 144.0;
}

Any thoughts as to why this would be? Is it a bug?

I seem to have some luck as long as
1) I don’t use the DocumentBuilder.CellFormat property; and
2) if I set the width of the cells after I have ended the row.

row = builder.EndRow();
for (int iCell = 1; iCell < row.Cells.Count; iCell++)
{
row.Cells[iCell].CellFormat.Width = 144.0;
}

Any thoughts as to why this would be? Is it a bug?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. This occurs because if you set builder.CellFormat.Width the next cell will be inserted with specified width. So the easiest way to achieve what you need is setting width of the second column after building table. See the following code:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

//builde table

Table myTab = builder.StartTable();

builder.RowFormat.HeadingFormat = true;

builder.RowFormat.Borders.Top.LineWidth = 1.5;

builder.RowFormat.Borders.Bottom.LineWidth = 1.5;

builder.InsertCell();

builder.Write("Headers");

builder.InsertCell();

builder.Write("value");

builder.EndRow();

builder.InsertCell();

//Set heading format to false

//In this case only first row will be repeated as table header

builder.RowFormat.HeadingFormat = false;

builder.Write("Some Text");

builder.InsertCell();

builder.Write("my value");

builder.EndRow();

builder.EndTable();

//Set width of the second column

foreach (Row row in myTab.Rows)

{

row.Cells[1].CellFormat.Width = 144.0;

}

doc.Save(@"Test133\out.doc");

I hope this helps.

Best regards.