Mirror

How to create and insert a *.wmf into an *.rtf file (Views: 100)


Problem/Question/Abstract:

How to create and insert a *.wmf into an *.rtf file

Answer:

Well, create a metafile with the old wmf format (with enhanced = false). Code like this works:


{ ... }
var
  f: TPicture;
  c: TMetafileCanvas;
  fs: TMemoryStream;
begin
  f := TPicture.create;
  f.Metafile.width := 100;
  f.Metafile.height := 100;
  f.Metafile.Enhanced := false;
  c := TMetafileCanvas.create(f.Metafile, 0);
  c.Ellipse(5, 5, 95, 95);
  c.Free;
end;


Get the bytes of the metafile, put in a buffer and call this function:


procedure TRtfWriter.InsertWMFFromBuffer(Buffer: PByte; const BufLen: integer;
  iWidth, iHeight: integer);
var
  wmfTag: string;
  HexEncoded: string;
  i: integer;
begin
  HexEncoded := '';
  for i := 0 to BufLen - 1 do
  begin
    HexEncoded := HexEncoded + IntToHex(Buffer^, 2);
    Inc(Buffer);
  end;
  {You gotta skip the wmf header}
  HexEncoded := Copy(HexEncoded, (Sizeof(LongInt) + Sizeof(SmallInt) + Sizeof(TSmallRect) +
    Sizeof(Word) + Sizeof(LongInt) + Sizeof(Word)) * 2 + 1,
    Length(HexEncoded));
  HexEncoded := LowerCase(HexEncoded);
  wmfTag := '{\pict\wmetafile8\picw%d\pich%d %s }';
  wmfTag := Format(wmfTag, [iWidth * 20, iHeight * 20, HexEncoded]);
  fStream.Write(wmfTag[1], Length(wmfTag));
end;



Note that fStream is a stream with the rtf file my TRtfWriter class is working on. You'll have to the the rtf job yourself, but that's the way to inser a wmf file. If you want a quick test, place this on the top of the file:



{\rtf1\ansi\ansicpg1252\deff0\deflang1046{\fonttbl{\f0\fswiss\fprq2\fcharset
0 Verdana;}{\f1\fswiss\fcharset0 Arial;}   {\f2\fmodern\fprq1\fcharset0
Courier New;}}\viewkind4\uc1


and this on the bottom


\par}

<< Back to main page