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

Free Support Forum - aspose.com

How to set table cell color using Aspose.Slides (C# .NET)

I can’t seem to find the code to set the color for a selected cell in a table using c# .net. I’ve created an example template that I open, it has a table in one of the slides which I can access and update. However I can’t seem to find the correct command to set an RGB color for a given cell. I’ve tried the following:

using Aspose.Slides;

//opening the presentation
string presLocation = “test presentation.pptx”;
Aspose.Slides.Presentation pres = new Aspose.Slides.Presentation(presLocation);

//selecting the slide
sld = pres.Slides[2];

//selecting the table
Aspose.Slides.Table table = (Aspose.Slides.Table)sld.FindShapeByAltText(“table”);

//updating text for col 0, row 1 particular cell in the table
table[0, 1].TextFrame.Text = “updated 1”;

//changing the text color for a col 1 row 1 cell to red
Portion portion = (Portion)table[1, 1].TextFrame.Paragraphs[0].Portions[0];
portion.PortionFormat.FillFormat.FillType = FillType.Solid;
portion.PortionFormat.FillFormat.SolidFillColor.Color = Color.Red;

//attempting to set col 4 row 1 to red
table[4, 1].FillFormat.FillType = FillType.Solid;
table[4, 1].FillFormat.SolidFillColor.ColorType = ColorType.RGB;
table[4, 1].FillFormat.SolidFillColor.Color = Color.FromArgb(250,0,0);

//save presentation
pres.Save(“test presentation aspose.pptx”, Aspose.Slides.Export.SaveFormat.Pptx);

I’ve looked through the forum and with some of the other languages there seems to be methods that I don’t have access to in C# (for example to set the ForColor). Perhaps I need to add some further refrences, etc… or I’m looking in the wrong place. Thanks for your help.

@kiarashmahdavi,

Please try using following sample code on your end.

public static void TestTableFill()
{
    // Instantiate Presentation class object
    Presentation presentation = new Presentation();

    // Access first slide
    ISlide islide = presentation.Slides[0];

    // Define columns with widths and rows with heights
    double[] dblCols = { 150, 150, 150, 150 };
    double[] dblRows = { 100, 100, 100, 100, 90 };

    // Add table shape to slide
    ITable tbl = islide.Shapes.AddTable(50, 50, dblCols, dblRows);

    // Add image to first table cell
    tbl[0, 0].FillFormat.FillType = FillType.Solid;
    tbl[0, 0].FillFormat.SolidFillColor.Color = Color.Red;
    // Save PPTX to Disk
    presentation.Save( "C:\\Aspose Data\\Color_Fill_TableCell_out.pptx", Aspose.Slides.Export.SaveFormat.Pptx);
}