views:

53

answers:

3

Hello,

Does anyone know of a way to store arbitrary data in a RichTextBox without the user being able to see this data? The 2007 RTF specification includes annotations ("\atnid", "\atnauthor", "\annotation", etc.) but whenever I insert these into a RichTextBox's .Rtf, the annotations disappear (presumably because the RichTextBox doesn't support RTF annotations.) I have a related question about whether it is possible to store the information inside a Metafile image. Either of these solutions would be acceptable. TIA.

What I'm trying is something like this:

string objectXml = MySerialization.ToXml(object);
string commentRtfFragment = String.Format(@"{{\*\atnid MyApp}}{{\*\atnauthor MyApp}}{{\*\annotation {0}}}", objectXml);

string imageRtf = String.Format(@"{{\rtf1 {{\pict\wmetafile{0}\picw{1}\pich{2}\picwgoal{3}\pichgoal{4} {5}}}{6}}}",
    PixelMappingMode.MM_ANISOTROPIC, picw, pich, picwgoal, pichgoal, imageHex, commentRtfFragment);
richTextBox.SelectedRtf = imageRtf;

Update: The application metadata ("annotations") must correspond with particular locations in the RTF. There will also be multiple annotations per RichTextBox (or RTF document if you like.) I also want the metadata to persist with the RTF. So while it would be possible to persist the metadata in a control.Tag, then I would have to take care of adding the information to the database myself, noting whenever the user edited the RTF and somehow determine the new location of the metadata after the edit.

A: 

I don't know if there's any special way for doing this for RTF documents, but if you just want to store some data in a control (any kind of Control) without showing it to the user, you could use the Tag property as can be seen here: Control.Tag

ho1
Unfortunately I require more than one annotation per RichTextBox control, and I want the results to persist to a database that is storing the richtext. Also the annotations must match up with a particular location in the RTF, and storing the information in the control's tag would not allow this, I don't think. ty though.
Carl
@Carl: Well, you could store the whole RTF in the `Tag`, not just the annotations. It would of course be duplication of data since the non annotation data would be both in the `Tag` and in the "view" but at least you'd still have the unadulterated data.
ho1
But then if the user edited the RTF I would not know where the metadata was "located" within the RTF. I am using the metadata to correspond to a particular location in the RTF (i.e., CitationField is here, and these are its characteristics.) If I stored the RTF in the tag but then the user edited the RTF in the control, I would have no way to know "well where was the CitationField again?" I (think I) need a way to insert something into the RTF so that even across RTF edits the metadata has the correct location.
Carl
@Carl: Forgot about that you might want to let the user edit the text... Might be some commerical control that lets you do it, though the only one I've got access to (XtraRichEdit by DevExpress) has an open feature request (http://www.devexpress.com/Support/Center/p/S35106.aspx) where they say they're going to add it, so I assume it's not in there yet. Might be worth taking a look at Telerik and ComponentOne etc
ho1
@ho1 okay thanks for the recommendations I'll check them out.
Carl
A: 

I think ho1 has the right idea. Control.Tag is an object so you could use a generic data structure like List, Hash, Dictionary, etc. to store your multiple annotations and store that in the Tag property.

dprice
I think the idea is to save app data with the RTF so that he can persist state with the document.
Ron Warholic
@Ron yes, I want the metadata to persist with the RTF. I also need the metadata to correspond to the particular RTF location at which it was originally inserted, even if the user performs an RTF edit that moves that location around. Think of it as an invisible Word Field, if you are familiar with those. I want to store some data at a particular location, and have that location flow with the RTF across user edits.
Carl
A: 

The richtext control supports hidden words with \v and turns hidden off with \v0 and no, I have not confused them even though logically \v would stand for visible it does the opposite.

atandb