11
votes

Comment lire un fichier .RTF en utilisant .NET 4.0

J'ai vu des échantillons à l'aide de la bibliothèque d'objets Word 9.0. Mais j'ai Office 2010 Beta et .NET 4.0 à VS2010. Toute astuce sur la façon d'aller avec le nouveau mot dlls?

Donc, je voulais juste obtenir la fonctionnalité de RTF au texte avec .NET.NET3.5 ou ultérieure.


0 commentaires

4 Réponses :


5
votes

Avez-vous vraiment de nouveau pour charger .rtf en mot? .NET a le contrôle RichtextBox qui peut gérer des fichiers .RTF. Voir ici: http://msdn.microsoft.com/en-us/library/ 1z7hy77a.aspx (Comment: charger des fichiers dans la commande Windows Forms Richtextbox Control)


0 commentaires

11
votes

J'ai 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);
}


0 commentaires

1
votes
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);
    }
}    
edited to correct syntax highlighting

1 commentaires

Le texte est en fait un peu assez difficile comme ça. Vous ne prenez pas de texte en tenant compte du tout.



0
votes

Si quelqu'un a besoin d'une solution à ASP.NET, j'ai trouvé cette solution parfaite:

Ajouter une référence à System.Windows.Forms ou Téléchargez la DLL elle-même et référez-la.

Suivant Vous pouvez extraire le texte en créant un temporaire richtextbox : xxx


0 commentaires