trichview.com

trichview.support.examples




Example 2: making table from document


Return to index


Author

Message

Sergey Tkachenko

Posted: 11/07/2004 20:11:37


This procedure creates a table 1x2, clears the document and add this table.

If the original document had N paragraphs, the first (N+1) div 2 paragraphs

are copied in

the first column of this table, the rest of these paragraphs are copied to

the second column.


This procedure works both for TRichView and TRichViewEdit.

Unlike Example 1, this procedure cannot be undone.

Document must be formatted before calling this procedure (because it uses

selecting).

Call rv.Format after calling this procedure.


procedure CreateTableFromRV(rv: TCustomRichView);

var MidNo, i, ParaCount, ParaCount1: Integer;

    table: TRVTableItemInfo;

    Stream: TMemoryStream;

    Clr: TColor;

begin

  if rv.ItemCount=0 then

    exit;

  SendMessage(rv.Handle, WM_SETREDRAW, 0, 0);

  try

    // ParaCount := number of paragraphs

    ParaCount := 0;

    for i := 0 to rv.ItemCount-1 do

      if rv.IsParaStart(i) then

        inc(ParaCount);

    // ParaCount1 := number of paragraphs in the first column

    ParaCount1 := (ParaCount+1) div 2;

    // MidNo := index of the last item to copy in the first column

    MidNo := rv.ItemCount-1;

    if ParaCount>1 then begin

      ParaCount := 0;

      for i := 0 to rv.ItemCount-1 do

        if rv.IsParaStart(i) then begin

          inc(ParaCount);

          if ParaCount=ParaCount1+1 then begin

            MidNo := i-1;

            break;

          end;

        end;

    end;

    // creating table 1 x 2

    table := TRVTableItemInfo.CreateEx(1, 2, rv.RVData);

    table.CellBorderWidth := 1;

    table.CellBorderStyle := rvtbColor;

    table.CellHSpacing := -1;

    table.CellVSpacing := -1;

    table.BorderHSpacing := 0;

    table.BorderVSpacing := 0;

    // copying the first column

    Stream := TMemoryStream.Create;

    rv.SetSelectionBounds(0, rv.GetOffsBeforeItem(0),

      MidNo, rv.GetOffsAfterItem(MidNo));

    rv.SaveRVFToStream(Stream, True);

    Stream.Position := 0;

    table.Cells[0,0].LoadRVFFromStream(Stream, Clr, nil, nil);

    Stream.Free;

    // copying the second column

    if MidNo<>rv.ItemCount-1 then begin

      Stream := TMemoryStream.Create;

      rv.SetSelectionBounds(MidNo+1, rv.GetOffsBeforeItem(MidNo+1),

        rv.ItemCount-1, rv.GetOffsAfterItem(rv.ItemCount-1));

      rv.SaveRVFToStream(Stream, True);

      Stream.Position := 0;

      table.Cells[0,1].LoadRVFFromStream(Stream, Clr, nil, nil);

      Stream.Free;

    end;

    // inserting table

    rv.Clear;

    rv.AddItem('', table);

  finally

    SendMessage(rv.Handle, WM_SETREDRAW, 1, 0);

  end;

end;






Powered by ABC Amber Outlook Express Converter