Validate Rich Text Structure RTF
Asked Answered
N

1

7

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 "}".

Nagey answered 1/10, 2012 at 21:24 Comment(0)
E
2

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.

Evin answered 1/10, 2012 at 21:56 Comment(3)
I believe a conversion utility was used to convert from something to PST. I have asked the service provider who did the conversion what format the source emails were in and what conversion tool was used.Nagey
If I were to load the RTF into an RTF control, would it tell me whether the RTF is valid?Nagey
I have accepted the answer even though it did not solve my particular issue. If the RTF is damaged severely enough, then setting setting RichTextBox.Rtf equal to the string results in "ArgumentException was unhandled", "File format is not valid." What I need is something more strict - that errors on even minor errors.Nagey

© 2022 - 2024 — McMap. All rights reserved.