public static string ConvertRtf2Html(StreamReader rtfStream) { // image converter // convert all images to JPG RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter(ImageFormat.Jpeg); RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter); imageConvertSettings.ScaleImage = true; // scale images RtfImageConverter imageConverter = new RtfImageConverter(imageConvertSettings); RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder(); RtfParser parser = new RtfParser(structureBuilder); IRtfGroup rtfStructure; parser.IgnoreContentAfterRootGroup = true; // support WordPad documents parser.Parse(new RtfSource(rtfStream)); rtfStructure = structureBuilder.StructureRoot; // interpreter IRtfDocument rtfDocument = InterpretRtf(rtfStructure); // html converter RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings(imageAdapter); htmlConvertSettings.StyleSheetLinks.Add("default.css"); RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings); return htmlConverter.Convert(); }
} // RtfHtmlConverter // ---------------------------------------------------------------------- public RtfHtmlConverter(IRtfDocument rtfDocument, RtfHtmlConvertSettings settings) { if (rtfDocument == null) { throw new ArgumentNullException("rtfDocument"); } if (settings == null) { throw new ArgumentNullException("settings"); } this.rtfDocument = rtfDocument; this.settings = settings; } // RtfHtmlConverter
// ---------------------------------------------------------------------- public RtfHtmlConverter( IRtfDocument rtfDocument, RtfHtmlConvertSettings settings ) { if ( rtfDocument == null ) { throw new ArgumentNullException( "rtfDocument" ); } if ( settings == null ) { throw new ArgumentNullException( "settings" ); } this.rtfDocument = rtfDocument; this.settings = settings; specialCharacters = new RtfHtmlSpecialCharCollection( settings.SpecialCharsRepresentation ); }
} // RtfHtmlConverter // ---------------------------------------------------------------------- public RtfHtmlConverter(IRtfDocument rtfDocument, RtfHtmlConvertSettings settings) { if (rtfDocument == null) { throw new ArgumentNullException("rtfDocument"); } if (settings == null) { throw new ArgumentNullException("settings"); } this.rtfDocument = rtfDocument; this.settings = settings; specialCharacters = new RtfHtmlSpecialCharCollection(settings.SpecialCharsRepresentation); } // RtfHtmlConverter
} // RtfHtmlConverter public RtfHtmlConverter(IRtfDocument rtfDocument, RtfHtmlConvertSettings settings) { if (rtfDocument == null) { throw new ArgumentNullException(nameof(rtfDocument)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } RtfDocument = rtfDocument; Settings = settings; SpecialCharacters = new RtfHtmlSpecialCharCollection(settings.SpecialCharsRepresentation); } // RtfHtmlConverter
private static string ConvertRtfToHtml(string rtfText) { IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(rtfText); RtfHtmlConvertSettings settings = new RtfHtmlConvertSettings(); settings.ConvertScope = RtfHtmlConvertScope.Content; RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, settings); return htmlConverter.Convert(); }
// ---------------------------------------------------------------------- private string ConvertHmtl( IRtfDocument rtfDocument, IRtfVisualImageAdapter imageAdapter ) { string html; try { RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings( imageAdapter ); if ( settings.CharacterSet != null ) { htmlConvertSettings.CharacterSet = settings.CharacterSet; } htmlConvertSettings.Title = settings.SourceFileNameWithoutExtension; htmlConvertSettings.ImagesPath = settings.ImagesPath; htmlConvertSettings.IsShowHiddenText = settings.ShowHiddenText; htmlConvertSettings.UseNonBreakingSpaces = settings.UseNonBreakingSpaces; if ( settings.ConvertScope != RtfHtmlConvertScope.None ) { htmlConvertSettings.ConvertScope = settings.ConvertScope; } if ( !string.IsNullOrEmpty( settings.StyleSheets ) ) { string[] styleSheets = settings.StyleSheets.Split( ',' ); htmlConvertSettings.StyleSheetLinks.AddRange( styleSheets ); } htmlConvertSettings.ConvertVisualHyperlinks = settings.ConvertVisualHyperlinks; if ( !string.IsNullOrEmpty( settings.VisualHyperlinkPattern ) ) { htmlConvertSettings.VisualHyperlinkPattern = settings.VisualHyperlinkPattern; } htmlConvertSettings.SpecialCharsRepresentation = settings.SpecialCharsRepresentation; RtfHtmlConverter htmlConverter = new RtfHtmlConverter( rtfDocument, htmlConvertSettings ); html = htmlConverter.Convert(); } catch ( Exception e ) { Console.WriteLine( "error while converting to html: " + e.Message ); ExitCode = ProgramExitCode.ConvertHtml; return null; } return html; }