首页 > 解决方案 > 在 C#.NET 中使用字符串 RichTexBox 的内联 CSS(包括颜色、字体等)创建 HTML

问题描述

基本上我正在开发一个电子邮件营销软件,我必须发送包含正确格式文本正文的电子邮件。现在我正在尝试使用内联 CSS 将字符串转换为 Html。但是 CSS 无法正常工作。

这是richtextbox中的文本

在此处输入图像描述

将文本文件转换为 Html 的 C# 代码。

       // Help from: https://sautinsoft.com/products/rtf-to-html/convert-text-to-html-dotnet-csharp.php

        SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
     
        string htmlFile1 = Path.ChangeExtension(textfile, ".html");

        r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.HTML_5;
        r.Encoding = SautinSoft.RtfToHtml.eEncoding.UTF_8;
        r.TextStyle.InlineCSS = true;
        r.ConvertFile(textfile, htmlFile);
        // Open the stream and read it back.
        string htmlString = "";
        using (StreamReader sr = File.OpenText(htmlFile1))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null)
            {    if(!s.Contains("unlicensed"))
                htmlString = htmlString + s;
            }
        }

html输出

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="SautinSoft.RtfToHtml.dll">
<title>Untitled document</title>
<style type="text/css"></style>
</head>
<body>
<div>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">Dear Khuram Shahzad, </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">                                  I am your Business Partner, Anjum.</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">&nbsp;</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">Regards,</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">Anjum,</span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;">Software Engineer FAST.</span></p>
</div><div style="text-align:center;">The unlicensed version of &laquo;RTF to HTML .Net&raquo;.<br><a href="https://www.sautinsoft.com/products/rtf-to-html/order.php">Get the full featured version!</a></div>
</body>
</html>

Html 的输出是: 在此处输入图像描述

但它没有在 Html 中显示颜色?

标签: c#windowsvisual-studioemail

解决方案


您可以尝试使用以下代码从richtextbox 创建具有格式的html。

private void button1_Click(object sender, EventArgs e)
        {
            SautinSoft.RtfToHtml r = new SautinSoft.RtfToHtml();
            r.OutputFormat = SautinSoft.RtfToHtml.eOutputFormat.HTML_401;
            r.ImageStyle.IncludeImageInHtml = false;
            string rtf =box.Rtf ;
            string html = r.ConvertString(rtf);
            File.WriteAllText("test.html", html);
        }
        RichTextBox box = new RichTextBox();
        private void Form1_Load(object sender, EventArgs e)
        {

            box.Size = new Size(600, 300);
            box.AppendText("Dear Friend",Color.Black,new Font("Times New Roman", 10));
            box.AppendText(Environment.NewLine);
            box.AppendText(Environment.NewLine);
            box.AppendText(Environment.NewLine);
            box.AppendText(Environment.NewLine);
            box.AppendText(Environment.NewLine);
            box.AppendText("Regards", Color.Black, new Font("Times New Roman", 10));
            box.AppendText(Environment.NewLine);
            box.AppendText("Anjum", Color.Red, new Font("Bodoni MT Black", 20));
            this.Controls.Add(box);
        }

public static class RichTextBoxExtensions
    {
        public static void AppendText(this RichTextBox box, string text, Color color,Font font)
        {
            box.SelectionStart = box.TextLength;
            box.SelectionLength = 0;
            box.Font = font;
            box.SelectionColor = color;
            box.AppendText(text);
            box.SelectionColor = box.ForeColor;
        }
    }

注意:我使用 RichTextBoxExtensions 覆盖 AppendText 方法,以便进行测试。

Winform富文本框:

在此处输入图像描述

在 html 中:

在此处输入图像描述


推荐阅读