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();
        }
Пример #2
0
		} // RtfImageConverter

		// ----------------------------------------------------------------------
		public RtfImageConverter( RtfImageConvertSettings settings )
		{
			if ( settings == null )
			{
				throw new ArgumentNullException( "settings" );
			}

			this.settings = settings;
		} // RtfImageConverter
Пример #3
0
        // ----------------------------------------------------------------------
        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;
        }
        // ----------------------------------------------------------------------
        private IRtfDocument InterpretRtf( IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter )
        {
            IRtfDocument rtfDocument;
            RtfInterpreterListenerFileLogger interpreterLogger = null;
            try
            {
                // logger
                if ( settings.LogInterpreter )
                {
                    string logFileName = settings.BuildDestinationFileName(
                        settings.LogDirectory,
                        RtfInterpreterListenerFileLogger.DefaultLogFileExtension );
                    interpreterLogger = new RtfInterpreterListenerFileLogger( logFileName );
                }

                // image converter
                RtfImageConverter imageConverter = null;
                if ( settings.SaveImage )
                {
                    RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings( imageAdapter );
                    imageConvertSettings.ImagesPath = settings.DestinationDirectory;
                    imageConvertSettings.BackgroundColor = settings.ImageBackgroundColor;
                    if ( settings.ExtendedImageScale )
                    {
                        imageConvertSettings.ScaleExtension = 0.5f;
                    }
                    imageConverter = new RtfImageConverter( imageConvertSettings );
                }

                // rtf parser
                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc( rtfStructure, interpreterLogger, imageConverter );

            }
            catch ( Exception e )
            {
                if ( interpreterLogger != null )
                {
                    interpreterLogger.Dispose();
                }

                Console.WriteLine( "error while interpreting rtf: " + e.Message );
                ExitCode = ProgramExitCode.InterpretRtf;
                return null;
            }

            return rtfDocument;
        }