2015-11-03 53 views
2

我試圖以編程方式將內容插入Powerpoint的Notes頁面。同一行上會有不同的彩色文字。我只通過段落得到它,但這樣做會使它們出現在不同的路線上。VSTO 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"; 

的2個字出來的不同顏色,但我想有他們在同一行。

我曾嘗試通過TextRange(TextRange.Font.Color)設置顏色,但是這似乎是隻讀屬性。

Picture showing what I wish to achieve

回答

0

所以我花了很多時間公平位在此尋找一個更優雅的解決方案.....但是這是我所發現的最佳.... 這是假設你有您的文本格式爲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(); 
    } 
} 
相關問題