VSTO Powerpoint Notes Page - Different colored words on same line

Deadly 提交于 2019-12-20 03:16:46

问题


I am trying to insert content into the Notes Page of Powerpoint programmatically. There will be different colored text on the same line. I have only gotten it to work via paragraphs, but doing so will make them appear on different lines.

This is what I have tried:

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

The 2 words come out in different colors but I would like to have them on the same line.

I have tried setting the color through the TextRange (TextRange.Font.Color), however this seems to be a read only property.

Picture showing what I wish to achieve


回答1:


So I've spent a fair bit of time on this looking for a more elegant solution..... but this is the best I have found.... This assumes that you have your text formatted in HTML, but there is a marked section to start if you already have a RTF format.

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


来源:https://stackoverflow.com/questions/33493983/vsto-powerpoint-notes-page-different-colored-words-on-same-line

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