C# - Convert HTML String to formated plain text - c#

Is there a way to convert a html string for example content saved using a wysiwyg editor such as tinyMCE to formatted plain text i.e. retain line breaks.
I've been looking at the HTML Agility Pack but there are no examples that show how you can achieve something like the above.
Basically I want send content saved using tinyMCE in an email notification but i need to ensure any styles and formatting saved by tinyMCE don't break the styles in my HTML email hence the need to convert the content to plain text.
Any examples would be appreciated.

Related

HTML to Text Email Converter in C#

I'm looking for code or a library in C# that convert HTML email format to plain text format. I want to send my emails with both format.
Also, I would like the href links to be converted to text as well. Exactly how mailchimp does it here: https://templates.mailchimp.com/resources/html-to-text/
Any idea?
You can use HtmlAgilityPack and loop through nodes to extract text. Example: Grab all text from html with Html Agility Pack

C# ASP.NET: How do I Turn Text into HTML

How would i show the output as HTML. I have tried HTML Decode and it still didn't work.
#section Grid {
#Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd()))
}
Edit: I am taking XML from output.InnerXml (A XMLDocument) and trying to put it into a HTML Document as HTML (As in <a> is a link and <img> is a picture and not Text)
It turns out I had to add #Html.Raw along with HtmlDecode for it to display correctly
#Html.Raw(Server.HtmlDecode(lister.gen(new System.IO.StreamReader(Server.MapPath("~/Grid.xml")).ReadToEnd())))
If you want to show the HTML in an HTML page, you need to use HTML encode, not decode. This will put the proper tags in to turn < and > (and other HTML elements.
ADDED due to comment:
If showing XML AS HTML is the goal, then you will end up extracting the XML, from the DOM and putting it into the format you desire. You can bind XML to a table if you are simply trying to get it into a grid. If you need sorting, etc, LINQ to XML works nicely.
ADDED - second edit
Your XML appears to be XHTML, so you can simply throw it into the stream at the correct location. I would create a server control and then Response.Write the XML from the server control. You will want the decoded version based on what you posted. I was assuming you wanted to show the XML in the page, which was incorrect.
There is one minor bit of an issue with the XML, as it contains paragraph and div tags inside of an anchor tag. Not illegal, but not necessary in this case.

Handling Hyperlinks in WPF RichTextBox Html?

I have a RichTextBox in a WPF application (C#.NET4.5). The RichTextBox is in Rtf format. I'm using a MarkupConverter to convert and save the RichTextBox.Text Rtf text back in the Db as Html. If a user opens an existing record, the markup converter converts from Html back into Rtf for display in the UI. All works well.
I have an issue trying to handle Hyperlinks. Because the Markup Converter has to convert from Rtf-Xaml-Html I don't believe that there is a Xaml tag which equates to Href or HYPERLINK as there is in Html or Rtf?
Therefore the converter is just stripping out the hyperlink.
Is there any way to handle this in Xaml?
Thanks

How to display RTF text in to tinyMCE editor

I am using tinyMCE editor. I have rtf formatted string in database column which I want to show in the browser editor i.e. tinyMCE. So that, user will edit the text as he wants. But for that I found I need to convert the rtf string in to xml to see it, since my tinyMCE is showing xmls properly. I am able to show the text from rtf string but I misses out the formatting like bold text, red colored text, \n etc. I want to retain all this formatting. How shall I retain the formatting or how shall I display the rtf text with the formatting or how shall I convert rtf text in to xml properly? I want to display rtf text like I am saving it in some rtf file and opening that file in ms word or open office word that way I want to see the format. rtf is Rich Text format.
You should convert your RTF to HTML and use that for TinyMCE, here is a code project article that will get you started :
http://www.codeproject.com/Articles/27431/Writing-Your-Own-RTF-Converter

iTextSharp 5.1.3 - Put HTML in Form Field using c#?

I have a PDF Form and I'm trying to put HTML formatted text into a field using itextsharp 5.1.3 using c#. I want the text to keep the same formatting as it has in the HTML. How can I do this?
I've searched everywhere and I haven't been able to find anything. The only examples I can find show how to convert an entire document, but none show a clear way to convert a string of HTML code to PDF format and set a Field with it.

Categories

Resources