Free Support Forum -

Setting background color of a cell - Urgent

I am trying to apply color to the background of a cell in a table. When i give Color.Red or some color that is available it is applying the background color. But when i do it as below it is not applying the color. How can get it?

Aspose.Words.Document Doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder DocBuilder = new Aspose.Words.DocumentBuilder(Doc);
Aspose.Words.CellFormat CF = DocBuilder.CellFormat;
CF.Shading.BackgroundPatternColor = Color.FromName("#cccccc");

Try using Color.FromArgb method instead. For example

CF.Shading.BackgroundPatternColor = Color.FromArgb(0xCC, 0xCC, 0xCC);

So you mean to say Color.Fromname does not work. Isn't it? How can i get the red, blue, green values for a particular color of which i am storing hashcode in the table?

I don't know how you get other colors, but the description for Color.FromName in MSDN clearly implies that it will not work for strings like "#cccccc". It says a color with all zero ARGB components will be created. I don't think it is a good idea to use a function for a purpose that it was not designed for. So clearly you should not use Color.FromName.

In my opinion you need to parse the "cccccc" string as hexadecimal string into int and then use one of the Color.FromArgb overloads to create the color you want. To parse the string you can use int.Parse that takes some parameters that specify what format the string is in. You need to specify it is hexadecimal.

If you read the Color.FromName description in MSDN you will see that FromName accepts only a string that is the name of a pre-defined color. Valid names are the same as the names of the elements of the KnownColor enumeration.

To work with hex color code strings formatted in '#aabbcc' manner you can use ColorTranslator.FromHtml method. For example:

cell.CellFormat.Shading.BackgroundPatternColor = ColorTranslator.FromHtml("#cccccc");


Thank you. Its working.