WPCubed Product Support

TextBox: How to add image?

In my document I have a left page margin of 6cm so that I have quite some space to add custom things on the left side. I want to use this space to add text boxes and position them to fixed positions.

This works well as long as I have simple text in the boxes. But I also want to add an image to the box and that fails. I used TWPORTFTextBox.AddSubImage for that purpose but I can't see the image. Here is my code:

procedure AddLogo;
var
  lTextObj: TWPTextObj;
  lTextBox: TWPORTFTextBox;
  Img: TWPOImage;
begin
  Img:=TWPOImage.Create(FRtfDoc);
  Img.LoadFromFile('C:\tmp\flag.png');     
  Img.WidthTW:=Img.ContentsWidth;
  Img.HeightTW:=Img.ContentsHeight;

  lTextBox := TWPORTFTextBox.Create(FRtfDoc);
  lTextBox.WidthTW  := WPCentimeterToTwips(4.2);
  lTextBox.HeightTW := WPCentimeterToTwips(2.5);
  lTextBox.ObjName  := '_AUTO_' + IntToStr(GetTickCount);

  lTextBox.AddSubImage('Logo', Img);
  lTextObj := FRtfDoc.TextObjects.InsertMovableImage(lTextBox);

  if lTextObj <> nil then
  begin
    lTextObj.Mode := lTextObj.Mode + [ wpobjObjectUnderText, wpobjCreateAutoName ];
    lTextObj.RelX := WPCentimeterToTwips(0);
    lTextObj.RelY := WPCentimeterToTwips(9);
  end;
end;

Can you please correct my code or tell me another approach to add an image to a text box?

Many thanks in advance,
Dirk.

#1 ♥ 0
6k Views 2 users read 2 min
support

Hi,

AddSubImage is reserved for certain object types and require a custom object implementation.

To insert an image into a TextObject yopu need to place the cursor inside and add the image using TextObjects as if it was in regular text.

#2 ♥ 0

Thank you for your answer. Could I ask you to also add a few lines of code that demonstrate that because I am not sure what you exactly mean.

Thank you very much.

#3 ♥ 0
support

Hi,

To insert an image inside a texbox use this code:

var RTFData : TWPRTFDataBlock;
begin
WPRichText1.TextObjects.InsertTextBox(2000,4000, RTFData);
WPRichText1.ActiveText := RTFData;
WPRichText1.InsertGraphic('test.bmp');
end;

#4 ♥ 0
This category is archived. No new posts are possible.