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?
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.
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);
Related
I want to be able to write unit tests against Xaml to look for improper patterns. We have issues where the Xaml is still valid and otherwise compiles and "works" fine but can cause hidden issues or lacks consistency. In order to help combat this we want to write unit tests against the files.
A simple example: If you define a grid and the grid column is bound to a numeric field on the underlying object then the column title of the grid should have the appropriate right align template per our internal standards. Leaving the template off the definition will yield a left aligned column header which doesn't really break anything other than our internal standards. Given this is basically xml, I should easily be able to write a test that interrogates the xml element, detects the existence of a numeric column and checks to make sure that the file also defines a title template that is right aligned. This way I don't have to rely on the developer or QA to cover things that are easily testable like this.
I have access to the assembly during the unit test process and can get to all the baml resource files by reading the manifest resource stream. But I can't figure out how to read them into a useful format that I could load into something like an XDocument for parsing and perusing. Has anyone done this? I've tried using Baml2006Reader, etc... without any good success.
If you want to convert BAML to XAML, you could take a look at the following BamlTranslator class on GitHub.
It uses a BinaryReader to decompile the BAML.
I've looked on MSDN, and I am having trouble finding which file types (extensions) can be displayed in a RichTextBox in Winforms.
I have written an app that allows the user to display files from a specific folder based on the user's choice of file extensions. For example only display .txt and .html and ignore anything else.
Therefore, I need an exhaustive list of the extensions that wiil render properly so that I can populate a list for the user to choose from.
I don't suppose it's relevant but I'm using C#.
Any help is appreciated. thank you.
From MSDN's documentation, it appears that the RichTextBox is only intended to open .rtf files, but can also open plain text (.txt) ASCII and Unicode files using the LoadFile method with the RichTextBoxStreamType argument.
Sources:
RichTextBox.LoadFile Method
RichTextBoxStreamType Enumeration
I like DocumentViewer for display of an XPS document in a WPF application. But I also need to redact the XPS document. I have found stand alone applications (.exe) for redacting XPS. What I need is a WPF control for view and redaction of XPS. Or an extension to DocumentViewer for redaction. The redaction must actually remove the data and not just hide it (this is for a litigation application). Does anyone know of way to view and redact XPS in WPF?
With a custom DocumentPaginator you can control how each element of the document is printed.
If you can identify the text ranges you want to redact then you can replace the glyphs with a black visual and not add the text range to the final document.
...
Hmm. As I typed the above it really sounds a bit complex when there are simpler solutions.
Read the XPS document, convert it to a FlowDocument.
Allow the user to select text ranges to redact.
Replace* the selected text with the ████████████████ character (U+2588, Full Block).
Convert the document back to XPS and print.
* As you replaced the original text, it will not be available in copy/paste actions.
There are plenty of tutorials on how to each the above steps separately. Let me know if you need any additional help.
If my mind don't lie, you can't edit XPS directly, but you can create WPF representation of document, edit it and print to new XPS file.
I have a requirement to save emails written in a RichTextBox to a database. Obviously, I would like to persist the formatting to the DB and send out HTML emails. However, this requires converting XAML to HTML which will be stored and then converting back if they are reloaded from the DB.
I have tried out the XAMLtoHTML converter. The problem I am getting is that the converter only works partially. It sets up all the formatting correctly in HTML but the text part is missing. I have followed this post:: XAML to HTML Conversion - WPF RichTextBox and had the same error but adding the tags didnt solve the problem.
Has anyone got experience with XAML to HTML conversions? Thanks
Consider using this free HTML Editor for silverlight instead. Html RichTextArea Control (Silverlight 4 Only).
You then load and save HTML and not bother with Xaml at all. Just be careful with the fontsizes, last time I looked only px sizes worked, pt sizes were not supported. You may want to fiddle with the samples font drop down so that the actual value is 1.33 the displayed size.
I have a RichTextBox and need to serialize its content to my database purely for storage purposes. It would appear that I have a choice between serializing as XAML or as RTF, and am wondering if there are any advantages to serializing to XAML over RTF, which I would consider as more "standard".
In particular, am I losing any capability by serializing to RTF instead of XAML? I understand XAML supports custom classes inside the FlowDocument, but I'm not currently using any custom classes (though the potential for extensibility might be enough reason to use XAML).
Update: I ended up going with RTF because of its support for text-encoded embedded images. XAML doesn't seem to include image data in its encoding, and XamlPackage encodes to binary, so RTF just works better for me. So far I haven't noticed any lack in capability.
If all your users are doing is typing in the RichTextBox and doing character formatting, RTF is as good as XAML. However there are many FlowDocument capabilities you may expose in your UI that are not convertible to RTF.
Here are some examples of FlowDocument (and RichTextBox) features that are not expressable in RTF or are implemented differently:
A Block can have an arbitrary BorderBrush, including gradient brushes with stops, VisualBrush
A Section has the HasTrailingParagraphBreakOnPaste property
Floater / ClearFloaters is implemented differently
Hyphenation can be enabled/disabled per block, not just per paragraph
WPF Styles and ResourceDictionaries can be included in the Resources property
Arbitrary WPF UI such as bound CheckBoxes, etc, can be embedded inside the RichTextBox and can be cut-and-pasted from other windows.
For example, suppose you want to allow users to drag or cut/paste in a "current date/time" field into your RichTextBox that would always show the current date and time. This could be done by adding a second read-only RichTextBox that has the InlineUIContainer and the already-bound control. This even works when cutting and pasting from other applications and does not require custom controls.
Another consideration is that the code to convert between FlowDocument and RTF is relatively complex so it may have lower performance than going with XAML. Of course loose XAML doesn't include images and such - for that you need to use XamlPackage. I store my XamlPackage in the database as a byte[], but you can also choose to Base64 encode it for storage as a string.
The bottom line is that it really depends on whether you want the user to be able to use features not available in RTF. Even if your application doesn't include tools to generate FlowDocuments that use these features, it is possible to cut-and-paste them from other applications.
Be aware that in Wpf RichTextBox's method called TextRange.Save has a bug whereby it loses any end of line terminator. Microsoft will not fix.
https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=478640&wa=wsignin1.0#tabs