How to Set the Background Color of a Cell in a MigraDoc Table

Jeff G picture Jeff G · Apr 3, 2015 · Viewed 8.5k times · Source

I have a MigraDoc table where I specify a row height of 0.75cm, and the text is vertically-aligned in the middle of the cell. When I set cell.Format.Shading.Color to something non-white, there is still a portion of the cell near the border that is shown as white around all four sides.

I discovered I can remove the white section to the left and right of the text by setting column.LeftPadding = 0 and column.RightPadding = 0. However, I cannot figure out how to get the white stripes at the top/bottom of the text to disappear without affecting the vertical alignment of the text. If I change the paragraph line height to 0.75cm, the stripes disappear, but the text is then bottom-aligned within the cell. I cannot set the column shading color because each cell in the column contains a different color. Does anyone know a way to force the paragraph to fill the cell vertically (or otherwise get the background color to be uniform within the cell)?

Here is a sample of my code (in C#) where table is of type MigraDoc.DocumentObjectModel.Tables.Table:

...

// Add a column at index #2
var column = table.AddColumn();
column.LeftPadding  = 0;
column.RightPadding = 0;

// Add more columns
... 

// Iterate through the data printed in each row
foreach (var rowData in myData)
{
    // Create a row for the data
    var row = table.AddRow();
    row.Height = ".75cm";
    row.Format.Font.Size = 11;
    row.VerticalAlignment = VerticalAlignment.Center;

    ...

    // The following is for illustrative purposes... the actual
    //     colors and text is determined by the data within the cell
    var cell = row.Cells[2];
    cell.Format.Shading.Color = Colors.Black;
    cell.Format.Font.Color    = Colors.White;
    var paragraph = cell.AddParagraph("Example");

    ...
}

Answer

Try cell.Shading.Color instead of cell.Format.Shading.Color - the former sets the colour of the cell, the latter sets the colour of the text background (and the padding of the cell will then have a different colour).