![](/img/trans.png)
[英]Display word/pdf/excel etc. files in browser. (Asp.net , C#.net 2008)
[英]Create HTML with inline CSS(including color, font etc.) of string richTexBox in C#.NET
基本上我正在开发 email 营销软件,我必须发送 email 包括格式正确的文本正文。 现在我正在尝试将字符串转换为 Html 与行 CSS。 但是 CSS 工作不正常。
这是richtextbox中的文本
C# 将文本文件转换为 Html 的代码。
// 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 Output
<;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 中没有显示颜色?
您可以尝试使用以下代码创建 html 格式为来自richtextbox。
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 中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.