Search code examples
c#uwppowerpointvsto

How can I programmatically insert RTF text into a PowerPoint text frame using C#?


Is there any way to programmatically insert RTF-formatted text into a PowerPoint TextFrame?

string myString = <some RTF formatted text>;

MyPlaceholder.TextFrame.TextRange.Text = myString;

This answer gets me close: Is it possible to insert pieces of RTF text into a Word document (.docx) using OpenXml?

But I am having trouble figuring out how to do this in PowerPoint rather than Word.

What I am trying to do is copy text from a RichEditBox in my application onto a PowerPoint slide and maintain font-color, format, etc. I am able to successfully bring the text into PowerPoint. I just can't bring in the formatting (such as font-color) with it.

Thanks.


Solution

  • I was finally able to accomplish this by saving from the RichEditBox to a stream using the following line of code and then writing to a file:

    tempRichEditBox.Document.SaveToStream(Windows.UI.Text.TextGetOptions.FormatRtf, randAccStream);
    

    I then used this line to import the file into PowerPoint. The font color and formatting are maintained.

    var rtfShape = PptSlide.Shapes.AddOLEObject(FileName: rtfFileName);