SaveHTMLToStream and SaveTextToStream

General TRichView support forum. Please post your questions here
Post Reply
Splinter
Posts: 44
Joined: Mon Aug 29, 2005 6:08 pm

SaveHTMLToStream and SaveTextToStream

Post by Splinter »

Hi Sergey,

I am saving the RVE content to my database file as HTML, and that is working fine.

However, I have another component on the form that displays a sub-set of the 'plain text' from the RVE. What is the correct way to extract the plain text? I tried creating a TextStream and use SaveTextToStream, however it occasionally throws exceptions with the error "Exception class EEncodingError with message 'No mapping for the Unicode character exists in the target multi-byte code page'"

var
HTMLStream,TextStream:TStringStream;

HTMLStream:=TStringStream.Create('', TEncoding.UTF8);
TextStream:=TStringStream.Create('', TEncoding.UTF8);
rve.SaveHTMLToStream(HTMLStream);
rve.SaveTextToStream('',TextStream,80,false,true); //this line generates exception on some RVE content

myComponent.text := TextStream.DataString.

Thanks
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: SaveHTMLToStream and SaveTextToStream

Post by Sergey Tkachenko »

By default, SaveTextToStream saves text using the default Windows code page.
If you created TextStream with TEncoding.UTF8, you need to save UTF-8 as well:

rve.SaveTextToStream('',TextStream,80,false,true, CP_UTF8);
Splinter
Posts: 44
Joined: Mon Aug 29, 2005 6:08 pm

Re: SaveHTMLToStream and SaveTextToStream

Post by Splinter »

Great thanks Sergey, the exception errors have gone away with the addition of CP_UTF8.

On a related note, when I use rve.SaveHTMLToStream(HTMLStream), and save to the database, and then a subsequent rve.LoadHTMLFromStream(HTMLStream, '', CP_UTF8) from the database, an extra line appears at the top of the RVE. So if I type some text on the top line of the RVE, after a save/load it will be one line down, after another save/load it will be two lines down.

I've looked at the HTML code being saved/loaded and an extra <p><br></p> seems to get added on each save. Is there something I can configure to prevent that?

Procedure SaveRecord;
var
HTMLStream:TStringStream;
HTMLString:String;
begin
HTMLStream:=TStringStream.Create('', TEncoding.UTF8);
rve.SaveHTMLToStream(HTMLStream);
HTMLString := (HTMLStream as TStringStream).DataString;
//now save HTMLString to database
end;

Procedure LoadRecord;
var
HTMLString: String;
HTMLStream:TStringStream;
begin
HTMLString:=LoadFieldFromMyDatabase;
//showmessage(HTMLString); //after each save and load I can see extra <p><br></p> in <body>

HTMLStream:=TStringStream.Create(HTMLString, TEncoding.UTF8);
rve.Clear;
rve.Format;
rve.LoadHTMLFromStream(HTMLStream, '', CP_UTF8);
rve.Format;
end;
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: SaveHTMLToStream and SaveTextToStream

Post by Sergey Tkachenko »

Remove Format after Clear:

HTMLStream:=TStringStream.Create(HTMLString, TEncoding.UTF8);
rve.Clear;
rve.Format; <------------- REMOVE THIS
rve.LoadHTMLFromStream(HTMLStream, '', CP_UTF8);
rve.Format;
end;

If the editor is completely empty, rve.Format adds one empty text item.
LoadHTMLFromStream does not need document to be formatted, so it's enough to call Format one time, at the end.
Splinter
Posts: 44
Joined: Mon Aug 29, 2005 6:08 pm

Re: SaveHTMLToStream and SaveTextToStream

Post by Splinter »

Doh, I should have thought of trying that. Thanks it worked.
Post Reply