Insert/Modify Image

Top  Previous  Next

To insert a picture in the OnMailMergeGetText event:

 

Since WPTools 9 You can simply call

 

Contents.LoadImageFromFile or Contents.LoadImageFromStream.

 

Before it was required to use the property Obj.

 

If You assign a new TWPObject instance to Contents.obj - that will be inserted into the merge field (and will be owned by the RTFEngine). But is is also possible (and recommended) to use the TWPObject from the previous mail merge run.

 

The field is inserted with:

TemplateEdit.InputMergeField('PICTURE','[img]');

 

This is the event handler (simplified, using 2 TImage objects)

 

procedure TWPEdTest.OnMailMergeGetText(Sender: TObject;

const inspname: String; Contents: TWPMMInsertTextContents);

var i : Integer;

   wpobj : TWPOImage;

begin

if inspname='PICTURE' then

begin

  // A) - there is no image in this field

  if Contents.CurrentObject=nil then

  begin

      wpobj := TWPOImage.Create(TemplateEdit);

      // wpobj.LoadFromFile()

      wpobj.Graphic := Image1.Picture.Graphic;

      wpobj.WidthTW := wpobj.ContentsWidth;

      wpobj.HeightTW:= wpobj.ContentsHeight;

      Contents.obj := wpobj;

  end

  else

  // B) There is an image, we replace the contents

  begin

     Contents.CurrentObject.Graphic

       := Image2.Picture.Graphic;

  end;

end;

end;

 

If you need to insert an image from a database you can use code like this (taken from unit WPDBRich = TWPMMDataProvider implementation)

 

if field is TGraphicField then

              wpobj.Picture.Assign(field)

else if field is TBlobField then

begin

              blobstream := TMemoryStream.Create;

              try

                TBlobField(field).SaveToStream(blobstream);

                blobstream.Position := 0;

                obj.FileExtension := 'BMP';

                obj.LoadFromStream(blobstream);

            except

              // We don't want to see any image errors here !

            end;

             blobstream.Free;

end;