none
Display rtf string in word using c# RRS feed

  • Question

  • Hi guys,

     I am create a word document with plain text. I want to insert rtf string in that document (rtf string may contain images,math type equations...etc). what is way to display rtf string. I am inserting plain text like

     //Insert a paragraph at the beginning of the document.  
    Word.Paragraph oPara1;
    oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
     //Add Text
    oPara1.Range.Text = "Have a nice day.... ";
            

    anybody help me pls.

    Thanks in advance.

    Wednesday, June 27, 2012 9:24 AM

Answers

  • Hey hi i have another way to solve this thread.

    1) Insert rtf string in Richtextbox

    2)Richtextbox has saveFile() option => save it as .rtf file

    3)Now open this file in word doc like below code

    //Open another page here object missing = Missing.Value; object fullFilePath = @"F:\RtfFile.rtf"; object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; object noEncodingDialog = true;

    Word._Document myDoc = oWord.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);

    //Ends here

    Like this way is their is another way anybody knows...

    THe only other way I know of is to place the RTF on the Windows Clipboard then use the Paste method in Word's object model.

    Word requires a converter in order to use an RTF result in its documents. Converters are only triggered by opening or pasting content.


    Cindy Meister, VSTO/Word MVP

    Wednesday, July 4, 2012 8:49 AM
    Moderator

All replies

  • Hey hi i have another way to solve this thread.

    1) Insert rtf string in Richtextbox

    2)Richtextbox has saveFile() option => save it as .rtf file

    3)Now open this file in word doc like below code

    //Open another page here object missing = Missing.Value; object fullFilePath = @"F:\RtfFile.rtf"; object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; object noEncodingDialog = true;

    Word._Document myDoc = oWord.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);

    //Ends here

    Like this way is their is another way anybody knows...

    Wednesday, June 27, 2012 10:55 AM
  • Hi,

    Thanks for posting in the MSDN Forum.

    As far as I know, I would suggest you a common method of how to convert RTF to Plain Text with this:http://msdn.microsoft.com/en-us/library/cc488002.aspx

    Also, please see the following thread  for more ideas.http://social.msdn.microsoft.com/forums/en-US/vsto/thread/b4d0a038-473e-412a-9c0e-9ada979f0440

    Hope this can help you.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Friday, June 29, 2012 3:03 AM
    Moderator
  • Hi Leo_Gao,

    Thanks for reply as you suggest the link here. It's works only for plain Text in Richtextbox if their is any image or mathtype equation then this solution is not working.

    Monday, July 2, 2012 4:01 AM
  • Hey hi i have another way to solve this thread.

    1) Insert rtf string in Richtextbox

    2)Richtextbox has saveFile() option => save it as .rtf file

    3)Now open this file in word doc like below code

    //Open another page here object missing = Missing.Value; object fullFilePath = @"F:\RtfFile.rtf"; object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8; object noEncodingDialog = true;

    Word._Document myDoc = oWord.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);

    //Ends here

    Like this way is their is another way anybody knows...

    THe only other way I know of is to place the RTF on the Windows Clipboard then use the Paste method in Word's object model.

    Word requires a converter in order to use an RTF result in its documents. Converters are only triggered by opening or pasting content.


    Cindy Meister, VSTO/Word MVP

    Wednesday, July 4, 2012 8:49 AM
    Moderator
  • For future references, if anyone is interested I found alternative here:
    https://code.msdn.microsoft.com/Word-Document-Editor-in-d97fd70b
    In short it uses the following to insert the rtf string in the document:
    Paragraph oPara1 = new Paragraph(document);
    oPara1.Content.LoadText("here goes the rtf string"LoadOptions.RtfDefault);
    The sample uses this Word component for .NET that has an easy to use API for processing the RTF content in C#.
    Tuesday, December 16, 2014 10:53 AM