RTF to HTML - styling

1 Answer 124 Views
WordsProcessing
CPS
Top achievements
Rank 1
Iron
Iron
CPS asked on 27 Sep 2023, 10:20 AM

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);
This code works . The output looks like this:
<body><p style="margin-bottom: 0px;"><span style="font-size: 13.333333333333334px;">Dampf- und Gassperre aus Fl&uuml;ssigkunststoff f&uuml;r D&auml;mmung von K&auml;lteleitungen. Flexible, schwerentflammbare und dampfbremsende Beschichtung auf Wasserbasis. Auch geeignet zum Schutz von l&ouml;sungsmittelempfindlichen Isolierstoffenbei K&auml;lte-Isolierungen im Innen- und Aussenbereich.</span></p></body>
How can I remove or replace the style-attribute with the font-size in the span?
Roger
Top achievements
Rank 2
commented on 28 Sep 2023, 02:31 AM | edited

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;
}

1 Answer, 1 is accepted

Sort by
0
Anna
Telerik team
answered on 29 Sep 2023, 11:10 AM

Hi,

The style attribute reflects the properties of this particular span that have been imported from the rtf and stored in the RadFlowDocument object. You can edit the font size of this run before exporting the document to html and this will be reflected in the html style tag:

// Getting the first run of the file. The run will then be exported as span in the html.
Run firstRun = rtfDoc.EnumerateChildrenOfType<Run>().First();

// We can delete the value like this.
firstRun.Properties.FontSize.ClearValue();

// Or replace it like this.
firstRun.FontSize = 20;

I hope I understood your question correctly. Please, let me know if any further clarification is needed. 

Regards,
Anna
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
WordsProcessing
Asked by
CPS
Top achievements
Rank 1
Iron
Iron
Answers by
Anna
Telerik team
Share this question
or