Author: Tomas Rutkauskas
How to copy RTF text to a TMemo via a TStringStream
Answer:
Here is a method to copy the formatted text to a memo via a stringstream. The same
approach should work to store it in a database field that takes a string.
1 2 procedure TForm1.Button1Click(Sender: TObject);
3 var4 ss: TStringStream;
5 begin6 ss := TStringStream.Create(EmptyStr);
7 try8 richedit1.plaintext := false;
9 richedit1.lines.savetostream(ss);
10 memo1.text := ss.DataString;
11 finally12 ss.free;
13 end;
14 end;
15 16 procedure TForm1.Button2Click(Sender: TObject);
17 var18 ss: TStringStream;
19 begin20 ss := TStringStream.Create(memo1.text);
21 try22 richedit1.plaintext := false;
23 richedit1.lines.LoadFromstream(ss);
24 finally25 ss.free;
26 end;
27 end;
To simply copy formatted text from a TRichEdit to a string, you could also use:
28 29 function GetRawRTFText(aRichedit: TRichedit): string;
30 var31 SS: TStringstream;
32 begin33 SS := TStringstream.Create(EmptyStr);
34 try35 aRichedit.plaintext := False;
36 arichedit.Lines.SaveToStream(SS);
37 Result := SS.DataString;
38 finally39 SS.Free
40 end;
41 end;