123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System.Windows.Forms;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Drawing;
- using System;
- using System.Linq;
- namespace wispro.sp.winClient
- {
-
- public class HtmlToRtfConverter
- {
- public string ConvertHtmlToRtf(string html)
- {
- try
- {
- using (RichTextBox rtBox = new RichTextBox())
- {
- string decodedHtml = HttpUtility.HtmlDecode(html);
- ProcessHtmlContent(rtBox, decodedHtml);
- return rtBox.Rtf;
- }
- }
- catch (Exception ex)
- {
- throw new Exception($"HTML转RTF时发生错误: {ex.Message}");
- }
- }
- private void ProcessHtmlContent(RichTextBox rtBox, string html)
- {
- // 首先处理段落
- var paragraphs = Regex.Split(html, @"</?p>")
- .Where(p => !string.IsNullOrWhiteSpace(p));
- foreach (var paragraph in paragraphs)
- {
- // 处理段落内的内联元素
- ProcessInlineElements(rtBox, paragraph);
- // 在段落后添加换行
- if (!rtBox.Text.EndsWith(Environment.NewLine))
- {
- rtBox.AppendText(Environment.NewLine);
- }
- }
- }
- private void ProcessInlineElements(RichTextBox rtBox, string html)
- {
- // 匹配内联元素,包括strike、u标签和br标签
- string pattern = @"<(?<tag>strike|u)[^>]*style=['""](?<style>[^'""]*)['""]>(?<text>[^<]*)</\k<tag>>|<br\s*/>|(?<text>[^<]+)";
- int lastIndex = 0;
- foreach (Match match in Regex.Matches(html, pattern))
- {
- // 处理标签之间的纯文本
- int textStart = match.Index;
- if (textStart > lastIndex)
- {
- string plainText = html.Substring(lastIndex, textStart - lastIndex);
- if (!string.IsNullOrWhiteSpace(plainText))
- {
- rtBox.AppendText(plainText);
- }
- }
- string tag = match.Groups["tag"].Value;
- string style = match.Groups["style"].Value;
- string text = match.Groups["text"].Value;
- if (match.Value.StartsWith("<br"))
- {
- // 处理换行标签
- rtBox.AppendText(Environment.NewLine);
- }
- else if (!string.IsNullOrEmpty(tag))
- {
- // 处理格式化标签
- ApplyFormatting(rtBox, tag, style, text);
- }
- else if (!string.IsNullOrEmpty(text))
- {
- // 处理纯文本
- rtBox.AppendText(text);
- }
- lastIndex = match.Index + match.Length;
- }
- // 处理剩余的文本
- if (lastIndex < html.Length)
- {
- string remainingText = html.Substring(lastIndex);
- if (!string.IsNullOrWhiteSpace(remainingText))
- {
- rtBox.AppendText(remainingText);
- }
- }
- }
- private void ApplyFormatting(RichTextBox rtBox, string tag, string style, string text)
- {
- // 保存当前的格式设置
- Font originalFont = rtBox.SelectionFont ?? rtBox.Font;
- Color originalColor = rtBox.SelectionColor;
- // 设置字体样式
- FontStyle fontStyle = FontStyle.Regular;
- Color textColor = originalColor;
- // 根据标签和样式设置格式
- if (tag == "strike" || style.Contains("line-through"))
- {
- fontStyle |= FontStyle.Strikeout;
- }
- if (tag == "u" || style.Contains("underline"))
- {
- fontStyle |= FontStyle.Underline;
- }
- // 处理颜色
- var colorMatch = Regex.Match(style, @"color:\s*([^;]+)");
- if (colorMatch.Success)
- {
- try
- {
- string colorValue = colorMatch.Groups[1].Value.Trim();
- textColor = ColorTranslator.FromHtml(colorValue);
- }
- catch { /* 忽略无效的颜色值 */ }
- }
- // 应用格式
- rtBox.SelectionStart = rtBox.TextLength;
- rtBox.SelectionLength = 0;
- rtBox.SelectionFont = new Font(originalFont.FontFamily, originalFont.Size, fontStyle);
- rtBox.SelectionColor = textColor;
- // 添加文本
- rtBox.AppendText(text);
- // 恢复原始格式
- rtBox.SelectionFont = originalFont;
- rtBox.SelectionColor = originalColor;
- }
- }
- }
|