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(); }
// ---------------------------------------------------------------------- private string InterpretRtf( IRtfGroup rtfStructure ) { string text = null; RtfInterpreterListenerFileLogger interpreterLogger = null; try { // logger if ( settings.LogInterpreter ) { string logFileName = settings.BuildDestinationFileName( settings.LogDirectory, RtfInterpreterListenerFileLogger.DefaultLogFileExtension ); interpreterLogger = new RtfInterpreterListenerFileLogger( logFileName ); } // text converter RtfTextConverter textConverter = null; if ( settings.SaveText ) { RtfTextConvertSettings textConvertSettings = new RtfTextConvertSettings(); textConvertSettings.IsShowHiddenText = settings.ShowHiddenText; textConverter = new RtfTextConverter( textConvertSettings ); } // image converter RtfImageConverter imageConverter = null; if ( settings.SaveImage ) { RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter( settings.ImageFileNamePattern, settings.ImageFormat ); RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings( imageAdapter ); imageConvertSettings.ImagesPath = settings.DestinationDirectory; imageConvertSettings.ScaleImage = settings.ScaleImage; imageConvertSettings.BackgroundColor = settings.ImageBackgroundColor; if ( settings.ExtendedImageScale ) { imageConvertSettings.ScaleExtension = 0.5f; } imageConverter = new RtfImageConverter( imageConvertSettings ); } // rtf parser // interpret the rtf structure using the extractors RtfInterpreterTool.Interpret( rtfStructure, interpreterLogger, textConverter, imageConverter ); // get the resulting text if ( textConverter != null ) { text = textConverter.PlainText; } } catch ( Exception e ) { if ( interpreterLogger != null ) { interpreterLogger.Dispose(); } Console.WriteLine( "error while interpreting rtf: " + e.Message ); ExitCode = ProgramExitCode.InterpretRtf; return null; } return text; }
// ---------------------------------------------------------------------- public void Execute() { Console.WriteLine( string.Concat( ApplicationInfo.ShortCaption, ", ", ApplicationInfo.Copyright ) ); // program settings if ( ValidateProgramSettings() == false ) { return; } // parse rtf IRtfGroup rtfStructure = ParseRtf(); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // destination directory EnsureDestinationDirectory(); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // image handling RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter( settings.ImageFileNamePattern, settings.ImageFormat ); // interpret rtf IRtfDocument rtfDocument = InterpretRtf( rtfStructure, imageAdapter ); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // convert to hmtl string html = ConvertHmtl( rtfDocument, imageAdapter ); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // save html string fileName = SaveHmtl( html ); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // open html file OpenHtmlFile( fileName ); if ( ExitCode != ProgramExitCode.Successfully ) { return; } // display html text DisplayHtmlText( html ); if ( ExitCode != ProgramExitCode.Successfully ) { return; } Console.WriteLine( "successfully converted RTF to HTML in " + settings.DestinationDirectory ); }