Free Support Forum - aspose.com

Set the background color of the current row to 'light gray'

Hi,

Here is my main objective: I would like to set the background color of each cells in the respective row which has "Total" value. Usually we would set the background color of the "Total" row.

Apparently i can only able to set a particular cell. Do you have the code snippet to set the whole row ?

I am using the following code snippet

http://www.aspose.com/community/forums/permalink/229124/229124/showthread.aspx#229124

I have also made a sample based on the above thread in order to communicate my objective. Inside the attachment, you will find "Program.zip" which contains the sample code. There is an attachment called "word template.zip" which contains the templates that i am using.

Regards,

hadi teo

Hi Hadi,


Thanks for your inquiry. In your case, you need to get the first ancestor Row of Total field and change the background color of each cell. Please modify your FieldMerging method as follow:

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:blue;mso-font-kerning:
0pt;mso-no-proof:yes”>void<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> IFieldMergingCallback.FieldMerging(FieldMergingArgs args)<o:p></o:p>

{

string value = args.FieldValue.ToString();

DocumentBuilder builder = new DocumentBuilder(args.Document);

if (value.ToLower().Contains("total"))

{

builder.MoveToField(args.Field, true);

Row currentRow = (Row)builder.CurrentParagraph.GetAncestor(NodeType.Row);

if (currentRow != null)

{

foreach (Cell cell in currentRow)

cell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

}

//builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

}

}

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.


Hi Tahir,

Thanks for your assistance. The code is working actually. But unfortunately out of 5 cells, only 2 cells are shaded properly.

I have debugged it and actually the application detects 5 cells in the 'currentRow'.

Is there anything wrong with the 'in.doc' file itself ?

Regards,

hadi teo

Hi Hadi,


Thanks for your inquiry. I have cleared the shading color in your input document (in.doc) by using MS Word and have got the correct output by using same code snippet shared earlier.

You can use Shading.ClearFormatting method to remove shading from table’s cell. I have attached the output document with this post generated by using following FieldMerging method . Please modify your FieldMerging method as follow:

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:blue;mso-font-kerning:
0pt;mso-no-proof:yes”>void<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> IFieldMergingCallback.FieldMerging(FieldMergingArgs args)<o:p></o:p>

{

string value = args.FieldValue.ToString();

DocumentBuilder builder = new DocumentBuilder(args.Document);

if (value.ToLower().Contains("total"))

{

builder.MoveToField(args.Field, true);

Row currentRow = (Row)builder.CurrentParagraph.GetAncestor(NodeType.Row);

if (currentRow != null)

{

foreach (Cell cell in currentRow)

{

cell.CellFormat.Shading.ClearFormatting();

cell.CellFormat.Shading.BackgroundPatternColor = Color.LightGray;

}

}

}

}


Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi Tahir,

Thanks very much for your assistance. It's working well now with the changes that you have proposed.

Regards,

hadi teo

Hi Hadi,


Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.