How to convert simple RichText tags to HTML in Delphi?

You can say that there are a lot of discussions in stackOverflow, but most of them are more complicated than what I need, and mostly for other languages.

I have a remote MySQL database in which I have a Help table with code to populate the help pages of a dynamic website using this database.

I decided to make a Delphi application to manage this site instead of making the website itself for more speed and security.

I want to put TRichEdit to make this help text, and use simple things like alignment, bold, italics and underlined styles. I do not want to use pictures and fonts.

How to select this rich styled text and convert it to HTML to put in my BLOB field in a remote database and then convert to rich text if I want to edit it again

+7
source share
2 answers

After several solutions that did not give accurate results, I was inspired by this solution: Convert RTF to HTML and HTML to RTF .

The idea is that the TWebBrowser control (in design / edit mode) can process and transform correctly. Rich text format when it was pasted from the clipboard.

 uses SHDocVw, MSHTML; function ClipboardToHTML(AParent: TWinControl): WideString; var wb: TWebBrowser; function WaitDocumentReady: Boolean; var StartTime: DWORD; begin StartTime := GetTickCount; while wb.ReadyState <> READYSTATE_COMPLETE do begin Application.HandleMessage; if GetTickCount >= StartTime + 2000 then // time-out of max 2 sec begin Result := False; // time-out Exit; end; end; Result := True; end; begin Result := ''; wb := TWebBrowser.Create(nil); try wb.Silent := True; wb.Width := 0; wb.Height := 0; wb.Visible := False; TWinControl(wb).Parent := AParent; wb.HandleNeeded; if wb.HandleAllocated then begin wb.Navigate('about:blank'); (wb.Document as IHTMLDocument2).designMode := 'on'; if WaitDocumentReady then begin (wb.Document as IHTMLDocument2).execCommand('Paste', False, 0); Result := (wb.Document as IHTMLDocument2).body.innerHTML; end; end; finally wb.Free; end; end; procedure TForm1.Button1Click(Sender: TObject); begin RichEdit1.SelectAll; RichEdit1.CopyToClipboard; ShowMessage(ClipboardToHTML(Self)); end; 
+5
source

If you really want to create RTF content using TRichEdit , then you must save the native RTF that it creates with the converted HTML. If the only reason you use TRichEdit is because you can have simple formatting capabilities, then you are probably better off using an HTML control that generates its own HTML content.

No matter how you go, it’s better to keep your own format for users to edit the content, and convert it as needed to other formats (instead of converting it in both directions).

If you use TRichEdit , you can easily transfer the contents of the RTF to and from the control, although I recommend TJvRichEdit over TRichEdit :

 procedure GetRTFData(MS: TMemoryStream; RTF: TRichEdit); begin MS.Clear; RTF.Lines.SaveToStream(MS); MS.Position := 0; end; procedure SetRTFData(MS: TMemoryStream; RTF: TRichEdit); begin MS.Position := 0; RTF.StreamFormat := sfRichText; RTF.Lines.LoadFromStream(MS); end; 

Manually converting RTF to HTML is no easy task. There are Unicode character considerations, font styles, font codes, paragraph formatting, numbered lists, special HTML characters, and more. Despite the fact that you just need to support simple formatting, users often use other functions that cause headaches for conversion - for example, copying content from MSWord and pasting it into your application with all types of formatting and fonts.

JvRichEditToHtml does a decent job converting RTF to HTML, but we ended up writing our own conversion unit because we do a lot more with RTF than simple formatting. JvRichEditToHtml should easily handle what you described if users do not enter complex content through copy / paste or use keyboard shortcuts to format the content (e.g. bullets = ctrl + shft + L, indent = ctrl + M, etc.).

There are also some good HTML controls for Delphi if you want to get around the complexity of authoring in RTF and converting to HTML:

Google Results :: Delphi, HTML, Editor, Component

Stack Overflow :: Delphi, HTML, editor, component

We use TRichView because of its extensive capabilities. It can load / create RTF and export HTML. However, this is not free. If you are looking for something free, TJvRichView and JvRichEditToHtml are good options.

+6
source

All Articles