Can I merge 2 RTFStrings on a non visible TRichviewEdit

General TRichView support forum. Please post your questions here
Post Reply
LuckySpacy
Posts: 7
Joined: Fri Nov 22, 2013 7:45 am

Can I merge 2 RTFStrings on a non visible TRichviewEdit

Post by LuckySpacy »

// Here is the working code

Code: Select all

procedure TfrmTest.Button8Click(Sender: TObject);
var
  s: string;
  List : TStringList;
  m    : TMemoryStream;
begin
  RichViewEdit1.Style := RvStyle1;
  s := MergeRTF3(_Artikel.getLangBezRTF, _KuText.getLangTextRTF); [color=red]// Different to Button7Click[/color]
  RichViewEdit1.Clear;
  List := TStringList.Create;
  m    := TMemoryStream.Create;
  try
    List.Text := s;
    List.SaveToStream(m);
    m.Position := 0;
    RichViewEdit1.LoadRTFFromStream(m);
  finally
    FreeAndNil(List);
    FreeAndNil(m);
  end;
  RichViewEdit1.Format;
end;

// This works fine
function TfrmTest.MergeRTF3(aRTFString1, aRTFString2: string): string;
var
  List: TStringList;
  m   : TMemoryStream;
  rv  : TRichViewEdit;
  ItemNo, Offs: Integer;
begin
  rv := RichViewEdit1;  [color=red] // The different RichViewEdit is on the Form [/color]

  // Load RTFString1 in RichviewEdit
  List := TStringList.Create;
  m    := TMemoryStream.Create;
  try
    List.Text := aRTFString1;
    m.Position := 0;
    List.SaveToStream(m);
    m.Position := 0;
    rv.LoadRTFFromStream(m);
    rv.Format;
  finally
    FreeAndNil(List);
    FreeAndNil(m);
  end;

  // Move Caret to the end
  ItemNo := rv.ItemCount-1;
  Offs   := rv.GetOffsAfterItem(ItemNo);
  rv.SetSelectionBounds(ItemNo,Offs,ItemNo,Offs);

  // Insert RTFString2 in RichviewEdit
  List := TStringList.Create;
  m    := TMemoryStream.Create;
  try
    List.Text := aRTFString2;
    m.Position := 0;
    List.SaveToStream(m);
    m.Position := 0;
    rv.InsertRTFFromStreamEd(m);
    rv.Format;
  finally
    FreeAndNil(List);
    FreeAndNil(m);
  end;

  // New RTFString
  List := TStringList.Create;
  m    := TMemoryStream.Create;
  try
    m.Position := 0;
    rv.SaveRTFToStream(m, false);
    m.Position := 0;
    List.LoadFromStream(m);
    Result := List.Text;
  finally
    FreeAndNil(List);
    FreeAndNil(m);
  end;

end;


-------------------------------------------------------------------------------------
This is what I want, but this is not working, the style is lost

Code: Select all

procedure TfrmTest.Button7Click(Sender: TObject);
var
  s: string;
  List : TStringList;
  m    : TMemoryStream;
begin
  RichViewEdit1.Style := RvStyle1;
  s := MergeRTF2(_Artikel.getLangBezRTF, _KuText.getLangTextRTF);
  RichViewEdit1.Clear;

  List := TStringList.Create;
  m    := TMemoryStream.Create;
  try
    List.Text := s;
    List.SaveToStream(m);
    m.Position := 0;
    RichViewEdit1.LoadRTFFromStream(m);
  finally
    FreeAndNil(List);
    FreeAndNil(m);
  end;
  RichViewEdit1.Format;
end;

// This don't work.
function TfrmTest.MergeRTF2(aRTFString1, aRTFString2: string): string;
var
  List: TStringList;
  m   : TMemoryStream;
  rv  : TRichViewEdit;
  ItemNo, Offs: Integer;
begin
  rv   := TRichViewEdit.Create(Self);  [color=red] // The different create RichViewEdit [/color]
  try
    rv.Parent := Self;
    rv.Style  := RvStyle1;

    // Load RTFString1 in RichviewEdit
    List := TStringList.Create;
    m    := TMemoryStream.Create;
    try
      List.Text := aRTFString1;
      m.Position := 0;
      List.SaveToStream(m);
      m.Position := 0;
      rv.LoadRTFFromStream(m);
      rv.Format;
    finally
      FreeAndNil(List);
      FreeAndNil(m);
    end;

    // Move Caret to the end
    ItemNo := rv.ItemCount-1;
    Offs   := rv.GetOffsAfterItem(ItemNo);
    rv.SetSelectionBounds(ItemNo,Offs,ItemNo,Offs);

    // Insert RTFString2 in RichviewEdit
    List := TStringList.Create;
    m    := TMemoryStream.Create;
    try
      List.Text := aRTFString2;
      m.Position := 0;
      List.SaveToStream(m);
      m.Position := 0;
      rv.InsertRTFFromStreamEd(m);
      rv.Format;
    finally
      FreeAndNil(List);
      FreeAndNil(m);
    end;

    // New RTFString
    List := TStringList.Create;
    m    := TMemoryStream.Create;
    try
      m.Position := 0;
      rv.SaveRTFToStream(m, false);
      m.Position := 0;
      List.LoadFromStream(m);
      Result := List.Text;
    finally
      FreeAndNil(List);
      FreeAndNil(m);
    end;

  finally
    FreeAndNil(rv);
  end;
end;
LuckySpacy
Posts: 7
Joined: Fri Nov 22, 2013 7:45 am

Solution

Post by LuckySpacy »

I've got it!
TRvReporthelper is my friend.

Here is my solution:

Code: Select all

function TfrmTest.MergeRTF(aRTFString1, aRTFString2: string): string;
var
  List: TStringList;
  m   : TMemoryStream;
  rh  : TRvReportHelper;
  rv  : TReportRichView;
  Style: TRvStyle;
begin
  rh    := TRvReportHelper.Create(Self);
  List  := TStringList.Create;
  m     := TMemoryStream.Create;
  Style := TRvStyle.Create(nil);
  try
    rh.RichView.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded;
    rh.RichView.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded;
    rh.RichView.RVFOptions := rh.RichView.RVFOptions + [rvfoSaveTextStyles];
    rh.RichView.RVFOptions := rh.RichView.RVFOptions + [rvfoSaveParaStyles];
    rv       := rh.RichView;
    rv.Style := Style;

    // Load RTFString1
    List.Text := aRTFString1;
    m.Position := 0;
    List.SaveToStream(m);
    m.Position := 0;
    rv.LoadRTFFromStream(m);


    // Load RTFString2
    List.Text := aRTFString2;
    m.Clear;
    m.Position := 0;
    List.SaveToStream(m);
    m.Position := 0;
    rv.LoadRTFFromStream(m);

    // New RTFString
    m.Position := 0;
    rv.SaveRTFToStream(m, false);
    m.Position := 0;
    List.LoadFromStream(m);
    Result := List.Text;

  finally
    FreeAndNil(List);
    FreeAndNil(m);
    FreeAndNil(Style);
    FreeAndNil(rh);
  end;
end;
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

TRichView controls created at runtime have different initial values of properties than controls placed on a form at designtime.
(we cannot change initial runtime values of properties because of compatibility reasons).
In the last code fragments, you change these properties:

Code: Select all

    rh.RichView.RTFReadProperties.TextStyleMode := rvrsAddIfNeeded; 
    rh.RichView.RTFReadProperties.ParaStyleMode := rvrsAddIfNeeded; 
    rh.RichView.RVFOptions := rh.RichView.RVFOptions + [rvfoSaveTextStyles]; 
    rh.RichView.RVFOptions := rh.RichView.RVFOptions + [rvfoSaveParaStyles]; 
This change allows loading RVF and RTF files without losing format.

Actually, you can do similar assignment to rv in your initial non-working code.
Post Reply