我正在尝试以编程方式将内容插入Powerpoint的“注释”页面。同一行上将有不同颜色的文本。我只是通过段落来使它起作用,但是这样做会使它们出现在不同的行上。
这是我尝试过的:
var slideRange = Application.ActivePresentation.Slides;
slideRange[1].NotesPage.Shapes[2].TextFrame.TextRange.Paragraphs(1).Font.Color.RGB = Color.Black;
slideRange[1].NotesPage.Shapes[2].TextFrame.TextRange.Paragraphs(1).Text = "word1\r";
slideRange[1].NotesPage.Shapes[2].TextFrame.TextRange.Paragraphs(2).Font.Color.RGB = Color.Gray;
slideRange[1].NotesPage.Shapes[2].TextFrame.TextRange.Paragraphs(2).Text = "word2\r";
这两个词用不同的颜色显示,但我希望它们在同一行上。
我尝试通过TextRange(TextRange.Font.Color)设置颜色,但是这似乎是只读属性。
Picture showing what I wish to achieve
最佳答案
因此,我在此上花费了很多时间,以寻找更优雅的解决方案.....但这是我所发现的最好的解决方案....
这假定您已将文本格式设置为HTML,但是如果您已经具有RTF格式,则有一个标记的部分可以开始。
var html = "<!DOCTYPE html><html><head></head><body><style type=\"text/css\">.SQLCode{font-size:13px;font-weight:bold;font-family:monospace;;white-space:pre;-o-tab-size:4;-moz-tab-size:4;-webkit-tab-size:4;}.SQLComment{color:#00AA00;}.SQLString{color:#AA0000;}.SQLFunction{color:#AA00AA;}.SQLKeyword{color:#0000AA;}.SQLOperator{color:#777777;}.SQLErrorHighlight{background-color:#FFFF00;}</style><pre class=\"SQLCode\"><span class=\"SQLComment\">--Example Commend</span><span class=\"SQLKeyword\">SELECT</span><span class=\"SQLKeyword\">TOP</span> 1 <span class=\"SQLFunction\">COALESCE</span><span class=\"SQLOperator\">(</span>ASPU<span class=\"SQLOperator\">.</span>MobileAlias<span class=\"SQLOperator\">,</span> ASPU<span class=\"SQLOperator\">.</span>UserName<span class=\"SQLOperator\">)</span><span class=\"SQLKeyword\">AS</span> UName<span class=\"SQLKeyword\">FROM</span> dbo<span class=\"SQLOperator\">.</span>aspnet_Users ASPU</pre></body></html>";
var title = "Header Text";
if (!string.IsNullOrWhiteSpace(html))
{
var web = new WebBrowser();
web.CreateControl();
web.DocumentText = html;
while (web.DocumentText != html)
{
System.Windows.Forms.Application.DoEvents();
}
web.Document.ExecCommand("SelectAll", false, null);
web.Document.ExecCommand("Copy", false,
//Start here if you already have it in RTF.
var rtf = Clipboard.GetData(DataFormats.Rtf) as string;
if (!string.IsNullOrWhiteSpace(rtf))
{
slide = ppt.Slides.AddSlide(ppt.Slides.Count + 1, ppt.SlideMaster.CustomLayouts[1]);
slide.Layout = PpSlideLayout.ppLayoutTextAndObject;
slide.Shapes.Title.TextFrame.TextRange.Text = title;
slide.Select();
slide.Shapes[2].Select();
Globals.ThisAddIn.Application.CommandBars.ExecuteMso("PasteSourceFormatting");
System.Windows.Forms.Application.DoEvents();
}
}
关于c# - VSTO Powerpoint注释页面-同一行上的不同颜色的单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33493983/