Table background color

Hi Team,

I’m using AsposeWordsForJava v13.5.

Is there a method to set background color to the entire table? For eg, set table background color to blue and for few cells, set background color to red.

The method I use to set cell background color is
docBuilder.getCellFormat().getShading().setBackgroundPatternColor(new Color(Integer.parseInt(“808080”, 16)));

Thanks,
Kumar

Hi Kumar,


Thanks for your inquiry. You can set table background color by using the following code:
Document doc = new Document(“C:\Temp\input.docx”);
Table tab = doc.getFirstSection().getBody().getTables().get(0);
tab.setShading(TextureIndex.TEXTURE_NONE, Color.BLUE, Color.BLUE);
tab.getFirstRow().getLastCell().getCellFormat().getShading().setBackgroundPatternColor(Color.red);
doc.save(“C:\Temp\out.docx”);
I hope, this helps.

Best regards,

Hi Awais,

Thanks for the sample code. But I doubt if it suits my needs.
For eg: I have a table with single row and 3 cells. I want to set table color as blue. At the same time, I want to override 2nd cell color to red. In the final table, 1st and 3rd cell color should be blue and 2nd cell color should be red.
It doesn’t seem to work.

private static void writeTableWithBackgroundColor(DocumentBuilder docBuilder)
{
Table table = docBuilder.startTable();
table.setShading(TextureIndex.TEXTURE_NONE, Color.BLUE, Color.BLUE);
docBuilder.getCellFormat().getBorders().setLineWidth(1);

docBuilder.insertCell();
docBuilder.write(“Table bg color blue”);

docBuilder.insertCell();
docBuilder.getCellFormat().getShading().setBackgroundPatternColor(Color.RED);
docBuilder.write(“cell bg color red”);

docBuilder.insertCell();
docBuilder.write(“cell bg color should be blue”);

docBuilder.endRow();
}

Thanks,
Kumar

Hi Kumar,


Thank for your inquiry. Please use the following code to achieve this:
Document doc = new Document();
DocumentBuilder docBuilder = new DocumentBuilder(doc);

Table table = docBuilder.startTable();
docBuilder.getCellFormat().getBorders().setLineWidth(1);

docBuilder.insertCell();
docBuilder.write(“Table bg color blue”);

Cell cell2 = docBuilder.insertCell();
docBuilder.write(“cell bg color red”);

docBuilder.insertCell();
docBuilder.write(“cell bg color should be blue”);

docBuilder.endRow();

docBuilder.endTable();

table.setShading(TextureIndex.TEXTURE_NONE, Color.BLUE, Color.BLUE);
cell2.getCellFormat().getShading().setBackgroundPatternColor(Color.red);

doc.save(“C:\Temp\out.docx”);

Best regards,

Hi Awais,

Thanks. I get this now. It means that one has to set table bg color after the end of table creation and the cell bg color code should follow, right? which means, I cannot set cell bg color right after cell creation along with table bg color.

Thanks,
Kumar

And also, is there an API to set bg color for a row or should it be done individually for all cells in a row?

Hi Kumar,


Thanks for your inquiry. Yes, in this case, this is good practice to first build the table structure then using Table.SetShading method specify shading on all cells in the table and then finally override individual cell background colors using Cell.CellFormat.Shading.BackgroundPatternColor property.

Secondly, I am afraid, there is no API to set background color of a row; instead, please iterate through all cells in a row and specify background color using Cell.CellFormat.Shading.BackgroundPatternColor property.

Best regards,

Thanks Awais for the answers and tips.