c# - 在 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;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </span></p>
<p style="margin:0pt 0pt 8pt 0pt;line-height:1.079167;"><span style="font-family:Calibri;font-size:11pt;color:#000000;;"> </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 «RTF to HTML .Net».<br><a href="https://www.sautinsoft.com/products/rtf-to-html/order.php">Get the full featured version!</a></div>
</body>
</html>
但它没有在 Html 中显示颜色?
解决方案
您可以尝试使用以下代码从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 中:
推荐阅读
- airflow - Airflow:SubDagOperator 的成功基于什么?
- mysql - MySQL:选择多个子查询的结果时,“子查询返回超过 1 行”
- node.js - 如何让 CucumberJS 在步骤定义中识别我的场景大纲参数?
- python - 从结构不均匀的字符串中提取日期
- apache - MAMP Apache 服务器 - 一个子目录有效,另一个无效
- electron - Electron-mksnapshot snapshotResult.setGlobals 位置?
- angular - Angular Material:选择列表示例不起作用
- c++ - 链表 push_front 和 push_back 中的 C++ 内存泄漏
- angular - TypeError:无法读取未定义的属性-但站点可以正常工作-Angular 7
- vbscript - 如何从 VBScript 最大化屏幕?