Search code examples
c#pdfsharpmigradoc

How to format all Migradoc Table's Cells at once?


I'm creating a Table using Migradoc, and I want to set all cells shading color and cell borders.

At this point I'm only able to do it Cell by cell:

        var tableRow = table.AddRow();            
        table.Cells[0].Shading.Color = Colors.LightBlue;
        table.Cells[0].Format.LeftIndent = 1;
        table.Cells[0].Borders.Color = Colors.White;
        table.Cells[0].Borders.Width = 4;

        table.Cells[1].Shading.Color = Colors.LightBlue;
        table.Cells[1].Format.LeftIndent = 1;
        table.Cells[1].Borders.Color = Colors.White;
        table.Cells[1].Borders.Width = 4;

If it were inside a loop, it is not so painful, but all my rows are created one by one.

How can I set these properties to ALL CELLS in the table?


Solution

  • You can mix up Rows, Columns and Style to achieve what you want:

            // Create a new style called Table based on style Normal
            Style style = document.Styles.AddStyle("Table", "Normal"); //this is optional, you can have your own style :) 
            style.Font.Name = "Verdana";
            style.Font.Name = "Times New Roman";
            style.Font.Size = 9;
    
            Table table = new Table();
            table.Style = "Table";
            table.Rows.LeftIndent = -1; //to indent all the rows
            table.Columns.Width = 4; //width of all the columns
            table.Borders.Color = Colors.White; //color of all the border 
            table.Shading.Color = Colors.LightBlue; //all the cells table shading color