2010-02-19 24 views
9

का उपयोग कर आरटीएफ फ़ाइल को कैसे पढ़ा जाए मैंने वर्ड 9.0 ऑब्जेक्ट लाइब्रेरी का उपयोग करके नमूने देखे हैं। लेकिन मेरे पास वीएस -2010 में कार्यालय 2010 बीटा और .NET 4.0 है। नए शब्द डीएलएस के साथ कैसे जाना है इस पर कोई सुझाव?.NET 4.0

तो मैं सिर्फ आरटीएफ की कार्यक्षमता को .NET3.5 या उसके बाद के संस्करण में प्राप्त करना चाहता था।

उत्तर

9

मुझे टेक्स्टरेंज का उपयोग करके WPF के साथ बेहतर समाधान मिला।

FlowDocument document = new FlowDocument(); 

//Read the file stream to a Byte array 'data' 
TextRange txtRange = null; 

using (MemoryStream stream = new MemoryStream(data)) 
{ 
    // create a TextRange around the entire document 
    txtRange = new TextRange(document.ContentStart, document.ContentEnd); 
    txtRange.Load(stream, DataFormats.Rtf); 
} 

अब आप documentTextRange.Text

अंदर निकाले पाठ देख सकते हैं
5

क्या आप वास्तव में लोड करने के लिए नया हैं। आरटीएफ शब्द में? .NET में RichTextBox नियंत्रण है जो आरटीएफ फ़ाइलों को संभाल सकता है। यहां देखें: http://msdn.microsoft.com/en-us/library/1z7hy77a.aspx (कैसे करें: विंडोज़ फॉर्म में फ़ाइलें लोड करें RichTextBox नियंत्रण)

0
public enum eFileType 
{ 
    Invalid = -1, 
    TextDocument = 0, 
    RichTextDocument, 
    WordDocument 
} 

public interface IRead 
{ 
    string Read(string file); 
} 

public static class FileManager 
{ 
    public static eFileType GetFileType(string extension) 
    { 
     var type = eFileType.Invalid; 
     switch (extension) 
     { 
      case ".txt": type = eFileType.TextDocument; 
       break; 
      case ".rtf": type = eFileType.RichTextDocument; 
       break; 
      case ".docx": type = eFileType.WordDocument; 
       break; 
     } 
     return type; 
    } 
} 


public class TextDocument : IRead 
{ 
    public string Read(string file) 
    { 
     try 
     { 
      var reader = new StreamReader(file); 
      var content = reader.ReadToEnd(); 
      reader.Close(); 
      return content; 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 

public class RichTextDocument : IRead 
{ 
    public string Read(string file) 
    { 
     try 
     { 
      var wordApp = new Application(); 
      object path = file; 
      object nullobj = System.Reflection.Missing.Value; 
      var doc = wordApp.Documents.Open(ref path, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj); 
      var result = wordApp.ActiveDocument.Content.Text; 
      var doc_close = (_Document)doc; 
      doc_close.Close(); 
      return result; 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 

public class WordDocument : IRead 
{ 
    public string Read(string file) 
    { 
     try 
     { 
      var wordApp = new Application(); 
      object path = file; 
      object nullobj = System.Reflection.Missing.Value; 
      var doc = wordApp.Documents.Open(ref path, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj, 
                ref nullobj); 
      var result = wordApp.ActiveDocument.Content.Text; 
      var doc_close = (_Document)doc; 
      doc_close.Close(); 
      return result; 
     } 
     catch 
     { 
      return null; 
     } 
    } 
} 

public class Factory 
{ 
    public IRead Get(eFileType type) 
    { 
     IRead read = null; 
     switch (type) 
     { 
      case eFileType.RichTextDocument: read = new RichTextDocument(); 
       break; 
      case eFileType.WordDocument: read = new WordDocument(); 
       break; 
      case eFileType.TextDocument: read = new TextDocument(); 
       break; 
     } 
     return read; 
    } 
} 

public class ResumeReader 
{ 
    IRead _read; 
    public ResumeReader(IRead read) 
    { 
     if (read == null) throw new InvalidDataException("read cannot be null"); 

     _read = read; 
    } 
    public string Read(string file) 
    { 
     return _read.Read(file); 
    } 
}  

वाक्य रचना हाइलाइटिंग

सही करने के लिए संपादित