I have this method that parses my rich text to HTML. I noticed that is some places it is composing the inline style like this:
<DIV STYLE="text-align:Left;font-family:Segoe UI;font-style:normal;font-weight:normal;font-size:12;color:#000000;">
notice the font-size is missing the ‘pt’ at the end, which is breaking the report when I export to pdf.
Here is my code to handle it:
public static string RtfToHtml(string rtfText)
{
if (String.IsNullOrEmpty(rtfText)) return rtfText;
if (!rtfText.Contains(@"{\rtf1")) return rtfText.Replace("\r\n", "<br>").Replace("\r", "<br>");
Converter converter = new Converter();
StringBuilder sb = new StringBuilder(converter.ConvertRtfToHtml(rtfText));
sb.Replace("font-size:12;", "font-size:12pt;");
try
{
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(sb.ToString());
RemoveStyleTags(doc, "ol");
RemoveStyleTags(doc, "ul");
RemoveStyleTags(doc, "li");
return doc.DocumentNode.InnerHtml;
}
catch { }
return sb.ToString();
}
My question is this: Is there a more elegant way to perform the .Replace() method rather that doing this:
sb.Replace("font-size:12;", "font-size:12pt;");
sb.Replace("font-size:13;", "font-size:13pt;");
sb.Replace("font-size:14;", "font-size:14pt;");
...
sb.Replace("font-size:10000;", "font-size:10000pt;");
Obviously what I am trying to do is find all the mistyped font-size declaration, append the pt, while keeping the integer size they already have.
You can use Regex.Replace(string input, string pattern, string replacement), like so: