2011-11-04 1 views
0

나는 앞으로의 작업을 위해 RTF 파일을 FlowDocument에로드해야하는 간단한 콘솔 응용 프로그램을 만들고 있습니다.콘솔 응용 프로그램에서 이미지가있는 RTF 파일을 FlowDocument에로드

나는 FlowDocument에로드 파일이 코드를 사용하고 있습니다 : 나는 WPF 응용 프로그램에서이 작업을 수행하고 flowDocumentPageViewer에서 문서를 표시하는 경우

 // 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); 
      } 

     } 

이 모든 것이 OK입니다. 그러나 콘솔 응용 프로그램에서 파일을로드하려고하면 예외가 발생합니다 : 인식 할 수없는 구조의 데이터 형식 서식있는 텍스트 상자, 매개 변수 이름 스트림.

그리고 문서에 이미지가있는 경우에만이 예외가 표시됩니다.

어떤 아이디어가 잘못되었거나 어떻게 해결 되었습니까?

답변

1

DataFormats에 System.Windows 네임 스페이스를 사용하는 데 문제가있었습니다. System.Windows.Forms가 작동합니다.

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; 
      } 


     } 
    } 
} 
+0

'System.Windows.Forms.DataFormats.Rtf'가이 문제를 해결하지 못했습니다. textRange는 이미지를 포함 할 수 없습니다. 여전히'예외 : 데이터 형식으로 인식 할 수없는 구조의 서식있는 텍스트 상자, 매개 변수 이름 스트림'이 있습니다. – JharPaat

관련 문제