We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

TopLeftCell and TopLeftCell have incorrect values for shading using C#

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.

@dstj

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi @tahir.manzoor,

There you go. I’m almost there (by manually copying every possible properties; there should be a Aspose Helper to do that).

In my example, you’ll that the top left cell is not in the proper style. That’s because I need a way a way to check if tableStyle.ConditionalStyles.TopLeftCell is defined in the TableStyle used…

AsposeWordsCopyTableCellStyle.zip (2.1 KB)

@dstj

Thanks for sharing the detail. Please also share your expected output Word document. We will investigate the issue and provide you more information on it.

Like the 2nd table in the following document.
aspose-copy-table-cell-style.zip (23.4 KB)

But again, my question are

  1. “how to fully copy the tableStyle.ConditionalStyles.FirstRow to the second row? Do I have to do it manually property by property or do you have a helper?”

  2. How can I determine if a particular tableStyle.ConditionalStyles._____ has been defined by the document template I’m using? I tried:

if (tableStyle.ConditionalStyles.TopLeftCell != null) {...}

but that is never NULL it appears even it not defined by the user who created the word style. I would like a ConditionalStyles.IsDefined(TopLeft) or maybe ConditionalStyles.TopLeftCell == ConditionalStyle.UndefinedStyle

Thanks.

@dstj

We are investigating this issue and will get back to you soon.

@dstj

We have logged this problem in our issue tracking system as WORDSNET-20306. You will be notified via this forum thread once this issue is resolved.

We have logged this feature request as WORDSNET-20307 in our issue tracking system. You will be notified via this forum thread once this feature is available.

We apologize for your inconvenience.

@tahir.manzoor
Thanks for the follow-up.

What about that question?

@dstj

Aspose.Words does not provide API to clone/copy ConditionalStyles.FirstRow style and apply it to another row of table. Could you please ZIP and attach your input and expected output documents for this requirement? We will then log this feature in our issue tracking system.

It is not to fix a bug, it’s to avoid writing and maintaining this long code to apply a ConditionalStyle to a cell:

private static void ApplySameCellStyle(Cell cell, ConditionalStyle conditionalStyle)
{
	ApplySameBorderCollection(conditionalStyle, cell.CellFormat.Borders);

	ApplySameShading(conditionalStyle.Shading, cell.CellFormat.Shading);

	foreach (var paragraph in cell.Paragraphs.Cast<Paragraph>()) {
		foreach (var run in paragraph.Runs.Cast<Run>()) {
			var dest = run.Font;
			var src = conditionalStyle.Font;
			ApplySameFontStyle(src, dest);
		}
	}
}

private static void ApplySameBorderCollection(ConditionalStyle src, BorderCollection dest)
{
	dest.Color = src.Borders.Color;
	dest.Shadow = src.Borders.Shadow;
	dest.LineStyle = src.Borders.LineStyle;
	dest.LineWidth = src.Borders.LineWidth;
	dest.DistanceFromText = src.Borders.DistanceFromText;

	ApplySameBorder(src.Borders.Top, dest.Top);
	ApplySameBorder(src.Borders.Bottom, dest.Bottom);
	ApplySameBorder(src.Borders.Left, dest.Left);
	ApplySameBorder(src.Borders.Right, dest.Right);
}

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;
}

private static void ApplySameShading(Shading src, Shading dest)
{
	dest.BackgroundPatternColor = src.BackgroundPatternColor;
	dest.ForegroundPatternColor = src.ForegroundPatternColor;
	dest.Texture = src.Texture;
}

private static void ApplySameFontStyle(Font src, Font dest)
{
	dest.AllCaps = src.AllCaps;
	// dest.AutoColor = src.AutoColor;
	dest.Bidi = src.Bidi;
	dest.Bold = src.Bold;
	dest.BoldBi = src.BoldBi;
	ApplySameBorder(dest.Border, src.Border);
	dest.Color = src.Color;
	dest.ComplexScript = src.ComplexScript;
	dest.DoubleStrikeThrough = src.DoubleStrikeThrough;
	dest.Emboss = src.Emboss;
	dest.Engrave = src.Engrave;
	dest.Hidden = src.Hidden;
	dest.HighlightColor = src.HighlightColor;
	dest.Italic = src.Italic;
	dest.ItalicBi = src.ItalicBi;
	dest.Kerning = src.Kerning;
	// dest.LineSpacing = src.LineSpacing;
	dest.LocaleId = src.LocaleId;
	dest.LocaleIdBi = src.LocaleIdBi;
	dest.LocaleIdFarEast = src.LocaleIdFarEast;
	dest.Name = src.Name;
	dest.NameAscii = src.NameAscii;
	dest.NameBi = src.NameBi;
	dest.NameFarEast = src.NameFarEast;
	dest.NameOther = src.NameOther;
	dest.NoProofing = src.NoProofing;
	dest.Outline = src.Outline;
	dest.Position = src.Position;
	dest.Scaling = src.Scaling;
	ApplySameShading(src.Shading, dest.Shading);
	dest.Shadow = src.Shadow;
	dest.Size = src.Size;
	dest.SizeBi = src.SizeBi;
	dest.SmallCaps = src.SmallCaps;
	// dest.SnapToGrid = src.SnapToGrid;
	dest.Spacing = src.Spacing;
	dest.StrikeThrough = src.StrikeThrough;
	dest.Style = src.Style;
	dest.StyleIdentifier = src.StyleIdentifier;
	dest.StyleName = src.StyleName;
	dest.Subscript = src.Subscript;
	dest.Superscript = src.Superscript;
	dest.TextEffect = src.TextEffect;
	dest.Underline = src.Underline;
	dest.UnderlineColor = src.UnderlineColor;
}

@dstj

Thanks for sharing the additional information. We have logged it in our issue tracking system. We will inform you once there is an update available on it.