Ask a questionAsk a question
 

AnswerProgrammatically edit RTF files

  • Monday, August 24, 2009 12:48 PMm_schilder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi!

    I need a way to programmatically insert custom strings to RTF files/contents generated with Outlook 2007/Word with .NET.
    Since these files contain huge amounts of formatting code, it isn't possible to throw in a string without regarding the RTF-specification.
    Is there a possibility to do so without parsing the whole RTF-structure myself?

    Thank you!

Answers

All Replies

  • Monday, August 24, 2009 3:32 PMTom Jebo_DSCMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi m_schilder,

    The RTF specification can be downloaded here.  RTF is not part of the Open Protocol Specifications which can be found at Open Protocol Specifications

     and which are supported by this forum.  Having said that, the answer to your question is that your code must have an understanding of the RTF format in order to modify a document.  You might consider, however, using the RichTextBox control to do your insertion to the document contents.  If you search on "RichTextBox" on developer forums or code repositories like The Code Project, you'll find examples and explanation of how to use the control.  Here's an example of such an article that should get you started. 

    I hope this helps you.
     


    Regards, Tom Jebo Senior Support Escalation Engineer Microsoft DS Protocol Team
  • Tuesday, August 25, 2009 10:33 AMm_schilder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Tom Jebo_DSC,

    thank you very much and sorry for posting to the false forum.
    I've played a little bit with the RichTextBox-Control, resulting in the following code:


    System.Windows.Forms.RichTextBox rtbox = new RichTextBox();

    // filling my existing RTF-code to the corresponding property of the RichTextBox-control
    rtbox.Rtf = item.RTFBody;

    // inserting my custom string without caring for the RTF-formatting-tags
    rtbox.Text = rtbox.Text.Insert(0, defaultString_start + mailItem.CreationTime + defaultString_end + "\r\n\r\n");

    // writing back the modified RTF-code
    item.RTFBody = rtbox.Rtf;

    Unfortunately, this results in a loss of content/formatting.
    It seems the RichTextBox-control removes the formatting-tags it is not able to parse/display (in my case, I've 'lost' a horizonal line and the whole content following it).

    Do you have an idea, or are you able to move this thread to the appropriate forum section?

    Thank you!

  • Tuesday, August 25, 2009 5:35 PMObaid FarooqiMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi m_schilder:
    As Tom mentioned, RTF is not part of Open Protocol Specification. We would appreciate it if you
    could try a C# or .Net forum like

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/threads


    instead to find the information you are looking for.

    Regards,
    Obaid Farooqi 








    Regards, Obaid Farooqi