We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Changing the background and font color of a cell in a table

Hello support,


I would like to create a table on a slide and vary the font color and background cell color for each cell. I have tried several different ways to do this, but none of them seems to produce the desired results. Can you please provide an example of the calls to change both the font color and background color of a cell in a table.

// Change the background color
cell.

// Change the font color
cell.

Below is an example of my attempt to change the background color.

Best regards,

Kevin

Presentation pres = new Presentation();
Slide slide = pres.addEmptySlide();

try {

Table table = slide.getShapes().addTable(
0, //x
0, //y
5000, // width
600, // height
3, // numCols
4, //numRows
1, // border width
java.awt.Color.black // border color
);
for (int row = 0; row < table.getRowsNumber(); row++) {
for (int col = 0; col < table.getColumnsNumber(); col++) {
Cell cell = table.getCell(col, row);
cell.getFillFormat().setType(FillType.SOLID);
cell.getFillFormat().setBackColor(java.awt.Color.red);
}
}


Hi Kevin,

Thanks for your interest in Aspose.Slides.

You can set the both text font and background color of table cell. Please use the following code snippet that contains an example of table creation along with setting the background color of cells, border lines and text inside cell. Please share with us, if the problem still persists.

//Instantiate PresentationEx class that represents PPTX file
PresentationEx pres = new PresentationEx();
//Access first slide
SlideEx sld = pres.getSlides().get(0);
//Define columns with widths and rows with heights
double[] dblCols = { 50,50,50 };
double[] dblRows = { 50,30,30,30,30 };
//Add table shape to slide
int idx = sld.getShapes().addTable(100, 50, dblCols, dblRows);
TableEx tbl = (TableEx)sld.getShapes().get(idx);
java.awt.Color Color=new java.awt.Color(255,255,0);
java.awt.Color Color2=new java.awt.Color(0,0,255);

//Set border format for each cell

for( int i=0;i < tbl.getRows().size();i++ )

for( int j=0;j< tbl.getColumns().size();j++ )
{

CellEx cell= tbl.get(j, i);
cell.getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getFillFormat().getSolidFillColor().setColor(Color2 );

cell.getBorderTop().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderTop().getFillFormat().getSolidFillColor().setColor(Color );

cell.getBorderTop().setWidth(5);

cell.getBorderBottom().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderBottom().getFillFormat().getSolidFillColor().setColor(Color);
cell.getBorderBottom().setWidth(5);
cell.getBorderRight().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderRight().getFillFormat().getSolidFillColor().setColor(Color);
cell.getBorderRight().setWidth(5);
cell.getBorderLeft().getFillFormat().setFillType(FillTypeEx.SOLID);
cell.getBorderLeft().getFillFormat().getSolidFillColor().setColor(Color);
cell.getBorderLeft().setWidth(5);
}

java.awt.Color fColor=new java.awt.Color(0,255,0);

TextFrameEx tText=tbl.get(0,0).getTextFrame ();
tText.setText ("Hello World");
PortionEx por=tText.getParagraphs().get(0).getPortions().get(0);
//Setting font color
por.getFillFormat().setFillType(FillTypeEx.SOLID);
por.getFillFormat().getSolidFillColor().setColor(fColor);

//Write PPTX to Disk
pres.write("d:\\Aspose Data\\JavaData\\tableJ.pptx");

Thanks and Regards,