How to set different colors or other attributes to different cells of a table in aspose.words

Hi!

I did something like this

Document doc = new Document ();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.startTable();

// row 1
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("row 1 col 1" );

builder.insertCell();

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

builder.writeln("row 1 col 2" );

builder.endRow();

// row 2
builder.insertCell();

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

builder.writeln("row 2 col 1" );

builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("row 2 col 2" );
builder.endRow();

builder.endTable();
doc.save("D:/abc.doc");

My question is

  1. How can I set different colors to this 4 different cells of this table? I tried the following way
builder.insertCell();
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.blue);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("row 1 col 1" );

But it not only makes the color of the cell (row 1 col 1) blue but also of the rest ones. I don’t want this. I want here only the upper left cell (i.e. row 1 col 1 ) to be blue.

Please help me as soon as possible. This will help me to find setting of other properties like font, style and others. For security purpose I am not sending my file.

Thanks.

Regards,
Md. Asfak Mahamud.

Please this is an urgent issue. I need it as soon as possible. Can anybody tell me whether it is possible in aspose.words or not?

In Microsoft words this is possible to set different properties in different cells of the same table.

Regards,
Asfak Mahamud.

It’s done. Thanks.

Asfak Mahamud.

Hi
Thanks for your request. You should just reset background color as shown in the following code snippet.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.startTable();
// row 1
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
// Set collor of current cell (collor should be reset when the next cell will be inserted)
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.blue);
builder.writeln("row 1 col 1");
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
// Reset background color (Color(0,0,0,0) is transparent in MS Word)
builder.getCellFormat().getShading().setBackgroundPatternColor(new Color(0, 0, 0, 0));
builder.writeln("row 1 col 2");
builder.endRow();
// row 2
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("row 2 col 1");
builder.insertCell();
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.writeln("row 2 col 2");
builder.endRow();
builder.endTable();
doc.save("C:\\Temp\\out.doc");

Hope this helps.
Best regards.

Thanks for the reply. I’ve already done it. Many many thanks again for your response.

Regards,
Asfak Mahamud.