0

I am trying to display an RTF file (that was created in a legacy system) in a new WPF application and have run into some difficulty. The old RTF file contains a picture formatted as a binary jpegblip which, when read into the RichTextBox, causes this exception:

Unrecognized structure in data format 'Rich Text Format'. Parameter name: stream

I reduced the file down to the bare minimum to isolate the problem and ended up with the following opening line (the binary data has been removed for this post):

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip\bin9889

This still caused an exception so I converted the binary data to hex and created a new file with the opening line:

{\rtf1{\pict\picw2700\pich2700\picwgoal2700\pichgoal2700\jpegblip

The file with the hex data in it was displayed correctly by the control.

Has anyone been able to load a file containing binary picture data into the RichTexBox control, or failing that, is there a difinitive statement as to what the RichTextBox supports from the RTF specification?

2 Answers2

0

I'm not even sure Microsoft knows what RichTextBox implements. Their idea of RTF floats around untethered in space. See MS Word, Wordpad, the numerous legacy Rich Edit controls, etc.

This has some comments that talk about using the COM objects in your .NET code, which are somewhat predictable, but again Microsoft doesn't specify what parts of the spec they follow. Broken tables in RichTextBox control (word wrap) - you can use these in WPF if you wrap them properly.

If you just have to display the document, you might ask if all your users will have Word. Then you might be able to embed the Word document viewer, and presto, problem solved. It's an easy dependency to add if your software is used in an office.

I would probably read up on that format until I knew how it worked. Then in my program, I would read the RTF file into a buffer, grab the raw object data, translate it somehow, replace those objects with updated ones, then pass it to the control.

Community
  • 1
  • 1
djdanlib
  • 21,449
  • 1
  • 20
  • 29
  • Thanks for the link. Unfortunately having Word on all the client machines isn't an option (for viewing and editing), which is why RTF was used in the first place for our product. Looks like we're in a situtation where a conversion using Word automation may be required on those legacy products. – Mark Hiscocks Dec 01 '11 at 13:18
-1

For me, this code works fine. I can even pull it from a database table too.

<RichTextBox Height="100" HorizontalAlignment="Left" Margin="306,30,0,0" Name="rtfMain" VerticalAlignment="Top" Width="200" />


rtfMain.Selection.Load(new FileStream(@"C:\temp\document.rtf", FileMode.Open), DataFormats.Rtf);
Tom Stickel
  • 19,633
  • 6
  • 111
  • 113
  • This doesn't address the original question at all. The problem I described is with loading an RTF file with binary image data. – Mark Hiscocks Nov 08 '12 at 14:44
  • rtf is binary, regardless of whether it contains an image. I have tried both with an image and without and image in a rtf file which is binary. Perhaps my answer will help someone else. cheers – Tom Stickel Nov 09 '12 at 04:16
  • 1
    Tom, you are missing something here. The RTF specification allows for images to be encoded as binary (e.g. jpegblip\bin9889) or hex (e.g. jpegblip). This has nothing with how the file itself is stored on disk, rather how the image is encoded with that file. The issue isn't as straight-forward as you believe it to be. – Mark Hiscocks Nov 20 '12 at 20:34