Example #1
0
        public static string RtfToHtml(this string str, RtfHtmlConvertSettings htmlConvertSettings, bool throwOnError = false, IRtfParserListener listener = null, string destinationDirectory = null, RtfVisualImageAdapter imageAdapter = null, string imageAdapterLogFile = null, RtfImageConvertSettings imageConvertSettings = null)
        {
            IRtfGroup    rtfStructure;
            IRtfDocument rtfDocument = null;

            try
            {
                using (var stream = str.ToStream())
                {
                    // parse the rtf structure
                    var           structureBuilder = new RtfParserListenerStructureBuilder();
                    var           parser           = new RtfParser(structureBuilder);
                    DirectoryInfo destination;

                    if (destinationDirectory != null)
                    {
                        destination = new DirectoryInfo(destinationDirectory);
                    }

                    parser.IgnoreContentAfterRootGroup = true; // support WordPad documents

                    if (listener != null)
                    {
                        parser.AddParserListener(listener);
                    }

                    parser.Parse(new RtfSource(stream));
                    rtfStructure = structureBuilder.StructureRoot;

                    ThrowOnUnexpectedExitCode();

                    rtfDocument = InterpretRtf(rtfStructure, imageAdapter, imageAdapterLogFile, imageConvertSettings, throwOnError);

                    if (throwOnError)
                    {
                        ThrowOnUnexpectedExitCode();
                    }

                    // convert to hmtl

                    string html = ConvertHmtl(rtfDocument, imageAdapter, htmlConvertSettings, throwOnError);

                    if (throwOnError)
                    {
                        ThrowOnUnexpectedExitCode();
                    }

                    return(html);
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("RtfToHtml parser failed with error: {0}", e.Message);
            }

            return(null);
        }
Example #2
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
Example #3
0
        private string ConvertHmtl(IRtfDocument rtfDocument, RtfVisualImageAdapter imageAdapter)
        {
            RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings(imageAdapter);

            htmlConvertSettings.Title                   = "Doc Title " + DateTime.Now.ToString("dd/MM/yyyy");
            htmlConvertSettings.ImagesPath              = ImagePath;
            htmlConvertSettings.IsShowHiddenText        = IsShowHiddenText;
            htmlConvertSettings.ConvertVisualHyperlinks = ConvertVisualHyperlinks;
            RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings);

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

        // ----------------------------------------------------------------------
        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);
                if (!settings.UseInlineStyles)
                {
                    htmlConverter.StyleConverter = new RtfEmptyHtmlStyleConverter();
                }
                html = htmlConverter.Convert();
            }
            catch (Exception e)
            {
                Console.WriteLine("error while converting to html: " + e.Message);
                ExitCode = ProgramExitCode.ConvertHtml;
                return(null);
            }

            return(html);
        }         // ConvertHmtl
Example #5
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);
        }
Example #6
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);
            }
        }
Example #7
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
Example #8
0
        private static string ConvertHmtl(IRtfDocument rtfDocument, IRtfVisualImageAdapter imageAdapter, RtfHtmlConvertSettings htmlConvertSettings, bool throwOnError = false)
        {
            string html = null;

            try
            {
                var htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings);
                html = htmlConverter.Convert();
            }
            catch (Exception e)
            {
                if (throwOnError)
                {
                    ThrowOnUnexpectedExitCode(e);
                }
            }

            return(html);
        }
        public static string ConvertRtf2Html(string text, int slideID)
        {
            // parser
            text = text.Replace("\\tab", "$$$$$$$$");
            text = text.Replace("li240", "$$$$$$$$$$$$$$$$");
            text = text.Replace("li480", "$$$$$$$$$$$");
            text = text.Replace("ri480", "$$$$$$$$$$$");
            text = text.Replace("li720", "$$$$$$$$$$$$$$");



            IRtfGroup rtfStructure = ParseRtf(text);

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

            // image converter

            RtfVisualImageAdapter imageAdapter = new RtfVisualImageAdapter(
                Path.GetFileNameWithoutExtension(Form1.currentFile) + "\\Slide" + slideID + "_{0}{1}", ImageFormat.Jpeg);
            RtfImageConvertSettings imageConvertSettings = new RtfImageConvertSettings(imageAdapter);

            imageConvertSettings.ScaleImage = true; // scale images'

            string filePath      = Path.GetDirectoryName(Form1.currentFile);
            string folder        = Path.GetFileNameWithoutExtension(Form1.currentFile);
            string directoryPath = filePath + "\\" + folder;

            //string directoryPath = @"C:\Users\Knwal\Documents\Presentation Tool\Presentation Tool\Preview\";

            imageConvertSettings.ImagesPath = directoryPath;
            RtfImageConverter      imageConverter      = new RtfImageConverter(imageConvertSettings);
            Stream                 st                  = GenerateStreamFromString(text);
            RtfInterpreterSettings interpreterSettings = new RtfInterpreterSettings();
            IRtfDocument           rtfDocument         = RtfInterpreterTool.BuildDoc(rtfStructure, interpreterSettings, null, imageConverter);

            // html converter

            RtfHtmlConvertSettings htmlConvertSettings = new RtfHtmlConvertSettings(imageAdapter);

            htmlConvertSettings.StyleSheetLinks.Add("default.css");
            htmlConvertSettings.ConvertScope = RtfHtmlConvertScope.Content;
            //htmlConvertSettings.GetImageUrl();

            RtfHtmlConverter htmlConverter = new RtfHtmlConverter(rtfDocument, htmlConvertSettings);
            string           str           = htmlConverter.Convert();

            str = str.Replace("  ", "  ");
            str = str.Replace("$$$$$$$$", "     ");
            str = str.Replace("$$$", "   ");
            //str= str.Replace("#1#","");
            //str = str.Replace("#2#", "");
            //str = str.Replace("#3#", "");
            //str = str.Replace("#4#", "");
            //str = str.Replace("#5#", "");
            //str = str.Replace("#6#", "");
            //str = str.Replace("#7#", "");
            //str = str.Replace("#8#", "");
            //str = str.Replace("#9#", "");


            return(str);
        } // ConvertRtf2Html