We store some texts in rtf-format in our database. Now I have to convert them to html.
This is an expample of the rtf-text:
{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Arial;}{\f1\fnil\fcharset0 MS Sans Serif;}}
\viewkind4\uc1\pard\fs20 Dampf- und Gassperre aus Fl\'fcssigkunststoff f\'fcr D\'e4mmung von K\'e4lteleitungen. Flexible, schwerentflammbare und dampfbremsende Beschichtung auf Wasserbasis. Auch geeignet zum Schutz von l\'f6sungsmittelempfindlichen Isolierstoffenbei K\'e4lte-Isolierungen im Innen- und Aussenbereich.\f1\fs17\par
}
To convert it to html I use the following code:
Telerik.Windows.Documents.Flow.FormatProviders.Rtf.RtfFormatProvider rtfProv = new();
Telerik.Windows.Documents.Flow.Model.RadFlowDocument rtfDoc = rtfProv.Import(rtfInput);
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlExportSettings expSettings = new()
{
DocumentExportLevel = Telerik.Windows.Documents.Flow.FormatProviders.Html.DocumentExportLevel.Fragment,
StylesExportMode = Telerik.Windows.Documents.Flow.FormatProviders.Html.StylesExportMode.None,
};
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider htmlProv = new()
{
ExportSettings = expSettings
};
string ret = htmlProv.Export(rtfDoc);
return new HtmlString(ret);
<body><p style="margin-bottom: 0px;"><span style="font-size: 13.333333333333334px;">Dampf- und Gassperre aus Flüssigkunststoff für Dämmung von Kälteleitungen. Flexible, schwerentflammbare und dampfbremsende Beschichtung auf Wasserbasis. Auch geeignet zum Schutz von lösungsmittelempfindlichen Isolierstoffenbei Kälte-Isolierungen im Innen- und Aussenbereich.</span></p></body>
if you use HTMLAgility you can loop through all span tags (or others you may want) and look for the style attribute for each and replace it with what you want (or remove it)
here is how you would do that:
HtmlAgilityPack.HtmlDocument htmlBodyText = new HtmlAgilityPack.HtmlDocument(); htmlBodyText.LoadHtml(BodyText); if (BodyText.ToLower().Contains("<span")) { foreach (HtmlAgilityPack.HtmlNode dNode in htmlBodyText.DocumentNode.SelectNodes("//span")) { string nodeStyle = dNode.GetAttributeValue("style", ""); //only do this if the span has the style element you are looking for if (nodeStyle.ToLower().Contains("font-size")) { string replacementStyle = ""; foreach (string styleItem in nodeStyle.Split(";")) { if (styleItem.ToLower().StartsWith("font-size")) { // comment out the below if you want to skip it) replacementStyle += "font-size: 14px;"; } else { //put other styles back replacementStyle += styleItem + ";"; } } dNode.SetAttributeValue("style", replacementStyle); } } BodyText = htmlBodyText.DocumentNode.InnerHtml; }