[Example] How to export RTF having multicolumn sections

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] How to export RTF having multicolumn sections

Post by Sergey Tkachenko »

The current version of TRichView does not support multiple columns. But if your application needs to create RTF files having sections with multiple columns, it is possible.
You need to insert RTF code directly. The code is \sect\sbknone\colsN, where N is a column count.

There are two methods available:
1) Using text style with rvteoRTFCode option.
2) Using some control and OnSaveComponentToFile event.

The second method is preferred because you can customize how this RTF code looks like in TRichView, but I will show the both methods.
Last edited by Sergey Tkachenko on Fri Mar 19, 2010 7:28 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Inserting RTF code for column count using a special text style.

Code: Select all

var s: String;
    ColCount: Integer;
begin
  s := '1';
  if not InputQuery('Columns', 'Enter count of columns:', s) then
    exit;
  ColCount := StrToIntDef(s, 1);
  RichViewEdit1.CurTextStyleNo := GetRTFCodeStyleNo(RichViewEdit1.Style);
  RichViewEdit1.InsertText('\sect\sbknone\cols'+IntToStr(ColCount));
  // text after this position will be saved in RTF as ColCount columns
end;
The code above uses the function returning the index of "RTF code" text style:

Code: Select all

function GetRTFCodeStyleNo(RVStyle: TRVStyle): Integer;
var TextStyle: TFontInfo;
begin
  TextStyle := TFontInfo.Create(nil);
  TextStyle.Options := [rvteoRTFCode];
  TextStyle.Protection :=
    [rvprStyleProtect, rvprModifyProtect, rvprConcateProtect, 
rvprDoNotAutoSwitch];
  Result := RVStyle.TextStyles.FindSuchStyle(0, TextStyle, 
RVAllFontInfoProperties);
  if Result<0 then begin
    with RVStyle.TextStyles.Add do begin
      Assign(TextStyle);
      Standard := False;
    end;
    Result := RVStyle.TextStyles.Count-1;
  end;
  TextStyle.Free;
end;
The disadvantage of this method is in displaying RTF code in TRichView (it may confuse users).
Last edited by Sergey Tkachenko on Thu Sep 11, 2008 8:37 am, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Inserting RTF code for column count using controls

You can use any control, in this example I am using TStaticText with Caption = column count.

1) Inserting:

Code: Select all

var s: String;
    ColCount: Integer;
    StaticText: TStaticText;
begin
  s := '1';
  if not InputQuery('Columns', 'Enter count of columns:', s) then
    exit;
  ColCount := StrToIntDef(s, 1);
  StaticText := TStaticText.Create(nil);
  StaticText.Font.Color := clYellow;
  StaticText.Color := clBlack;
  StaticText.Caption := IntToStr(ColCount);
  StaticText.Hint := IntToStr(ColCount)+' column(s)';
  RichViewEdit1.InsertControl('', StaticText, rvvaBaseline);
end;
2) OnSaveComponentToFile event:

Code: Select all

procedure TForm3.RichViewEdit1SaveComponentToFile(Sender: TCustomRichView;
  Path: TRVUnicodeString; SaveMe: TPersistent; SaveFormat: TRVSaveFormat;
  var OutStr: TRVUnicodeString);
begin
  if (SaveFormat=rvsfRTF) and (SaveMe is TStaticText) then
    OutStr := '\sect\sbknone\cols'+TStaticText(SaveMe).Caption;
end;
3) TStaticText must be registered (one time, in the initialization section or when the main form is created:

Code: Select all

RegisterClass(TStaticText);
Update:
2018-Apr-9: for compatibility with TRichView 17.3
Sergey Tkachenko
Site Admin
Posts: 17566
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

These examples can be used as a template for inserting any codes in RTF or HTML files.
For HTML files, use rvteoHTMLCode instead of rvteoRTFCode (options of text style) and rvsfHTML instead of rvsfRTF (file format for OnSaveComponentToFile).
Post Reply