Example: table to text

<< Click to display table of contents >>

Example: table to text

This example "removes" the table at the caret position without removing its content.

This is an editing operation, it can be undone by the user.

procedure ConvertTableToText(RichViewEdit: TCustomRichViewEdit); 

var table: TRVTableItemInfo

    rv: TRichView

    rve: TCustomRichViewEdit

    Stream: TMemoryStream; 

    ItemNo,r,c: Integer; 

begin 

  if not RichViewEdit.GetCurrentItemEx(TRVTableItemInfo, rve,

    TCustomRVItemInfo(table)) then 

    exit; 

  rv := TRichView.Create(nil); 

  try 

    rv.Visible := False; 

    rv.Parent := RichViewEdit.Parent; 

    rv.Style := RichViewEdit.Style

    rv.RVFTextStylesReadMode := rvf_sIgnore; 

    rv.RVFParaStylesReadMode := rvf_sIgnore; 

    for r := 0 to table.RowCount-1 do 

      for c := 0 to table.ColCount-1 do 

        if table.Cells[r,c]<>nil then

        begin 

          Stream := TMemoryStream.Create; 

          try 

            table.Cells[r,c].GetRVData.SaveRVFToStream(Stream, 

              False, clNone, nilnil); 

            Stream.Position := 0

            rv.InsertRVFFromStream(Stream, rv.ItemCount

          finally 

            Stream.Free; 

          end

        end

    // GetMyItemNo is like rve.GetItemNo(table), but faster

    ItemNo := table.GetMyItemNo; 

    Stream := TMemoryStream.Create; 

    try 

      rv.SaveRVFToStream(Stream, False); 

      Stream.Position := 0

      rve.SetSelectionBounds(ItemNo, 0, ItemNo, 1); 

      rve.InsertRVFFromStreamEd(Stream); 

    finally 

      Stream.Free; 

    end

  finally 

    rv.Free; 

  end

end

The same for C++Builder:

void ConvertTableToText(TCustomRichViewEdit* RichViewEdit) 

  TCustomRVItemInfo   *item; 

  TCustomRichViewEdit *rve; 

  if (! RichViewEdit->GetCurrentItemEx(__classid(TRVTableItemInfo),

    rve, item)) 

    return

  TRVTableItemInfo*table = (TRVTableItemInfo*)item; 

  TRichView* rv = new TRichView((TComponent*)NULL); 

  rv->Visible = false

  rv->Parent  = RichViewEdit->Parent; 

  rv->Style   = RichViewEdit->Style; 

  rv->RVFTextStylesReadMode = rvf_sIgnore; 

  rv->RVFParaStylesReadMode = rvf_sIgnore; 

  for (int r=0; r<table->RowCount; r++) 

    for (int c=0; c<table->ColCount; c++) 

      if (table->Cells[r][c]) 

      { 

        TMemoryStream*Stream = new TMemoryStream; 

        table->Cells[r][c]->GetRVData()->SaveRVFToStream(Stream, false,

          clNone, NULL, NULL); 

        Stream->Position = 0

        rv->InsertRVFFromStream(Stream, rv->ItemCount); 

        delete Stream; 

      } 

  int ItemNo = table->GetMyItemNo(); 

  TMemoryStream*Stream = new TMemoryStream; 

  rv->SaveRVFToStream(Stream, false); 

  Stream->Position = 0

  rve->SetSelectionBounds(ItemNo, 0, ItemNo, 1); 

  rve->InsertRVFFromStreamEd(Stream); 

  delete Stream; 

  delete rv;