Insert Image at position in TCustomRVData

General TRichView support forum. Please post your questions here
Post Reply
HansDampf
Posts: 3
Joined: Mon Jun 15, 2020 2:04 pm

Insert Image at position in TCustomRVData

Post by HansDampf »

Hello Sergej,

I have a procedure with only a TCustomRVData and a matching pattern for the position and I want to replace the pattern with a Bitmap.

Code: Select all

Item.Inserting(aRVData, sr, False);
aRVData.Items.InsertObject(i, sr, Item);
Item.Inserted(aRVData, i);
adds the image but in front of the table and

Code: Select all

ItemNo := RVData.ItemCount - 1;
Offs := RVData.GetOffsAfterItem(ItemNo) - 1;
RVData.SetSelectionBounds(ItemNo, Offs, ItemNo, Offs); 
leads to an error in TCustomRVFormattedData.Item2DrawItem at

Code: Select all

DrawItemOffs := ItemOffs-DrawItems[DrawItemNo].Offs+1;
Do I need to get the TRichViewCustomEdit (if yes, how?).

Thanks for your help.

Best, Hans
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert Image at position in TCustomRVData

Post by Sergey Tkachenko »

SetSelectionBounds needs a formatted document. Adding items in aRVData.Items makes the document unformatted, so you need to call Format method when you finished making all changes.

BTW, instead of the code you use for inserting Item, use RVInsertItem from RVInsertItems unit. It does the same, and still requires calling Format after, but it hides low-level details.

If you do multiple replacements, calling multiple RVInsertItem and then Format is a good idea.

Editing methods (like InsertPicture) are better if you need a single replacement. They reformat the changed part of a document themselves.
HansDampf
Posts: 3
Joined: Mon Jun 15, 2020 2:04 pm

Re: Insert Image at position in TCustomRVData

Post by HansDampf »

Thanks for you reply, but could you help me with the code? I only have the TCustomRVData and don't know, how to get or format the document.
This is my code so far to find the placeholders:

Code: Select all

begin
  if not FileExists(aFileName) then Exit;

  Image := TPicture.Create;
  Image.LoadFromFile(aFileName);

  Item := TRVGraphicItemInfo.CreateEx(aRVData, Image.Graphic, rvvaBaseline);

  // iterate through tables
  for i := 0 to aRVData.ItemCount - 1 do
  begin
    table := TRVTableItemInfo(aRVData.GetItem(i));
    if Assigned(table.Rows) then
    begin
       for row := 0 to table.Rows.Count - 1 do
        for col := 0 to table.Rows[row].Count - 1 do
          while True do
          begin
            cellContent := table.Cells[row,col].GetRVData.GetItemText(0);
            stringIndex := Pos(aText, cellContent);

            // if placeholder does not exist in cell -> break loop
            if stringIndex = 0 then Break;

            Delete(cellContent, stringIndex, Length(aText));

            table.Cells[row, col].GetRVData.SetItemText(0, cellContent);

            //TODO: insert Image at stringIndex
          end;
    end;
  end;

  Image.Free;
end;
And I just cannot work out, how to insert the image at stringIndex.

Thanks!
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert Image at position in TCustomRVData

Post by Sergey Tkachenko »

You can get the it as TCustomRichView(aRVData.GetAbsoluteRootData.GetParentControl)

PS: I hope this is not a final code.
You assume that all items in aRVData are tables. If not, it will crash.
You assume that tables do not have merged cells (= nil). If not, it will crash.
You check text only inside table cells, not inside aRVData itself.
You do not check tables nested in tables.
You assume that there is exactly one place where this image will be inserted (maybe there is not field for this image, so you do not need to create it and its item? maybe there are more than one field, and you need to create several images and its items?)
I suggest to check MailMerge-Text4 demo: https://www.trichview.com/forums/viewto ... 707#p12707
It shows how to implement fields that have values of several types, including pictures.
HansDampf
Posts: 3
Joined: Mon Jun 15, 2020 2:04 pm

Re: Insert Image at position in TCustomRVData

Post by HansDampf »

Is there a guide on how to use TRichView or at least an overview of the necessary components?
I just started using your product and don't know where to find all the informations.

Thanks
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: Insert Image at position in TCustomRVData

Post by Sergey Tkachenko »

1) Manual: https://www.trichview.com/help/ (also available as CHM in TRichView\Help\, and as PDF https://www.trichview.com/rvfiles/rv_pdf_manual.zip )
2) Tutorial demo projects (very basic): TRichView\Demos\DelphiUnicode\Tutorials.groupproj
3) Other demo projects: TRichView\Demos\DelphiUnicode\Demos.groupproj
4) Examples in the forum: https://www.trichview.com/forums/viewforum.php?f=3
Post Reply