예제 #1
0
        private static object FromHtmlToRtf(ICoreItem coreItem, BodyReadConfiguration configuration, Stream bodyStream, bool createReader)
        {
            HtmlToRtf htmlToRtf = new HtmlToRtf();

            htmlToRtf.InputEncoding             = ConvertUtils.GetItemMimeCharset(coreItem.PropertyBag).GetEncoding();
            htmlToRtf.DetectEncodingFromMetaTag = false;
            htmlToRtf.Header             = configuration.InjectPrefix;
            htmlToRtf.Footer             = configuration.InjectSuffix;
            htmlToRtf.HeaderFooterFormat = configuration.InjectionHeaderFooterFormat;
            if (configuration.ImageRenderingCallback != null)
            {
                TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToRtf, configuration.ImageRenderingCallback);
            }
            return(BodyReadDelegates.GetRtfStreamOrReader(bodyStream, htmlToRtf, createReader));
        }
예제 #2
0
        private static object FromHtmlToRtf(ICoreItem coreItem, BodyWriteConfiguration configuration, Stream bodyStream, bool createWriter)
        {
            Stream stream = null;
            object obj    = null;

            try
            {
                stream = new ConverterStream(bodyStream, new RtfToRtfCompressed(), ConverterStreamAccess.Write);
                stream = new BodyCharsetDetectionStream(stream, null, coreItem, BodyStreamFormat.RtfUncompressed, null, configuration.TargetCharset, configuration.TargetCharsetFlags, null, false);
                HtmlToRtf htmlToRtf = new HtmlToRtf();
                htmlToRtf.InputEncoding             = configuration.SourceEncoding;
                htmlToRtf.DetectEncodingFromMetaTag = false;
                htmlToRtf.Header             = configuration.InjectPrefix;
                htmlToRtf.Footer             = configuration.InjectSuffix;
                htmlToRtf.HeaderFooterFormat = configuration.InjectionHeaderFooterFormat;
                if (configuration.ImageRenderingCallback != null)
                {
                    TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToRtf, configuration.ImageRenderingCallback);
                }
                TextConverter converter = htmlToRtf;
                if (configuration.FilterHtml || configuration.InternalHtmlTagCallback != null)
                {
                    stream    = new ConverterStream(stream, htmlToRtf, ConverterStreamAccess.Write);
                    converter = new HtmlToHtml
                    {
                        InputEncoding             = configuration.SourceEncoding,
                        OutputEncoding            = configuration.SourceEncoding,
                        DetectEncodingFromMetaTag = false,
                        FilterHtml      = configuration.FilterHtml,
                        HtmlTagCallback = configuration.InternalHtmlTagCallback
                    };
                }
                obj = BodyWriteDelegates.GetConverterStreamOrWriter(stream, converter, createWriter);
            }
            finally
            {
                if (obj == null && stream != null)
                {
                    BodyWriteDelegates.SafeDisposeStream(stream);
                }
            }
            return(obj);
        }
예제 #3
0
        private static object FromHtmlToText(ICoreItem coreItem, BodyReadConfiguration configuration, Stream bodyStream, bool createReader)
        {
            HtmlToText htmlToText = new HtmlToText();

            htmlToText.InputEncoding             = ConvertUtils.GetItemMimeCharset(coreItem.PropertyBag).GetEncoding();
            htmlToText.OutputEncoding            = configuration.Encoding;
            htmlToText.DetectEncodingFromMetaTag = false;
            htmlToText.Header             = configuration.InjectPrefix;
            htmlToText.Footer             = configuration.InjectSuffix;
            htmlToText.HeaderFooterFormat = configuration.InjectionHeaderFooterFormat;
            htmlToText.ShouldUseNarrowGapForPTagHtmlToTextConversion = configuration.ShouldUseNarrowGapForPTagHtmlToTextConversion;
            htmlToText.OutputAnchorLinks = configuration.ShouldOutputAnchorLinks;
            htmlToText.OutputImageLinks  = configuration.ShouldOutputImageLinks;
            if (configuration.ImageRenderingCallback != null)
            {
                TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToText, configuration.ImageRenderingCallback);
            }
            return(BodyReadDelegates.GetTextStreamOrReader(bodyStream, htmlToText, createReader));
        }
예제 #4
0
        public static string HtmlToText(string html, bool shouldUseNarrowGapForPTagHtmlToTextConversion)
        {
            if (string.IsNullOrEmpty(html))
            {
                return(html);
            }
            html = TextConverterHelper.RemoveHtmlLink(html);
            string result;

            using (StringReader stringReader = new StringReader(html))
            {
                using (StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture))
                {
                    HtmlToText htmlToText = new HtmlToText();
                    htmlToText.InputEncoding  = Encoding.UTF8;
                    htmlToText.OutputEncoding = Encoding.UTF8;
                    htmlToText.ShouldUseNarrowGapForPTagHtmlToTextConversion = shouldUseNarrowGapForPTagHtmlToTextConversion;
                    TextConvertersInternalHelpers.SetImageRenderingCallback(htmlToText, new ImageRenderingCallback(TextConverterHelper.RemoveImageCallback));
                    htmlToText.Convert(stringReader, stringWriter);
                    result = stringWriter.ToString();
                }
            }
            return(result);
        }