Insert Background image in Tables via code

ScaleRichView support and discussion (TRichView add-on for WYSIWYG editing)
Post Reply
bvworld
Posts: 2
Joined: Fri Jun 08, 2018 2:18 pm

Insert Background image in Tables via code

Post 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
Sergey Tkachenko
Site Admin
Posts: 17520
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert Background image in Tables via code

Post 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;
Sergey Tkachenko
Site Admin
Posts: 17520
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert Background image in Tables via code

Post 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;
bvworld
Posts: 2
Joined: Fri Jun 08, 2018 2:18 pm

Re: Insert Background image in Tables via code

Post by bvworld »

Hello Sergey,

thanks for your help! Works!

Best Regards
Post Reply