Comment lire un fichier .RTF à l’aide de .NET 4.0

J’ai vu des exemples utilisant la bibliothèque d’objects Word 9.0. Mais j’ai Office 2010 Beta et .NET 4.0 dans VS2010. Des conseils sur la marche à suivre pour utiliser les nouvelles Word Dll?

Je voulais donc simplement obtenir les fonctionnalités de RTF to TEXT avec .NET3.5 ou une version ultérieure.

J’ai eu une meilleure solution avec WPF, en utilisant TextRange.

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

Vous pouvez maintenant voir le texte extrait dans documentTextRange.Text

Êtes-vous vraiment nouveau pour charger. RTF dans Word? .net a le contrôle RichTextBox qui peut gérer les fichiers .RTF. Voir ici: http://msdn.microsoft.com/en-us/library/1z7hy77a.aspx (Procédure: charger des fichiers dans le contrôle Windows Forms RichTextBox)

 public enum eFileType { Invalid = -1, TextDocument = 0, RichTextDocument, WordDocument } public interface IRead { ssortingng Read(ssortingng file); } public static class FileManager { public static eFileType GetFileType(ssortingng 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 ssortingng Read(ssortingng file) { try { var reader = new StreamReader(file); var content = reader.ReadToEnd(); reader.Close(); return content; } catch { return null; } } } public class RichTextDocument : IRead { public ssortingng Read(ssortingng 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 ssortingng Read(ssortingng 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 ssortingng Read(ssortingng file) { return _read.Read(file); } } 

édité pour corriger la coloration syntaxique