简体   繁体   中英

How to store formatted snippets of Microsoft Word documents in sql server

I need to extract formatted text snippets of a Word document and store it inside an SQL Server table, for later processing and then reinsertion in the Word document using C#.

I've had a look at the Word DOM and it seems that I need to use a combination of the Document.Load(), Document.Save() and Range.Copy(), Range.Paste() methods to create a file for each snippets that I then load into the DB.

Isn't there a easier (more efficient way)?

By the way the code snippets can be hidden text and I was thinking about storing the snippets as RTF.

Finally I got to use Aspose.Words for .NET to extract the code snippets from the Word file I'm interested in and store them as 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();

One can then store the rtf into a text field of the database as usual and edit it in a RTF text control.

Document.load, then select the range via a RANGE object, then use the XML property of the range object to get the XML of that range and store it.

You can later insert the XML into another document using the reverse process.

Editing the snippets might prove interesting though, because I'm not aware of any web based WORD compatible editors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM