I just upgraded to Aspose.Words 6.0.1.0 from an older version and it seems that my table column is set to “Fixed width = 1 inch” even though no CellFormat.Width is specified for the first column…
How can I leave MS Word to autofit the first column based on its content with the new version?
I tried setting RowFormat.AllowAutoFit = true, but that did not change anything.
Thanks for the reply, but that does not seem to fix the problem.
Here’s a sample code that reproduces the problematic output. I also did notice a situation where a real 0 width column is produced. Just uncomment the marked lines to see that too…
Thanks for your help,
dstj.
------------------
Aspose.Words.Document wordDocument = new Aspose.Words.Document();
wordDocument.RemoveAllChildren();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(wordDocument);
Aspose.Words.Tables.Table table;
table = builder.StartTable();
builder.RowFormat.AllowAutoFit = true;
/* // Uncomment this to create 0 width column
builder.RowFormat.ClearFormatting();
builder.RowFormat.AllowAutoFit = false;
*/
builder.InsertCell(); // skip first cell of first column
builder.InsertCell();
builder.Write("Col2");
builder.EndRow();
Random rand = new Random();
for (int i = 0; i < 5; ++i)
{
builder.InsertCell();
builder.Write("THIS IS A LONG SAMPLE TEXT STRING…".Substring(0, rand.Next(36)));
builder.InsertCell();
builder.Write("Fixed02");
builder.EndRow();
}
foreach (Aspose.Words.Tables.Row row in table.Rows)
{
row.Cells[0].CellFormat.Width = 0; // as suggested
row.Cells[1].CellFormat.Width = Aspose.Words.ConvertUtil.InchToPoint(0.93);
}
builder.EndTable();
wordDocument.Save("c:\temp\asposeWords01.doc");
Hi
Thanks for your request. The behavior is correct; it is like MS Word does. If you try to insert a long word into the cell then cell width will be expanded. For example try the following:
Forgive me for insisting, but I want the cell to “AutoFit to Contents”, not just based on a long word. In attachment, you’ll find the result from the previous code and the desired result.
I’d like to get the same result as when I right-click on the first column and select “Autofit to Contents” (in Word 2003)
Hi
Thank you for additional information. Unfortunately, there is no way to auto-fit cell to content. You can try to calculate width of cell and set in before inserting text.
Best regards.
Thanks for the information, but it worked as expected in Aspose.Words 4.4.3.0. I did not have to set anything for the first column width and it was “Autofit to Contents”
Hi
Thank you for additional information and sorry for inconvenience. Now I can see the problem. I logged this in our defect database as issue #7349. I will notify you as soon as it is fixed.
Best regards.
Thanks for your request. Unfortunately, the issue still unresolved. Currently I cannot provide you any reliable estimate regarding this issue.
Best regards,