In trying to figure out how to get the exact bounding box of a MigraDoc Table cell (so that I can then do some manual PDFSharp drawing with XGraphics), I have found that table cell borders do not actually get rendered according to the Cell.Border definition. (I might be wrong though).
The following code just creates a simple MigraDoc Document, with a 10 x 10 table.
Each cell going from left to right, top to bottom will have a border color from an array of 4 colors.
I've added text to each cell so that its easier to reference them.
The output of the below code (after being rendered) produces the following:
The border shared between cell 0,0 and 0,1 is LightGreen, when it should (according to the code below) have green on the left and salmon on the right.
It looks like the previous cell's right hand border takes precedence over a cells left hand border.
Also the top and bottom borders extend into the previous cells right hand border.
Also there is an issue vertically, for example the 0,0 cell's bottom border takes precedence over the 1,0 cell's top border.
Code:
var document = new Document();
SetupStyles(document);
var section = document.AddSection();
section.PageSetup = document.DefaultPageSetup.Clone();
section.PageSetup.PageFormat = PageFormat.A4;
section.PageSetup.LeftMargin = section.PageSetup.RightMargin = Unit.FromCentimeter(1);
var table = section.AddTable();
table.Style = StyleNames.Normal;
table.Format.Alignment = ParagraphAlignment.Center;
table.Borders.Top.Style = table.Borders.Bottom.Style = table.Borders.Left.Style = table.Borders.Right.Style = BorderStyle.Single;
table.Borders.Top.Width = table.Borders.Bottom.Width = table.Borders.Left.Width = table.Borders.Right.Width = Unit.FromPoint(4);
Color[] colors = new Color[] { Colors.LightGreen, Colors.LightSalmon, Colors.LightGray, Colors.LightBlue };
int color_index = 0;
var width = section.PageSetup.PageWidth - (section.PageSetup.LeftMargin + section.PageSetup.RightMargin);
for (int c = 0; c < 10; c++)
{
table.AddColumn(width / 10);
}
for (int rowIndex = 0; rowIndex < 10; rowIndex++)
{
var row = table.AddRow();
for (int cellIndex = 0; cellIndex < 10; cellIndex++)
{
var color = colors[color_index]; //Give each cell its own border color
row.Cells[cellIndex].Borders.Top.Color = color;
row.Cells[cellIndex].Borders.Bottom.Color = color;
row.Cells[cellIndex].Borders.Left.Color = color;
row.Cells[cellIndex].Borders.Right.Color = color;
row.Cells[cellIndex].AddParagraph($"{rowIndex},{cellIndex}");
color_index++;
if (color_index > 3)
color_index = 0;
}
}
return document;