Page 1 of 1

Insert Background image in Tables via code

Posted: Sun Jul 07, 2019 3:39 pm
by bvworld
Hello,
currently I insert background images in tables over the table properties dialog. It works but it is not so useful for customers. Is there a way to insert background images via code? (like for pictures in tables: SRichViewEdit.ActiveEditor.InsertPicture('', Gr, rvvaBaseline)

Thanks!
Best Regards

Re: Insert Background image in Tables via code

Posted: Sun Jul 07, 2019 4:58 pm
by Sergey Tkachenko
If you have a Table object, you can simply assign BackgroundImage property:

Code: Select all

Table.BackgroundImage := gr;
This code copies an image from gr, you still need to free gr.
However, if Table.BackgroundStyle = rvbsColor, this image will not be displayed. You need to assign rvbsStretched, rvbsTiled, or rvbsCentered.


The code below loads an image from a file, and assigns it as a background image for the current table in SRichViewEdit1.
Since there are two operations here (changing a background image and a background style), we group changes, so they can be undone as a single operation.

Code: Select all

var
  gr: TJPEGImage;
  rve: TCustomRichViewEdit;
  Table: TRVTableItemInfo;
begin
  if not SRichViewEdit1.ActiveEditor.GetCurrentItemEx(
    TRVTableItemInfo, rve, TCustomRVItemInfo(Table)) then
    exit;
  rve.BeginUndoGroup(rvutModifyItem);
  rve.SetUndoGroupMode(True);
  try
    gr := TJPEGImage.Create;
    try
      gr.LoadFromFile('d:\Test\oldpaper.jpg');
      Table.BackgroundImage := gr;
      Table.BackgroundStyle := rvbsTiled;
    finally
      gr.Free;
    end;
  finally
    rve.SetUndoGroupMode(False);
  end;
  rve.RefreshAll;
end;

Re: Insert Background image in Tables via code

Posted: Sun Jul 07, 2019 5:04 pm
by Sergey Tkachenko
If you want to change a cell background image, the code is similar.
A direct assignment (cannot be undone):

Code: Select all

Table.Cells[0, 0].BackgroundImage := gr;
Table.Cells[0, 0].BackgroundStyle := rvbsTiled;
If you want to change as an editing operation (that can be undone), use SetCellBackgroundImage and SetCellBackgroundStyle instead:

Code: Select all

var
  gr: TJPEGImage;
  rve: TCustomRichViewEdit;
  Table: TRVTableItemInfo;
begin
  if not RichViewEdit1.GetCurrentItemEx(
    TRVTableItemInfo, rve, TCustomRVItemInfo(Table)) then
    exit;
  rve.BeginUndoGroup(rvutModifyItem);
  rve.SetUndoGroupMode(True);
  try
    gr := TJPEGImage.Create;
    try
      gr.LoadFromFile('d:\Test\oldpaper.jpg');
      Table.SetCellBackgroundImage(gr, 0, 0);
      Table.SetCellBackgroundStyle(rvbsTiled, 0, 0);
    finally
      gr.Free;
    end;
  finally
    rve.SetUndoGroupMode(False);
  end;
  rve.RefreshAll;
end;

Re: Insert Background image in Tables via code

Posted: Mon Jul 08, 2019 5:40 pm
by bvworld
Hello Sergey,

thanks for your help! Works!

Best Regards