示例#1
0
        public void ExtractPlainTextTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_0.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            var rtfString =
                @"{\rtf1\sste16000\ansi\deflang1033\ftnbj\uc1\deff0 {\fonttbl{\f0 \fnil Verdana;}{\f1 \fnil \fcharset0 Verdana;}} {\colortbl;\red0\green0\blue0;} {\stylesheet{\f0\fs28 Normal;}{\cs1 Default Paragraph Font;}} {\*\revtbl{Unknown;}} \paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\headery720\footery720\nogrowautofit\deftab720\formshade\fet4\aendnotes\aftnnrlc\pgbrdrhead\pgbrdrfoot \sectd\pgwsxn12240\pghsxn15840\marglsxn1800\margrsxn1800\margtsxn1440\margbsxn1440\headery720\footery720\sbkpage\pgncont\pgndec \plain\plain\f0\fs24\plain\f0\fs24\plain\f1\fs28\lang1033\hich\f1\dbch\f1\loch\f1\cf2\fs28 test again and again and again }";

            var rtfString2 =
                @"{\rtf1\sste16000\ansi\deflang1033\ftnbj\uc1\deff0 {\fonttbl{\f0 \f \fcharset0 Arial;}} {\colortbl ;\red255\green255\blue255 ;\red0\green0\blue0 ;\red0\green0\blue0 ;} {\stylesheet{\f0\fs24 Normal;}{\cs1 Default Paragraph Font;}} {\*\revtbl{Unknown;}} \paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\headery720\footery720\htmautsp1\nogrowautofit\deftab720\formshade\fet4\aendnotes\aftnnrlc\pgbrdrhead\pgbrdrfoot \sectd\pgwsxn12240\pghsxn15840\marglsxn1800\margrsxn1800\margtsxn1440\margbsxn1440\headery720\footery720\sbkpage\pgncont\pgndec \plain\plain\f0\fs24\ltrpar\ql\plain\f0\fs24\plain\f0\fs24\lang1033\hich\f0\dbch\f0\loch\f0\cf2\fs24\ltrch this is my cover changed in bulk\plain\f0\fs24\par and then in text\par }";

            var rtfString3 =
                @"{\rtf1\sste16000\ansi\deflang1033\ftnbj\uc1\deff0 {\fonttbl{\f0 \fnil Arial;}{\f1 \fnil \fcharset0 Arial;}} {\colortbl ;\red0\green0\blue0 ;\red255\green255\blue255 ;\red0\green0\blue0 ;\red255\green0\blue0 ;\red0\green0\blue0 ;} {\stylesheet{\f0\fs24 Normal;}{\cs1 Default Paragraph Font;}} {\*\revtbl{Unknown;}} \paperw12240\paperh15840\margl1800\margr1800\margt1440\margb1440\headery720\footery720\nogrowautofit\deftab720\formshade\fet4\aendnotes\aftnnrlc\pgbrdrhead\pgbrdrfoot \sectd\pgwsxn12240\pghsxn15840\marglsxn1800\margrsxn1800\margtsxn1440\margbsxn1440\headery720\footery720\sbkpage\pgncont\pgndec \plain\hich\f0\dbch\f0\loch\f0\cf1\fs24\pard\lang1033\hich\f1\dbch\f1\loch\f1 12:24  v3465        AP-APTN-1630: Turkey Syria Wednesday, 3 October 2012\par  STORY:Turkey Syria- Shell fired from Syria kills three in Turkey LENGTH: 00:40\tab  FIRST RUN: 1530 RESTRICTIONS: No Access Turkey/ROJ TV TYPE: Natsound SOURCE: Anadolu Agency STORY NUMBER: 861347\par  DATELINE: Sanliurfa - 3 Oct 2012 LENGTH: 00:40\tab\par \par \par   \cf4\b\protect [Duration: 07:06]\plain\f1\fs24  \par \par \par  SHOTLIST:\par  1. Wide of smoke rising, policemen run for cover and surround wounded officer lying on ground  2. Wide of street, smoke seen rising from the distance 3. Tracking view of wounded person in a car which pulls away, policemen seen in background surrounding  injured officer lying on the ground 4. Mid plainclothes officers leading crowd and media away from injured officer 5. Wide of street with smoke seen, people running along\par  STORYLINE:\par  A Turkish official said on Wednesday that a shell fired from Syria had landed on a house in neighbouring Turkey, killing at least three people, including a 6-year-old boy.\par  Abdulhakim Ayhan, the mayor of the Turkish town of Akcakale which is along the Syrian border, told Turkey's state-owned Anadolu Agency that the boy and a woman were among the dead in Wednesday's shelling.\par  Anadolu Agency reported angry townspeople marched to the mayor's office to protest against the deaths.\par  Video from the Anadolu agency purportedly also showed at least one policeman was also injured in the incident.\par  The village was also hit by a mortar shell on Friday, though no-one was injured on that occasion.\par  The Turkish Foreign Minister Ahmet Davutoglu called an emergency meeting to discuss developments, after receiving information on the incident from Turkish Armed Forces.\par  ===========================================================\par  Clients are reminded:  (i) to check the terms of their licence agreements for use of content outside news programming and that further advice and assistance can be obtained from the AP Archive on: Tel +44 (0) 20 7482 7482 Email: infoaparchive.com (ii)  they should check with the applicable collecting society in their Territory regarding the clearance of any sound recording or performance included within the AP Television News service  (iii) they have editorial responsibility for the use of all and any  content included within the AP Television News service and for libel, privacy, compliance and third party rights applicable to their Territory.\par  APTN\par \par \par \tab    \par \tab    (Copyright 2012 by The Associated Press.  All Rights Reserved.)\par \tab    AP-NY-10-03-12 1224EDT\par   \par \fs18\par \ql\lang1033\hich\f0\dbch\f0\loch\f0\cf1\fs24\plain\f1\fs24\par }";

            RtfTextConverter textConverter = new RtfTextConverter();

            //RtfInterpreterTool.Interpret( rtfStructure, textConverter, interpreterLogger );
            RtfInterpreterTool.Interpret(rtfString3, textConverter, interpreterLogger);

            string plainText = textConverter.PlainText;

            Assert.AreEqual("Hello RTF World\r\n", plainText);
        }         // ExtractPlainTextTest
示例#2
0
        public void ImageFormatDecodingTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();

            const RtfInterpreterListenerLogger interpreterLogger = null;

            //interpreterLogger = new RtfInterpreterListenerLogger();

// ReSharper disable RedundantExplicitArrayCreation
            int[] imageResources = new int[] { 4, 5, 6, 7, 8, 19 };
// ReSharper restore RedundantExplicitArrayCreation
            for (int i = 0; i < imageResources.Length; i++)
            {
                string    testCaseName = BuildTestResourceName("", imageResources[i], false, "rtf");
                IRtfGroup rtfStructure =
                    RtfParserTool.Parse(GetTestResource(testCaseName), parserLogger);
                IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger);
                Assert.IsNotNull(rtfDoc);
                bool imageFound = false;
                foreach (IRtfVisual visual in rtfDoc.VisualContent)
                {
                    if (visual.Kind == RtfVisualKind.Image)
                    {
                        IRtfVisualImage img = (IRtfVisualImage)visual;
                        Assert.IsNotNull(img.ImageForDrawing);
                        imageFound = true;
                        //Console.WriteLine( "image: " + img.ImageForDrawing );
                    }
                }
                Assert.IsTrue(imageFound, "no image found in test case " + testCaseName);
            }
        }         // ImageFormatDecodingTest
示例#3
0
        }         // ToTextButtonClick

        // ----------------------------------------------------------------------
        private void ToXmlButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                TextRange conversionText = ConversionText;
                using (MemoryStream stream = new MemoryStream())
                {
                    conversionText.Save(stream, DataFormats.Rtf);
                    stream.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(reader);

                        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                        xmlWriterSettings.Indent      = true;
                        xmlWriterSettings.IndentChars = ("  ");
                        StringBuilder sb = new StringBuilder();
                        using (XmlWriter writer = XmlWriter.Create(sb, xmlWriterSettings))
                        {
                            RtfXmlConverter xmlConverter = new RtfXmlConverter(rtfDocument, writer);
                            xmlConverter.Convert();
                            writer.Flush();
                            textBox.Text = sb.ToString();
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, "Error " + exception.Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }         // ToXmlButtonClick
示例#4
0
        }         // RtfDumpElement

        // ----------------------------------------------------------------------
        static void RtfSympleInterpreter()
        {
            RtfInterpreterListenerFileLogger logger = new RtfInterpreterListenerFileLogger(@"c:\temp\RtfInterpreter.log");
            IRtfDocument document = RtfInterpreterTool.BuildDoc(@"{\rtf1foobar}", logger);

            RtfWriteDocument(document);
        }         // RtfSympleInterpreter
示例#5
0
        public void ImageTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_4.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger);

            Assert.IsNotNull(rtfDoc);
            IRtfVisualCollection rtfVisuals = rtfDoc.VisualContent;

            Assert.AreEqual(RtfVisualKind.Image, rtfVisuals[4].Kind);
            IRtfVisualImage img = (IRtfVisualImage)rtfVisuals[4];

            Assert.AreEqual(RtfVisualImageFormat.Jpg, img.Format);
            Assert.AreEqual(100, img.Width);
            Assert.AreEqual(142, img.Height);
            Assert.AreEqual(720, img.DesiredWidth);
            Assert.AreEqual(1020, img.DesiredHeight);
        }         // ImageTest
示例#6
0
        private async Task <String> GetUgcPadContent()
        {
            String richText;

            #region Get RichEdit Raw Data

            this.RichBox.Document.GetText(TextGetOptions.FormatRtf, out richText);

            #endregion

            var structureBuilder = new RtfParserListenerStructureBuilder();
            var parser           = new RtfParser(structureBuilder);
            parser.IgnoreContentAfterRootGroup = true; // support WordPad documents

            parser.Parse(new RtfSource(richText));

            var rtfStructure = structureBuilder.StructureRoot;

            // image converter
            var imageAdapter         = new RtfVisualImageAdapter(ImageFormat.Jpeg);
            var imageConvertSettings = new RtfImageConvertSettings(imageAdapter);
            imageConvertSettings.ScaleImage = true; // scale images
            var imageConverter = new RtfImageConverter(imageConvertSettings);

            // rtf interpreter
            var interpreterSettings = new RtfInterpreterSettings();

            var rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, imageConverter);

            var htmlSegments = await RtfToHtmlSegments(rtfDocument.VisualContent);

            return(htmlSegments);
        }
示例#7
0
        // ----------------------------------------------------------------------
        public string ConvertRtf2Txt(string fileName)
        {
            // parser
            IRtfGroup rtfStructure = ParseRtf(fileName);

            if (rtfStructure == null)
            {
                return(string.Empty);
            }

            // interpreter logger
            RtfInterpreterListenerFileLogger interpreterLogger = null;

            if (!string.IsNullOrEmpty(InterpreterLogFileName))
            {
                interpreterLogger = new RtfInterpreterListenerFileLogger(InterpreterLogFileName);
            }

            // text converter
            RtfTextConvertSettings textConvertSettings = new RtfTextConvertSettings();

            textConvertSettings.BulletText = "-";
            RtfTextConverter textConverter = new RtfTextConverter(textConvertSettings);

            // text interpreter
            RtfInterpreterTool.Interpret(rtfStructure, interpreterLogger, textConverter);
            return(textConverter.PlainText);
        }         // ConvertRtf2Txt
示例#8
0
        private static string GetHtmlFromRTF(string rtfSelection)
        {
            IRtfDocument     rtfDocument   = RtfInterpreterTool.BuildDoc(rtfSelection);
            RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);

            return(htmlConverter.Convert());
        }
示例#9
0
        }         // EnsureDestinationDirectory

        // ----------------------------------------------------------------------
        private IRtfDocument InterpretRtf(IRtfGroup rtfStructure)
        {
            IRtfDocument rtfDocument;
            RtfInterpreterListenerFileLogger interpreterLogger = null;

            try
            {
                // logger
                if (settings.LogInterpreter)
                {
                    string logFileName = settings.BuildDestinationFileName(
                        settings.LogDirectory,
                        RtfInterpreterListenerFileLogger.DefaultLogFileExtension);
                    interpreterLogger = new RtfInterpreterListenerFileLogger(logFileName);
                }


                // rtf parser
                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger);
            }
            catch (Exception e)
            {
                if (interpreterLogger != null)
                {
                    interpreterLogger.Dispose();
                }

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

            return(rtfDocument);
        }         // InterpretRtf
示例#10
0
        public void TextAlignmentTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_3.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger);

            Assert.IsNotNull(rtfDoc);
            IRtfVisualCollection rtfVisuals = rtfDoc.VisualContent;

            Assert.AreEqual(8, rtfVisuals.Count);
            Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[0].Kind);
            Assert.AreEqual("left aligned", ((IRtfVisualText)rtfVisuals[0]).Text);
            Assert.AreEqual(RtfTextAlignment.Left, ((IRtfVisualText)rtfVisuals[0]).Format.Alignment);
            Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[2].Kind);
            Assert.AreEqual("centered", ((IRtfVisualText)rtfVisuals[2]).Text);
            Assert.AreEqual(RtfTextAlignment.Center, ((IRtfVisualText)rtfVisuals[2]).Format.Alignment);
            Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[4].Kind);
            Assert.AreEqual("right aligned", ((IRtfVisualText)rtfVisuals[4]).Text);
            Assert.AreEqual(RtfTextAlignment.Right, ((IRtfVisualText)rtfVisuals[4]).Format.Alignment);
            Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[6].Kind);
            Assert.AreEqual("block aligned", ((IRtfVisualText)rtfVisuals[6]).Text);
            Assert.AreEqual(RtfTextAlignment.Justify, ((IRtfVisualText)rtfVisuals[6]).Format.Alignment);
        }         // TextAlignmentTest
示例#11
0
        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());
        } // ConvertRtfToHtml
示例#12
0
        private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, RtfVisualImageAdapter imageAdapter)
        {
            IRtfDocument            rtfDocument          = null;
            RtfImageConverter       imageConverter       = null;
            RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter);

            imageConvertSettings.ImagesPath      = ImagePath;
            imageConvertSettings.BackgroundColor = null;
            imageConverter = new RtfImageConverter(imageConvertSettings);
            rtfDocument    = RtfInterpreterTool.BuildDoc(rtfStructure, null, imageConverter);
            return(rtfDocument);
        }
示例#13
0
        }         // EnsureDestinationDirectory

        // ----------------------------------------------------------------------
        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;
                    imageConvertSettings.ScaleImage      = !settings.UnscaledImages;
                    if (settings.ExtendedImageScale)
                    {
                        imageConvertSettings.ScaleExtension = 0.5f;
                    }
                    imageConverter = new RtfImageConverter(imageConvertSettings);
                }

                // rtf interpreter
                RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
                interpreterSettings.IgnoreDuplicatedFonts = settings.IgnoreDuplicatedFonts;
                interpreterSettings.IgnoreUnknownFonts    = settings.IgnoreUnknownFonts;

                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, 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);
        }         // InterpretRtf
示例#14
0
        /// <summary>
        /// Do the actual conversion by using a RichTextBox
        /// </summary>
        private void Convert()
        {
            if (string.IsNullOrEmpty(_rtf))
            {
                _convertedRtf = string.Empty;
                return;
            }

            IRtfDocument     rtfDocument = RtfInterpreterTool.BuildDoc(_rtf);
            RtfHtmlConverter converter   = new RtfHtmlConverter(rtfDocument);

            _convertedRtf = converter.Convert();
        }
示例#15
0
        }         // ToXmlButtonClick

        // ----------------------------------------------------------------------
        private void ToHtmlButtonClick(object sender, EventArgs e)
        {
            try
            {
                IRtfDocument     rtfDocument   = RtfInterpreterTool.BuildDoc(ConversionText);
                RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);
                textBox.Text = htmlConverter.Convert();
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, "Error " + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        } // ToHtmlButtonClick
示例#16
0
        }         // SetupDefaultText

        // ----------------------------------------------------------------------
        private void ToTextButtonClick(object sender, EventArgs e)
        {
            try
            {
                IRtfGroup        rtfStructure  = RtfParserTool.Parse(ConversionText);
                RtfTextConverter textConverter = new RtfTextConverter();
                RtfInterpreterTool.Interpret(rtfStructure, textConverter);
                textBox.Text = textConverter.PlainText;
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, "Error " + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }         // ToTextButtonClick
示例#17
0
    private string GetBibDescr(string s)
    {
        IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(s);
        string       ret         = "";

        foreach (IRtfVisual vt in rtfDocument.VisualContent)
        {
            if (vt.Kind == RtfVisualKind.Text)
            {
                ret += ((IRtfVisualText)vt).Text;
            }
        }
        return(ret);
    }
示例#18
0
        /// <summary>
        /// Convert RTF to HTML
        /// </summary>
        /// <param name="rtf">The rtf string</param>
        /// <returns></returns>
        public static string ConvertRtfToHtml(string rtf)
        {
            if (string.IsNullOrEmpty(rtf))
            {
                return(string.Empty);
            }

            var rtfDocument = RtfInterpreterTool.BuildDoc(rtf,
                                                          new RtfInterpreterSettings {
                IgnoreDuplicatedFonts = true, IgnoreUnknownFonts = true
            });
            var converter = new RtfHtmlConverter(rtfDocument);

            return(converter.Convert());
        }
示例#19
0
        }         // EnsureDestinationDirectory

        // ----------------------------------------------------------------------
        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();
                }

                MessageBox(IntPtr.Zero, "Error while interpreting RTF:\n" + e.Message, "Itenso.Rtf2Html", 0x010000);
                ExitCode = ProgramExitCode.InterpretRtf;
                return(null);
            }

            return(rtfDocument);
        }         // InterpretRtf
示例#20
0
        private static IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter, string imageAdapterLogFile = null, RtfImageConvertSettings imageConvertSettings = null, bool throwOnError = false)
        {
            IRtfDocument rtfDocument;
            RtfInterpreterListenerFileLogger interpreterLogger = null;

            try
            {
                // logger
                if (imageAdapterLogFile != null)
                {
                    interpreterLogger = new RtfInterpreterListenerFileLogger(imageAdapterLogFile);
                }

                // image converter
                RtfImageConverter imageConverter = null;

                if (imageConvertSettings != null)
                {
                    imageConverter = new RtfImageConverter(imageConvertSettings);
                }

                // rtf interpreter
                RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
                interpreterSettings.IgnoreDuplicatedFonts = true;
                interpreterSettings.IgnoreUnknownFonts    = true;

                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, interpreterLogger, imageConverter);
            }
            catch (Exception e)
            {
                if (interpreterLogger != null)
                {
                    interpreterLogger.Dispose();
                }

                if (throwOnError)
                {
                    ThrowOnUnexpectedExitCode(e);
                }

                return(null);
            }

            return(rtfDocument);
        }
示例#21
0
        } // EnsureDestinationDirectory

        // ----------------------------------------------------------------------
        private IRtfDocument InterpretRtf(IRtfGroup rtfStructure, IRtfVisualImageAdapter imageAdapter)
        {
            IRtfDocument rtfDocument;

            try
            {
                // image converter
                RtfImageConverter imageConverter = null;
                if (settings.SaveImage)
                {
                    RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter);
                    imageConvertSettings.ImagesPath = settings.DestinationDirectory;
                    if (settings.ImageBackgroundColor.HasValue)
                    {
                        imageConvertSettings.BackgroundColor = new Model.RtfColor(settings.ImageBackgroundColor.Value.R
                                                                                  , settings.ImageBackgroundColor.Value.G, settings.ImageBackgroundColor.Value.B);
                    }
                    imageConvertSettings.ScaleImage = !settings.UnscaledImages;
                    if (settings.ExtendedImageScale)
                    {
                        imageConvertSettings.ScaleExtension = 0.5f;
                    }
                    imageConverter = new RtfImageConverter(imageConvertSettings);
                }

                // rtf interpreter
                RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
                interpreterSettings.IgnoreDuplicatedFonts = settings.IgnoreDuplicatedFonts;
                interpreterSettings.IgnoreUnknownFonts    = settings.IgnoreUnknownFonts;

                // interpret the rtf structure using the extractors
                rtfDocument = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, imageConverter);
            }
            catch (Exception e)
            {
                Console.WriteLine("error while interpreting rtf: " + e.Message);
                ExitCode = ProgramExitCode.InterpretRtf;
                return(null);
            }

            return(rtfDocument);
        } // InterpretRtf
示例#22
0
        public void ExtractPlainTextTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_0.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            RtfTextConverter textConverter = new RtfTextConverter();

            RtfInterpreterTool.Interpret(rtfStructure, textConverter, interpreterLogger);

            string plainText = textConverter.PlainText;

            Assert.AreEqual("Hello RTF World\r\n", plainText);
        }         // ExtractPlainTextTest
示例#23
0
        public string CovnvertRtftoHtml(string rtf)
        {
            const RtfParserListenerLogger parserLogger = null;

            IRtfGroup rtfStructure = RtfParserTool.Parse(rtf, parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;

            RtfTextConverter       textConverter       = new RtfTextConverter();
            IRtfDocument           rtfDocument         = RtfInterpreterTool.BuildDoc(rtfStructure, textConverter, interpreterLogger);
            RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings();

            htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content;
            RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);
            string           plainText     = htmlConverter.Convert();

            plainText = plainText.Replace("@?]e1", "á").Replace("@?]e9", "é").Replace("@?]f3", "ó").Replace("@?]ed", "í").Replace("@?]c1", "á").Replace("@?]f1", "ñ").Replace("@?]fa", "ú"); //aqui reemplazamos para letras con acentos.
            return(plainText);
        }
示例#24
0
        }         // ToXmlButtonClick

        // ----------------------------------------------------------------------
        private void ToHtmlButtonClick(object sender, RoutedEventArgs e)
        {
            try
            {
                TextRange conversionText = ConversionText;
                using (MemoryStream stream = new MemoryStream())
                {
                    conversionText.Save(stream, DataFormats.Rtf);
                    stream.Seek(0, SeekOrigin.Begin);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        IRtfDocument     rtfDocument   = RtfInterpreterTool.BuildDoc(reader);
                        RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);
                        textBox.Text = htmlConverter.Convert();
                    }
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, "Error " + exception.Message, Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        } // ToHtmlButtonClick
示例#25
0
        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);
            }
        }
示例#26
0
        public void ListSupportTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();

            //string testCaseName = "RtfInterpreterTest_9";
            const string testCaseName = "RtfInterpreterTest_10";
            IRtfGroup    rtfStructure = RtfParserTool.Parse(GetTestResource(testCaseName + ".rtf"), parserLogger);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            RtfTextConverter textConverter = new RtfTextConverter();

            RtfInterpreterTool.Interpret(rtfStructure, textConverter, interpreterLogger);

            string plainText = textConverter.PlainText;

            AssertEqualLines("list support:",
                             new StreamReader(GetTestResource(testCaseName + ".txt"), RtfSpec.AnsiEncoding),
                             new StringReader(plainText));
        }         // ListSupportTest
示例#27
0
        public void UnicodeSupportTest()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_2.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            RtfTextConverter textConverter = new RtfTextConverter();

            RtfInterpreterTool.Interpret(rtfStructure, textConverter, interpreterLogger);

            string plainText = textConverter.PlainText;

            AssertEqualLines("unicode support:",
                             new StreamReader(GetTestResource("RtfInterpreterTest_2.txt"), RtfSpec.AnsiEncoding),
                             new StringReader(plainText));
        }         // UnicodeSupportTest
示例#28
0
        public void TextAlignmentTestFixed()
        {
            const RtfParserListenerLogger parserLogger = null;
            //parserLogger = new RtfParserListenerLogger();
            IRtfGroup rtfStructure =
                RtfParserTool.Parse(GetTestResource("RtfInterpreterTest_23.rtf"), parserLogger);

            Assert.IsNotNull(rtfStructure);

            const RtfInterpreterListenerLogger interpreterLogger = null;
            //interpreterLogger = new RtfInterpreterListenerLogger();

            IRtfDocument rtfDoc = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterLogger);

            Assert.IsNotNull(rtfDoc);
            IRtfVisualCollection rtfVisuals = rtfDoc.VisualContent;

            Assert.AreEqual(2, rtfVisuals.Count);
            Assert.AreEqual(RtfVisualKind.Text, rtfVisuals[0].Kind);
            Assert.AreEqual("Simple text", ((IRtfVisualText)rtfVisuals[0]).Text);
            Assert.AreEqual(RtfTextAlignment.Right, ((IRtfVisualText)rtfVisuals[0]).Format.Alignment);
        }         // TextAlignmentTestFixed
示例#29
0
        }         // ToTextButtonClick

        // ----------------------------------------------------------------------
        private void ToXmlButtonClick(object sender, EventArgs e)
        {
            try
            {
                IRtfDocument rtfDocument = RtfInterpreterTool.BuildDoc(ConversionText);

                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                xmlWriterSettings.Indent      = true;
                xmlWriterSettings.IndentChars = ("  ");
                StringBuilder sb = new StringBuilder();
                using (XmlWriter writer = XmlWriter.Create(sb, xmlWriterSettings))
                {
                    RtfXmlConverter xmlConverter = new RtfXmlConverter(rtfDocument, writer);
                    xmlConverter.Convert();
                    writer.Flush();
                    textBox.Text = sb.ToString();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(this, "Error " + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }         // ToXmlButtonClick
示例#30
0
        }         // ConvertRtf2Txt

        // ----------------------------------------------------------------------
        public string ConvertRtf2Html(string fileName)
        {
            // parser
            IRtfGroup rtfStructure = ParseRtf(fileName);

            if (rtfStructure == null)
            {
                return(string.Empty);
            }

            // interpreter logger
            RtfInterpreterListenerFileLogger interpreterLogger = null;

            if (!string.IsNullOrEmpty(InterpreterLogFileName))
            {
                interpreterLogger = new RtfInterpreterListenerFileLogger(InterpreterLogFileName);
            }

            // image converter
            RtfVisualImageAdapter   imageAdapter         = new RtfVisualImageAdapter(ImageFormat.Jpeg);
            RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter);

            imageConvertSettings.ScaleImage = true;             // scale images
            RtfImageConverter imageConverter = new RtfImageConverter(imageConvertSettings);

            // rtf interpreter
            RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
            IRtfDocument           rtfDocument         = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, interpreterLogger, imageConverter);

            // html converter
            RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings();

            htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content;
            RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument);

            return(htmlConverter.Convert());
        }         // ConvertRtf2Html