Author: Tomas Rutkauskas
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:
1 { ... }
2 var
3 f: TPicture;
4 c: TMetafileCanvas;
5 fs: TMemoryStream;
6 begin
7 f := TPicture.create;
8 f.Metafile.width := 100;
9 f.Metafile.height := 100;
10 f.Metafile.Enhanced := false;
11 c := TMetafileCanvas.create(f.Metafile, 0);
12 c.Ellipse(5, 5, 95, 95);
13 c.Free;
14 end;
Get the bytes of the metafile, put in a buffer and call this function:
15
16 procedure TRtfWriter.InsertWMFFromBuffer(Buffer: PByte; const BufLen: integer;
17 iWidth, iHeight: integer);
18 var
19 wmfTag: string;
20 HexEncoded: string;
21 i: integer;
22 begin
23 HexEncoded := '';
24 for i := 0 to BufLen - 1 do
25 begin
26 HexEncoded := HexEncoded + IntToHex(Buffer^, 2);
27 Inc(Buffer);
28 end;
29 {You gotta skip the wmf header}
30 HexEncoded := Copy(HexEncoded, (Sizeof(LongInt) + Sizeof(SmallInt) +
31 Sizeof(TSmallRect) +
32 Sizeof(Word) + Sizeof(LongInt) + Sizeof(Word)) * 2 + 1,
33 Length(HexEncoded));
34 HexEncoded := LowerCase(HexEncoded);
35 wmfTag := '{\pict\wmetafile8\picw%d\pich%d %s }';
36 wmfTag := Format(wmfTag, [iWidth * 20, iHeight * 20, HexEncoded]);
37 fStream.write(wmfTag[1], Length(wmfTag));
38 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}
|