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,
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”);
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,
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”);
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 Awais for the answers and tips.