How to: Export Range To Different Formats
- 2 minutes to read
The following example describes how to export document range to different formats.
RichEditDocumentServer provides a number of methods allowing you to retrieve contents of the specified range in different formats. These are:
- SubDocument.GetText
- SubDocument.GetRtfText
- SubDocument.GetHtmlText
- SubDocument.GetMhtText
- SubDocument.GetWordMLText
- SubDocument.GetDocxBytes.
To retrieve the document range into plane text, use the SubDocument.GetText, as illustrated below.
DevExpress.XtraRichEdit.API.Native.Document document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", DocumentFormat.Docx);
string plainText = document.GetText(document.Paragraphs[2].Range);
System.Windows.Forms.MessageBox.Show(plainText);
The SubDocument.GetHtmlText is used to export range contents into HTML format. The following code snippet retrieves the range in HTML format and opens the resulting document in the browser windows:
DevExpress.XtraRichEdit.API.Native.Document document = server.Document;
document.LoadDocument("Documents\\Grimm.docx", DocumentFormat.Docx);
// Get the range for three paragraphs.
DocumentRange r = document.CreateRange(document.Paragraphs[0].Range.Start, document.Paragraphs[0].Range.Length + document.Paragraphs[1].Range.Length + document.Paragraphs[2].Range.Length);
// Export to HTML.
string htmlText = document.GetHtmlText(r, null);
System.IO.File.WriteAllText("test.html", htmlText);
// Show the result in a browser window.
System.Diagnostics.Process.Start("test.html");
See Also