最後我用Aspose.Words for .NET從Word文件,我感興趣的提取的代碼片段,並將其存儲爲RTF:
// Get insteresting code snippets (in this case text runs with
// style "tw4winMark")
Document sourceDocument = new Document(fileName);
var runs = sourceDocument.GetChildNodes(NodeType.Run, true)
.Select(r => r.Font.StyleName == "tw4winMark").ToList();
// Store snippets into temporary document
// Read Aspose documentation for details
Document document = new Document();
if (runs.Count > 0) {
NodeImporter nodeImporter = new NodeImporter(
runs[0].Document,
document,
ImportFormatMode.KeepSourceFormatting
);
foreach (Run run in runs) {
Run importedRun = nodeImporter.ImportNode(run, true) as Run;
importedRun.Font.Hidden = false;
document.Sections[0].Body.Paragraphs[0].AppendChild(importedRun);
}
}
// save temporary document in MemoryStream as RTF
RtfSaveOptions saveOptions = new RtfSaveOptions();
MemoryStream ms = new MemoryStream();
document.Save(ms, saveOptions);
// retrieve RTF from MemoryStream
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string rtf = sr.ReadToEnd();
之一,那麼可以將RTF存儲到數據庫中的文本字段像往常一樣,並在RTF文本控件中進行編輯。
如果你使用的是Office 2007或更新的版本,Word文檔基本上是一個XML結構 - 所以一個片段(甚至格式化的)將是一個XML片段,真的...... – 2010-11-15 13:23:09
基本上是的。但是docx文檔的規範長達幾千頁。我沒有提到的是,片段必須由最終用戶在Web瀏覽器中進行編輯。 – 2010-11-16 09:01:54