C# – Loading RTF file with images to FlowDocument in console application

cconsolenetrtf

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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Windows.Documents;

namespace RtfTest
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            DoRead();
        }

        private static void DoRead()
        {
            // Create OpenFileDialog 
            OpenFileDialog dlg = new 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 
            var result = dlg.ShowDialog();
            try
            {
                if (result == DialogResult.OK)
                {
                    // 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);
                    }

                }
            }
            catch (Exception)
            {

                throw;
            }


        }
    }
}