Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Rich Text Structure RTF

Tags:

mapi

richtext

rtf

MS Exchange / Outlook messages store data using MAPI. One common MAPI property contains a rich text version of the message's body (0x1009, PR_RTF_COMPRESSED, PidTagRtfCompressed). If the rich text string has an invalid structure, then Outlook 2003 and earlier fail to display any body content.

For instance, this RTF code omits a closing "}".

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 asdfasdf\par

The correct version is

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 asdfasdf\par
}

Are there .NET methods or libraries that I can use to test whether RTF code is valid? If not, I am open to C++ and Java or COM libraries. If not, are there applications that report irregularities in RTF strings?

The ideal solution would report the line numbers containing irregularities. An adequate solution would report whether the RTF as a whole is valid and well formed.

This issue is relevant outside of MAPI. For instance, if the invalid RTF string specified above is written to a .RTF file, it opens correctly in WordPad 5.1, but Word 2007 reports an error and requests that I repair the file.


One suggestion was that I stream the RTF into a rich text box. I tried this code:

private void button1_Click(object sender, EventArgs e)
        {
            string aaa = richTextBox1.Rtf;
            richTextBox1.Rtf = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\f0\fs20 asdfasdf\par}";
            richTextBox1.Refresh();
        }

Whether richTextBox1.Rtf ends with "}" or not, the rich text box displays this as its contents: asdfasdf

The solution I am looking for would report an error when I omit the last "}".

like image 338
Jacob Quisenberry Avatar asked Oct 01 '12 21:10

Jacob Quisenberry


1 Answers

Why not load the RTF into a hidden RTF control and then stream it out? What exactly would set an invalid RTF? I don't think Outlook ever does that.

like image 52
Dmitry Streblechenko Avatar answered Dec 27 '22 09:12

Dmitry Streblechenko