Setting Cell Width & PreferredWidth for Multiple Tables in Word Document using C#.NET | Clear Cell Formatting

Hi,

Please see the attached reproduction:
TableCellWidthAffects2ndTable.zip (39.8 KB)

I create 3 tables. The 1st has a fixed width for the last cell (with _builder.CellFormat.Width), and table 2 and 3 are created identical (also with some fixed cell widths and some auto-widths)

The 2nd row of the 2nd table differs from the 3rd table: the cell width are not aligned properly. The 3rd table (again, created by calling the same function a second time) is OK like so:

If I comment out the first table creation (the CreateFirstTableThatCausesProblemOn2ndTable(); function), the 2nd table is OK. If I remove the fixed cell width on the first table, the 2nd table is OK.

It really seems that using CellFormat.Width = ___ and _builder.CellFormat.PreferredWidth = PreferredWidth.Auto; somehow causes an issue on the very next table.

Can you confirm the bug or help me understand what I’m doing wrong?

Thanks.

@dstj,

We have logged this problem in our issue tracking system. Your ticket number is WORDSNET-21313. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for any inconvenience.

@awais.hafeez
More information about the issue: step by step debugging shows that calling _builder.CellFormat.PreferredWidth = PreferredWidth.Auto; or _builder.CellFormat.ClearFormatting() does not clear out the _builder.CellFormat.Width value (i.e. does not put it back to 0 which means “no value” I think). That can certainly be the (or one of the) cause of the problem.

@dstj,

We have logged these details in our issue tracking system and will keep you posted here on any further updates.

@dstj,

Regarding WORDSNET-21313, we have completed the work on this issue and concluded to close this issue with “not a bug” status. The analysis reveals the following.

You use the ‘DocumentBuilder.CellFormat.Width’ property to set the width of the cell.
You also use the ‘DocumentBuilder.CellFormat.ClearFormatting()’ method to reset the cell width to zero.
And you also use the ‘DocumentBuilder.CellFormat.PreferredWidth’ property to reset the cell width from a fixed value to ‘auto’.

But you get unexpected results:

The ‘DocumentBuilder.CellFormat.ClearFormatting()’ method doesn’t reset the cell width to zero.
The ‘DocumentBuilder.CellFormat.PreferredWidth’ property doesn’t reset the cell width from a fixed value to ‘auto’.

Please Check out the tooltips for these properties and mehotds.png (31.2 KB).

For the ‘DocumentBuilder.CellFormat.ClearFormatting()’ method, it is noticed that this method does not change the width of the cell.
And for the ‘DocumentBuilder.CellFormat.Width’ property, it is noticed: 1) setting this property is not recommended; 2) consider using PreferredWidth for setting the cell width; 3) setting Width property sets PreferredWidth implicitly.

So we have:

_builder.CellFormat.ClearFormatting();                           // does not change the cell width
_builder.CellFormat.Width = 0;                                   // implicitly PreferredWidth = 0;
_builder.CellFormat.PreferredWidth = Tables.PreferredWidth.Auto; // takes no effect to Width

Therefore the suggested solution is to use the ‘DocumentBuilder.CellFormat.PreferredWidth’ property like this:

const double PtPerCm = 72 / 2.54;

public static void Test21313()
{
    DocumentBuilder _builder = new DocumentBuilder();

    CreateFirstTableThatCausesProblemOn2ndTable(_builder);

    _builder.Writeln("The next table will have messed-up column widths on the 2nd row");
    CreateOtherTable(_builder);

    _builder.Writeln("The next table has correct column widths on the 2nd row");
    CreateOtherTable(_builder);

    _builder.Document.Save(@"X:\Test21313.docx");
}

private static void CreateFirstTableThatCausesProblemOn2ndTable(DocumentBuilder _builder)
{
    _builder.StartTable();

    _builder.InsertCell();
    _builder.InsertCell();
    _builder.InsertCell();
    _builder.CellFormat.PreferredWidth = Tables.PreferredWidth.FromPoints(4 * PtPerCm);
    _builder.Write("This cell has a fixed width");

    _builder.EndRow();
    _builder.EndTable();
}

private static void CreateOtherTable(DocumentBuilder _builder)
{
    const double width = 2 * PtPerCm;

    _builder.StartTable();

    _builder.InsertCell();
    _builder.CellFormat.PreferredWidth = Tables.PreferredWidth.Auto;
    _builder.Write("Header 1");
    _builder.InsertCell();
    _builder.Write("Header 2");
    _builder.InsertCell();
    _builder.Write("Header 3");
    _builder.InsertCell();
    _builder.Write("Header 4");
    _builder.InsertCell();
    _builder.Write("Header 5");
    _builder.InsertCell();
    _builder.CellFormat.PreferredWidth = Tables.PreferredWidth.FromPoints(width);
    _builder.Write("Fixed 6");
    _builder.InsertCell();
    _builder.Write("Fixed 7");
    _builder.InsertCell();
    _builder.Write("Fixed 8");
    _builder.InsertCell();
    _builder.Write("Fixed 9");
    _builder.EndRow();

    _builder.InsertCell();
    _builder.CellFormat.PreferredWidth = Tables.PreferredWidth.Auto;
    _builder.Write("1");
    _builder.InsertCell();
    _builder.Write("2");
    _builder.InsertCell();
    _builder.Write("3");
    _builder.InsertCell();
    _builder.Write("4");
    _builder.InsertCell();
    _builder.Write("5");
    _builder.InsertCell();
    _builder.CellFormat.PreferredWidth = Tables.PreferredWidth.FromPoints(width);
    _builder.Write("fixed 6");
    _builder.InsertCell();
    _builder.Write("fixed 7");
    _builder.InsertCell();
    _builder.Write("fixed 8");
    _builder.InsertCell();
    _builder.Write("fixed 9");
    _builder.EndRow();

    _builder.EndTable();
}

And we get the correct result (please see the Test21313.zip (6.9 KB) and WORDSNET-21313 Analysis 02.png (8.1 KB)).

1 Like