如何从C#更改PowerPoint中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(); 

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

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

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

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

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

用于PPTX 2007

  private int BGR(Color color) { // PowerPoint's color codes seem to be reversed (ie, 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; } 

例如

  shape.TextFrame.TextRange.Font.Color.RGB = BGR(Color.Red); 

我认为这个MSDN页面解释了它。

编辑:但这只解释了如何在VBScript中做到这一点。 您可以看到TextRange对象具有属性Font 。 这将返回此处描述的Font对象。这些资源显示您可以访问RGB属性。 你可以像Cody告诉你的那样设置它。 如果您需要更多信息,请参阅我刚刚指出的MSDN部分。

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