Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the font color of a TextRange in PowerPoint from C#?

I created a PowerPoint presentation using C#:

PowerPoint.Application powerpointApplication;
PowerPoint.Presentation pptPresentation;
PowerPoint.Slide Slide;

// Create an instance of PowerPoint.
powerpointApplication = new PowerPoint.ApplicationClass();

// Create a PowerPoint presentation.
pptPresentation = powerpointApplication.Presentations.Add(
Microsoft.Office.Core.MsoTriState.msoTrue);


// Create empty slide
Slide = pptPresentation.Slides.Add(1, PowerPoint.PpSlideLayout.ppLayoutBlank);

TextRange objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
objTextRng.Text = "Remote sensing calendar 1";
objTextRng.Font.Name = "Comic Sans MS";
objTextRng.Font.Size = 48;
// TODO: change color
// objTextRng.Font.Color 



// Save presentation
pptPresentation.SaveAs( BasePath + "result\\2_example.ppt", 
                       PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                       MsoTriState.msoTrue // TODO: что за параметр???
                      );
pptPresentation.Close();

Now, how can I change the font color for objTextRng?

like image 883
pesten Avatar asked Mar 09 '11 14:03

pesten


2 Answers

The following code will set the font color to red:

objTextRng.Font.Color.RGB = Color.Red.ToArgb();

If you want to specify a different color, you can use one of the other pre-defined colors, or specify your own RGB values using the Color.FromArgb method.

Either way, make sure that you call the ToArgb method on the Color object that you use. The RGB property requires that an RGB color value be specified.

like image 124
Cody Gray Avatar answered Nov 02 '22 04:11

Cody Gray


Use this for PPTX 2007

    private int BGR(Color color)
    {
        // PowerPoint's color codes seem to be reversed (i.e., BGR) not RGB
        //      0x0000FF    produces RED not BLUE
        //      0xFF0000    produces BLUE not RED
        // so we have to produce the color "in reverse"

        int iColor = color.R + 0xFF * color.G + 0xFFFF * color.B;

        return iColor;
    }

for example

    shape.TextFrame.TextRange.Font.Color.RGB = BGR(Color.Red);  
like image 38
VVX Avatar answered Nov 02 '22 05:11

VVX