public static void ToHtml(RtfSource source, XmlWriter writer, RtfHtmlSettings settings = null) { var parser = new Parser(source.Reader); var interpreter = new Interpreter(writer); interpreter.ToHtml(parser.Parse(), settings); }
/// <summary> /// Convert a Rich Text Format (RTF) document to HTML /// </summary> /// <param name="source">The source RTF document (either a <see cref="string"/>, <see cref="TextReader"/>, or <see cref="Stream"/>)</param> /// <param name="writer"><see cref="TextWriter"/> that the HTML will be written to</param> /// <param name="settings">The settings used in the HTML rendering</param> public static void ToHtml(RtfSource source, TextWriter writer, RtfHtmlSettings settings = null) { using (var xmlWriter = new HtmlTextWriter(writer, settings)) { ToHtml(source, xmlWriter, settings); } }
public static string ToHtml(RtfSource source, RtfHtmlSettings settings = null) { using (var stringWriter = new StringWriter()) { using (var writer = new HtmlTextWriter(stringWriter, settings)) { ToHtml(source, writer, settings); } return(stringWriter.ToString()); } }
public static string ToHtml(RtfSource source, RtfHtmlWriterSettings settings = null) { using (var stringWriter = new StringWriter()) { using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings() { OmitXmlDeclaration = true })) { ToHtml(source, writer, settings); } return(stringWriter.ToString()); } }
private static IRtfGroup ParseRtf(RtfSource source) { IRtfGroup rtfStructure; try { // parse the rtf structure RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(source); rtfStructure = structureBuilder.StructureRoot; } catch { return(null); } return(rtfStructure); } // ParseRtf
/// <summary> /// Convert a Rich Text Format (RTF) document to HTML /// </summary> /// <param name="source">The source RTF document (either a <see cref="string"/>, <see cref="TextReader"/>, or <see cref="Stream"/>)</param> /// <param name="writer"><see cref="XmlWriter"/> that the HTML will be written to</param> /// <param name="settings">The settings used in the HTML rendering</param> /// <example> /// This overload can be used for creating a document that can be further manipulated /// <code lang="csharp"><![CDATA[var doc = new XDocument(); /// using (var writer = doc.CreateWriter()) /// { /// Rtf.ToHtml(rtf, writer); /// }]]> /// </code> /// </example> public static void ToHtml(RtfSource source, XmlWriter writer, RtfHtmlSettings settings = null) { var parser = new Parser(source.Reader); var doc = parser.Parse(); if (doc.HasHtml) { new Model.RawBuilder().Build(doc, writer); } else { var html = new Model.Builder().Build(parser.Parse()); var visitor = new Model.HtmlVisitor(writer) { Settings = settings ?? new RtfHtmlSettings() }; visitor.Visit(html); } writer.Flush(); }
public static void ToHtml(RtfSource source, XmlWriter writer, RtfHtmlWriterSettings settings = null) { settings = settings ?? new RtfHtmlWriterSettings(); var content = ParseRtf(source); // Try to extract encoded html from within the rtf (Outlook likes to do this) if (!BuildHtmlContent(content, writer)) { var intSettings = new RtfInterpreterSettings() { IgnoreDuplicatedFonts = true, IgnoreUnknownFonts = true }; var rtfDocument = RtfInterpreterTool.BuildDoc(content, intSettings); var htmlConvertSettings = new RtfHtmlConvertSettings(settings.ObjectVisitor); htmlConvertSettings.IsShowHiddenText = false; htmlConvertSettings.UseNonBreakingSpaces = false; htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.All; var htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings); htmlConverter.Convert(writer); } }