如何从C#更改PowerPoint中TextRange的字体颜色?

pes*_*ten 6 .net c# powerpoint office-interop textrange

我使用C#创建了一个PowerPoint演示文稿:

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();
Run Code Online (Sandbox Code Playgroud)

现在,我该如何更改字体颜色objTextRng

Cod*_*ray 6

以下代码将字体颜色设置为红色:

objTextRng.Font.Color.RGB = Color.Red.ToArgb();
Run Code Online (Sandbox Code Playgroud)

如果要指定其他颜色,可以使用其他预定义颜色之一,或使用该Color.FromArgb方法指定自己的RGB值.

无论哪种方式,请确保在您使用的对象上调用ToArgb方法Color.该RGB属性要求指定RGB颜色值.


小智 5

用于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;
    }
Run Code Online (Sandbox Code Playgroud)

例如

    shape.TextFrame.TextRange.Font.Color.RGB = BGR(Color.Red);  
Run Code Online (Sandbox Code Playgroud)

  • 在 Powerpoint 2013 中似乎仍然如此(我猜它是相同的格式)。有点傻,当你将 `Color.RGB` 设置为红色时,如果没有这个功能,你会变成蓝色。:) (2认同)