我見過使用Word 9.0對象庫的示例。但是我在VS2010中安裝了Office 2010 Beta和.NET 4.0。有關如何使用新的Word Dll的任何提示?如何使用.NET 4.0讀取.RTF文件
所以我只是想在.NET3.5或更高版本中將RTF的功能轉換爲TEXT。
我見過使用Word 9.0對象庫的示例。但是我在VS2010中安裝了Office 2010 Beta和.NET 4.0。有關如何使用新的Word Dll的任何提示?如何使用.NET 4.0讀取.RTF文件
所以我只是想在.NET3.5或更高版本中將RTF的功能轉換爲TEXT。
我使用TextRange獲得了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
你真的很新來加載.RTF到Word嗎? .net具有可處理.RTF文件的RichTextBox控件。請參閱:http://msdn.microsoft.com/en-us/library/1z7hy77a.aspx(如何:將文件加載到Windows窗體RichTextBox控件中)
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);
}
}
編輯以糾正語法高亮