Hi,
I’m using TableStyleIdentifier
to set my header row style, but since I have merged cells spanning the first two rows, the first two rows are actually my real table header.
So, I would like to copy the tableStyle.ConditionalStyles.FirstRow
style to the second row.
Instead of this table:
image.png (3.2 KB)
I want to obtain something like this table:
image.png (2.6 KB)
How would I go about copying the whole row style, i.e. Borders, Shading, Font, everything?
I do not want to use _builder.Document.ExpandTableStylesToDirectFormatting();
if possible because I have many tables in my document and I want to keep the Table Style references as much as possible. (I would be ok with table.ExpandTableStylesToDirectFormatting()
though if that existed, it doesn’t.)
Right now, I’m trying something like this:
var cell = table.Rows[1].Cells[2]; // row 2, column 3, then next sibling, etc.
var tableStyle = (TableStyle) _builder.Document.Styles.First(x => x.StyleIdentifier == StyleIdentifier.GridTable4Accent1);
CopyCellStyle(cell, tableStyle.ConditionalStyles.FirstRow);
private void CopyCellStyle(Cell cell, ConditionalStyle conditionalStyle) {
foreach (var paragraph in cell.Paragraphs.Cast<Paragraph>()) {
foreach (var run in paragraph.Runs.Cast<Run>()) {
run.Font.Style = conditionalStyle.Font.Style;
run.Font.Color = conditionalStyle.Font.Color;
run.Font.Bold = conditionalStyle.Font.Bold;
run.Font.Italic = conditionalStyle.Font.Italic;
run.Font.Name = conditionalStyle.Font.Name;
run.Font.Size = conditionalStyle.Font.Size;
run.Font.Bidi = conditionalStyle.Font.Bidi;
}
}
cell.CellFormat.Shading.BackgroundPatternColor = conditionalStyle.Shading.BackgroundPatternColor;
cell.CellFormat.Shading.ForegroundPatternColor = conditionalStyle.Shading.ForegroundPatternColor;
ApplySameBorder(conditionalStyle.Borders.Top, cell.CellFormat.Borders.Top);
ApplySameBorder(conditionalStyle.Borders.Bottom, cell.CellFormat.Borders.Bottom);
ApplySameBorder(conditionalStyle.Borders.Left, cell.CellFormat.Borders.Left);
ApplySameBorder(conditionalStyle.Borders.Right, cell.CellFormat.Borders.Right);
// I'm missing properties, for sure...
}
private static void ApplySameBorder(Border src, Border dest)
{
dest.Color = src.Color;
dest.Shadow = src.Shadow;
dest.LineStyle = src.LineStyle;
dest.LineWidth = src.LineWidth;
dest.DistanceFromText = src.DistanceFromText;
}
But that seems that there should be a simpler / better way? Is there?
Thanks!
p.s. I’ve read this and this post, but they don’t copy everything, and not from the same document.