Table formatting - column width, cell borders, row banding

Posted by: david.sheppard on 1 August 2023, 10:32 am EST

    • Post Options:
    • Link

    Posted 1 August 2023, 10:32 am EST

    I have been searching the documentation and cannot find how to set many of the properties for a table.

    Column width - I would like to set some of the columns in a table to specific widths and set other columns to auto size.

    Cell borders - I can see the properties for cell borders, but I cannot set the borders.

    Row banding - I want to have a gray shading on alternate rows. I cannot see how to set the background colors or how to set the alternating row count to 2. I do not want to use the built in styles in word.

  • Posted 2 August 2023, 6:51 am EST

    Hi,

    Thanks for reaching out to us with your query.

    1. We are discussing this requirement with the development team. Will get back to you once we have any update from them.[Internal Tracking Id - DOC-5649]

    2. You can set the borders for Table as:

    var ts1 = doc.Styles.Add("Table Style 1", StyleType.Table);
    // And assign the style to the table:
    t.Style = ts1;
    
    // Set up borders:
    foreach (var border in ts1.Table.Borders)
    {
        border.LineStyle = LineStyle.Double;
        border.LineWidth = 02f;
        border.Color.RGB = Color.DarkGray;
    }
    1. You can achieve this requirement by setting RowStripe and Table.Conditionals of Table Style.
    ts1.Table.RowStripe = 2;
    // Even rows' style:
    ts1.Table.Conditionals[TableStyleOverride.Band2Horizontal].Shading.Texture = TexturePattern.Clear;
    ts1.Table.Conditionals[TableStyleOverride.Band2Horizontal].Shading.BackgroundPatternColor.RGB = Color.Gray;

    Please refer the attached sample for the same: WordDemo.zip

    Best Regards,

    Nitin

  • Posted 2 August 2023, 9:12 am EST

    Thank you for your response. The solution worked as advertised.

    For table column widths, if the development team responds that it is not possible to define widths for some columns and autofit for other columns, is it possible to define column widths for all columns?

  • Posted 3 August 2023, 2:41 am EST

    Hi,

    Thanks for the acknowledgment.

    Could you please provide a sample document where you achieved Column-Width and AutoWidth Column in MS Word?

    Regards,

    Nitin

  • Posted 3 August 2023, 6:08 am EST

    The attached word document has a table with autosize and fixed width columns.

    The table width is autosize.

    The table has five columns. The first column has an autosize width. The remaining columns have a defined widthsTable with autosize and fixed width columns.zip

  • Posted 3 August 2023, 6:12 am EST

    Hi,

    Thanks for the document sample. We have shared this with the development team and will let you know once they respond.

    regards,

    Nitin

  • Posted 4 August 2023, 12:55 am EST

    Hi,

    You can achieve this requirement with below code:

    // iterate columns
    for (int c = 0; c < table.Rows[0].Cells.Count; c++)
    {
        // iterate cells in the current column
        for (int r = 0; r < table.Rows.Count; r++)
        {
            // set preferred width for each cell according to its column
            PreferredWidth width = table[r, c].Format.PreferredWidth;
            switch (c)
            {
                case 0:
                    // set auto width for the first column cells
                    width.Type = PreferredWidthType.Auto;
                    width.Value = 0;
                    break;
                case 1:
                    width.Type = PreferredWidthType.Points;
                    width.Value = 54;
                    break;
                case 2:
                    width.Type = PreferredWidthType.Points;
                    width.Value = 64.8f;
                    break;
                case 3:
                    width.Type = PreferredWidthType.Points;
                    width.Value = 57.6f;
                    break;
                case 4:
                    width.Type = PreferredWidthType.Points;
                    width.Value = 55.65f;
                    break;
            }
        }
    }

    Please refer the sample for the full implementation: TableAutoWidth.zip

    Best Regards,

    Nitin

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels