2010. január 18., hétfő

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


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


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

{ ... }
  f: TPicture;
  c: TMetafileCanvas;
  fs: TMemoryStream;
  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);

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);
  wmfTag: string;
  HexEncoded: string;
  i: integer;
  HexEncoded := '';
  for i := 0 to BufLen - 1 do
    HexEncoded := HexEncoded + IntToHex(Buffer^, 2);
  {You gotta skip the wmf header}
  HexEncoded := Copy(HexEncoded, (Sizeof(LongInt) + Sizeof(SmallInt) + Sizeof(TSmallRect) +
    Sizeof(Word) + Sizeof(LongInt) + Sizeof(Word)) * 2 + 1,
  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));

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:

0 Verdana;}{\f1\fswiss\fcharset0 Arial;}   {\f2\fmodern\fprq1\fcharset0
Courier New;}}\viewkind4\uc1

and this on the bottom


Nincsenek megjegyzések:

Megjegyzés küldése