Exporting to HTML / RTF
Text from a snapshot can easily be exported to a String
or file in various HTML and RTF (rich text) formats.
The ITextExporter interface provides a unified model that supports exporting to any custom output format, although the text framework only includes built-in implementations for HTML and RTF.
Built-in HTML Exporters
The text framework has four formats for exporting HTML, all available as methods on the TextExporterFactory class:
Member | Description |
---|---|
CreateHtmlClassBased Method |
Returns the IHtmlTextExporter that can export text to HTML, using CSS classes for syntax highlighting. CSS class references will be used for highlighting. This type adds a |
CreateHtmlClipboard Method |
Returns the IHtmlTextExporter that can export text to HTML, in a format that is compatible with the Windows clipboard HTML data format. The output of this exporter is the same as CreateHtmlInlineFragment, except that some additional information is added to make the exported text compatible with the Windows clipboard HTML data format. |
CreateHtmlInline Method |
Returns the IHtmlTextExporter that can export text to HTML, using inline CSS for syntax highlighting. Inline CSS styles will be used for highlighting. This type allows for the code block to be directly copied out of the exported document and pasted into another. Root |
CreateHtmlInlineFragment Method |
Returns the IHtmlTextExporter that can export text to HTML, using inline CSS for syntax highlighting and no root Inline CSS styles will be used for highlighting. This type allows for the code block to be directly copied out of the exported document and pasted into another. Root |
The appropriate exporter should be used based on your HTML output needs.
Built-in RTF Exporter
The TextExporterFactory class also has a static CreateRtf method for creating a RTF exporter.
Using a Custom IHighlightingStyleRegistry
The constructor of the TextExporterFactory class optionally takes an IHighlightingStyleRegistry argument. If specified, that registry will be used for converting classification types to styles. Otherwise, the AmbientHighlightingStyleRegistry will be used.
Exporting to a String
Exporting to a string
is done via the ITextSnapshot.Export method.
This code shows how to export the contents of an ITextSnapshot to a string
in RTF format:
string text = document.CurrentSnapshot.Export(new TextExporterFactory().CreateRtf());
Exporting to a File
Exporting to a file is done via the ITextSnapshot.ExportToFile method.
This code shows how to export the contents of an ITextSnapshot to a file in RTF format:
string path = @"C:\output.rtf";
document.CurrentSnapshot.ExportToFile(new TextExporterFactory().CreateRtf(), path);
Exporting Selected Text in a SyntaxEditor View
Exporting to the selected text in a SyntaxEditor view to a string
is done via the IEditorView.ExportSelectedText method.
This code shows how to export the selected text in the active view to a string
in RTF format:
string text = editor.ActiveView.ExportSelectedText(new TextExporterFactory().CreateRtf());
Exporting a Snapshot Text Range
The ITextExporter.Export method allows for the exporting of any custom snapshot text range.
This code shows how to export a snapshot text range to a string
in RTF format:
string text = new TextExporterFactory().CreateRtf().Export(snapshot, new TextRange[] { textRange });