Page 1 of 1

Paste without font formatting

Posted: Mon Apr 19, 2010 9:05 pm
by Tobias
I need to paste formatted text, but without font face. In my application users can't change font, but can use formatting like bold, italic, can insert images and use bullets and tables. So I need all this things in pasted data, but with default font used in my application.
I understand that I must use OnPaste event, but I don't understand how exactly...

Posted: Tue Apr 20, 2010 8:38 am
by Sergey Tkachenko
1) If you have a very limited set of formatting, you can add all possible formatting in RVStyle.TextStyles (if you use one font, of the same color, of the same size, only 4 items are required: normal, bold, italic, bold italic).
After that, you can assign properties so that new formatting will not be added on pasting RVF or RTF. Instead, the most similar of existing formatting will be used.
The simplest way to do it: at design time, right click RichViewEdit, choose "Settings" in the context menu, select "Use a predefined set of styles".
The only disadvantage: this mode of insertion is not implemented for RTF bullets&numbering, so text will be pasted from RTF without bullets and numbering.

2) In case if you have too many possible formatting options to add them initially (for example, if user can define arbitrary text color), you need using OnPaste.
Create rveHidden: TRichViewEdit, with Visible=False, linked to its own RVStyle control.

Code: Select all

procedure TForm1.RichViewEdit1Paste(Sender: TCustomRichViewEdit;
  var DoDefault: Boolean);
var Stream: TMemoryStream;
begin
  rveHidden.Clear;
  rveHidden.DeleteUnusedStyles(True, True, True);
  rveHidden.Format;
  rveHidden.Paste;
  <process rveHidden.Style, changing properties of items in collections of styles;
  do not worry that styles with identical properties will appear:
  InsertRVFFromStream will "compact" collections
  >
  Stream := TMemoryStream.Create;
  rveHidden.SaveRVFToStream(Stream, False);
  Stream.Position := 0;
  RichViewEdit1.InsertRVFFromStreamEd(Stream);
  Stream.Free;
  DoDefault := False;
end;

Posted: Thu Apr 22, 2010 10:40 pm
by Tobias
Thanks, it works for me, but I think you mean InsertRVFFromStreamEd :wink:

Posted: Fri Apr 23, 2010 5:20 pm
by Sergey Tkachenko
Thank you, I fixed the sample code.