Widths not working on Table

Widths on the tables is unpredictable.

My code:

builder.StartTable();
builder.CellFormat.Width = 10;
builder.InsertCell();
builder.Write("TEST");

builder.CellFormat.Width = 90;
builder.InsertCell();
builder.Write("TEST2");

builder.EndRow();

builder.CellFormat.Width = 10;
builder.InsertCell();
builder.Write("TEST");

builder.CellFormat.Width = 90;
builder.InsertCell();
builder.Write("TEST2");

builder.EndRow();

The problem is that the widths get to a certain point and then dont re-size properly. I basically want the minimum widths for the columns, based on the length of text inside the column. Is there another way of setting the widths?

Thanks.

Hi,

You should set cell width after you've inserted a cell. DocumentBuilder has an internal "pointer" which attaches to the current node so you must insert this node first.

Actually the code I wrote here, was incorrect. I actually did have the insert first. I find the widths to be unpredictable. If I set 1 column to 10 and the other to 90, what are the units? Pixels? Pct?

If I set the 2 columns to be 10 and 10, it seems to split the table 50% each column and take up the entire width of the document. Is there a way to get the table to be the minimum width based on the text inside the column (rather than forcing a width)?

Basically what I want is a table in Word as if I had set the column widths to "Auto Fit to Contents".

"Auto fit to contents" is set by DocumentBuilder.RowFormat.AllowAutoFit property.

The units that are taken by CellFormat.Width are points. There are 72 points in 1 inch. You can use WordConvert methods to convert between inches, pixels and points.

OK, I got that to work so long as all of my tables in the document are generated from code. If I had 1 table created within word, and then a table created by code, thats when things became a problem. Just an FYI. Not sure if I should be doing anything different to prevent this. In the meantime I will just create all table from my code.

If you are experiencing problem that gives you inconvenience or trouble, please send us the exact description of the problem together with document and code sample. It can be a bug to fix, or improper coding practice which could be corrected.

I'm experiencing the same problems.

builder.CellFormat.ClearFormatting()
' Header rij met vette koppen en onderlijnd

builder.Font.Bold = True
builder.CellFormat.Borders(Aspose.Words.BorderType.Bottom).LineStyle = LineStyle.Single
builder.StartTable()

builder.InsertCell()
builder.CellFormat.Width = 185.0
builder.Write("Naam")

builder.InsertCell()
builder.CellFormat.Width = 255.0
builder.Write("Praktijkkenmerken")

builder.EndRow()

' Overige regels gewoon
builder.Font.Bold = False
builder.CellFormat.ClearFormatting()
builder.CellFormat.Borders(Aspose.Words.BorderType.Bottom).ClearFormatting()

' Ophalen van de apotheken en deze weergeven inde gecreeerde tabel
Dim colPraktijken As New CollectionClasses.S2RelatiesPraktijkenHuisartsCollection
Dim praktijk As EntityClasses.S2RelatiesPraktijkenHuisartsEntity

For Each praktijk In colPraktijken
For x = 0 To 1
builder.InsertCell()
builder.CellFormat.ClearFormatting()
Select Case x
Case 0
builder.CellFormat.Width = 185.0
builder.Write(praktijk.Naam)
Case 1
builder.CellFormat.Width = 255.0
builder.Write(praktijk.Kenmerk)
End Select
Next

builder.EndRow()
builder.CellFormat.ClearFormatting()
Next

builder.EndTable()

When running this code the first time it shows perfectly. A second time it doesn't when filling another table with exactly the same formatting code but different data. And suprisingly a third time it works fine again. After some tables the total width of the table will always be the same.

I just cannot figure it out what is going wrong. Any suggestions are greatly appreciated.

Regards,

Willem Jan Kortleve