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

梦想的初衷 提交于 2019-12-12 10:43:06

问题


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?


回答1:


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.




回答2:


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);  



回答3:


I think this MSDN page explain it.

EDIT: But this only explain how to do it in VBScript. You can see that the TextRange object have a property Font. This returns the Font object describe here These resources show you that you have access to a RGB property. You can set it like Cody told you. If you need further info, refer to the MSDN section I just point you.




回答4:


objTextRng.Font.Color.RGB = System.Drawing.ColorTranslator.ToOl(System.Drawing.Color.Blue);



来源:https://stackoverflow.com/questions/5247135/how-can-i-change-the-font-color-of-a-textrange-in-powerpoint-from-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!