Inheritance: RtfParserBase
        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();
        }
 public void ParseLoggerTest()
 {
     RtfParser parser = new RtfParser();
     const bool enableLogging = false;
     parser.AddParserListener( new RtfParserListenerLogger( new RtfParserLoggerSettings( enableLogging ) ) );
     parser.Parse( new RtfSource( @"{\rtf1foobar}" ) );
     parser.Parse( new RtfSource( GetTestResource( "minimal.rtf" ) ) );
 }
 // ----------------------------------------------------------------------
 static void RtfSympleParser()
 {
     RtfParser parser = new RtfParser();
     RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
     parser.AddParserListener( structureBuilder );
     parser.Parse( new RtfSource( @"{\rtf1foobar}" ) );
     RtfDumpElement( structureBuilder.StructureRoot );
 }
 // ----------------------------------------------------------------------
 public static IRtfGroup Parse( IRtfSource rtfTextSource, params IRtfParserListener[] listeners )
 {
     RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
     RtfParser parser = new RtfParser( structureBuilder );
     if ( listeners != null )
     {
         foreach ( IRtfParserListener listener in listeners )
         {
             if ( listener != null )
             {
                 parser.AddParserListener( listener );
             }
         }
     }
     parser.Parse( rtfTextSource );
     return structureBuilder.StructureRoot;
 }
        public void ParseStructureBuilderTest()
        {
            RtfParser parser = new RtfParser();
            RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
            parser.AddParserListener( structureBuilder );
            parser.Parse( new RtfSource( @"{\rtf1foobar}" ) );

            IRtfGroup rtfStructure = structureBuilder.StructureRoot;
            Assert.IsNotNull( rtfStructure );

            Assert.AreEqual( RtfElementKind.Group, rtfStructure.Kind );
            Assert.AreEqual( 2, rtfStructure.Contents.Count );

            Assert.AreEqual( RtfElementKind.Tag, rtfStructure.Contents[ 0 ].Kind );
            Assert.AreEqual( "rtf", ((IRtfTag)rtfStructure.Contents[ 0 ]).Name );
            Assert.AreEqual( true, ((IRtfTag)rtfStructure.Contents[ 0 ]).HasValue );
            Assert.AreEqual( "1", ((IRtfTag)rtfStructure.Contents[ 0 ]).ValueAsText );
            Assert.AreEqual( 1, ((IRtfTag)rtfStructure.Contents[ 0 ]).ValueAsNumber );

            Assert.AreEqual( RtfElementKind.Text, rtfStructure.Contents[ 1 ].Kind );
            Assert.AreEqual( "foobar", ((IRtfText)rtfStructure.Contents[ 1 ]).Text );
        }
Example #6
0
        // ----------------------------------------------------------------------
        private IRtfGroup ParseRtf()
        {
            IRtfGroup rtfStructure;
            RtfParserListenerFileLogger parserLogger = null;
            try
            {
                // logger
                if ( settings.LogParser )
                {
                    string logFileName = settings.BuildDestinationFileName(
                        settings.LogDirectory,
                        RtfParserListenerFileLogger.DefaultLogFileExtension );
                    parserLogger = new RtfParserListenerFileLogger( logFileName );
                }

                // rtf parser
                // open readonly - in case of dominant locks...
                using ( FileStream stream = File.Open( settings.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) )
                {
                    // parse the rtf structure
                    RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
                    RtfParser parser = new RtfParser( structureBuilder );
                    parser.IgnoreContentAfterRootGroup = true; // support WordPad documents
                    if ( parserLogger != null )
                    {
                        parser.AddParserListener( parserLogger );
                    }
                    parser.Parse( new RtfSource( stream ) );
                    rtfStructure = structureBuilder.StructureRoot;
                }
            }
            catch ( Exception e )
            {
                if ( parserLogger != null )
                {
                    parserLogger.Dispose();
                }

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

            return rtfStructure;
        }
        // ----------------------------------------------------------------------
        protected override void DoTest( string kind, Stream testRes, string testCaseName )
        {
            RtfParserListenerStructureBuilder structureBuilder = new RtfParserListenerStructureBuilder();
            RtfParser parser = new RtfParser();
            //parser.AddParserListener( new RtfParserListenerLogger() );
            parser.AddParserListener( structureBuilder );

            parser.Parse( new RtfSource( testRes ) );
            Assert.IsNotNull( structureBuilder.StructureRoot );
        }