I am creating simple console application where I need to load RTF file to FlowDocument for further work.
I am using this code for loading file to FlowDocument:
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".rtf";
dlg.Filter = "RichText Files (*.rtf)|*.rtf";
// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
// Open document
string filename = dlg.FileName;
FlowDocument flowDocument = new FlowDocument();
TextRange textRange = new TextRange(flowDocument.ContentStart, flowDocument.ContentEnd);
using (FileStream fileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
textRange.Load(fileStream, DataFormats.Rtf);
}
}
If I do this in WPF application and show the document in flowDocumentPageViewer, everything is OK. But if I try to load file in console application, I get exception: Unrecognized structure in data format Rich Text Box, parameter name stream.
And for some reason, this exception appears only, if there is image in document.
Any ideas what's wrong or better how to solve it?
Best Answer
Problem was in using System.Windows namesapce for DataFormats. With System.Windows.Forms its functional.