Get EMF Text

Get EMF Text

Post by Johannes Palme » Mon, 30 Jun 2003 18:21:55



Hi,

I have a EMF file and want to get all text out of the file and convert it to
a String so that I can use it in a TMemo.

I do already know how to get all records(PEnhMetaRecord) which have the type
EMR_EXTTEXTOUTW or EMR_EXTTEXTOUTA (btw what's the difference between those
two types?).
Know I want to get the text from those records. I've found only two source
codes which deal with this issue, but they don't work properly. They always
return stuff like "??????" or"|?|", but not the String I need.

Unfortunately there's not much information to find about that topic and I'm
not really into all that character set and pointer stuff, so can anyone help
me.

Thanks

Johannes

 
 
 

Get EMF Text

Post by Boris Novgorodo » Mon, 30 Jun 2003 21:03:42


Quote:> I have a EMF file and want to get all text out of the file and convert it
to
> a String so that I can use it in a TMemo.

procedure TForm1.Button1Click(Sender: TObject);
var emf:TMetaFile;
    emc:TMetaFileCanvas;
begin
emf:=TMetaFile.Create;
emc:=TMetaFileCanvas.Create(emf,0);
emc.TextOut(0,0,'Aaaabbbb');
emc.Ellipse(10,10,200,100);
emc.TextOut(100,90,'123 456 789');
emc.Free;
emf.SaveToFile('e:\emf.emf');
Canvas.Draw(0,0,emf);
emf.Free;
end;

function EnumProc(DC:HDC;lpHTable:PHANDLETABLE;
    EMFR:PENHMETARECORD; nObj,lpData:Integer):Integer;stdcall;
var
    ws:WideString;
    rec:PEMRExtTextOut;
    r:Trect;
    p:Tpoint;
begin
if (EMFR.iType=EMR_EXTTEXTOUTW) then begin
  rec:=PEMRExtTextOut(EMFR);
  setlength(ws,rec.emrtext.nChars);
  p:=rec.emrtext.ptlReference;
  r:=rec.rclBounds;
  Form1.Memo1.Lines.Add(Format('BasePoint:%d:%d',[p.x,p.y]));
  Form1.Memo1.Lines.Add(Format('BoundsRect:%d:%d - %d:%d',
    [r.left,r.top,r.right,r.bottom]));

    ws[1],Length(ws)*2);
  Form1.Memo1.Lines.Add('String: '+ws);
  if ws[1]='A' then
    SetTextColor(form1.canvas.Handle,clLime);
end;
PlayEnhMetafileRecord(Form1.Canvas.Handle,lpHTable^,emfr^,nobj);
Result:=1;
end;

procedure TForm1.Button2Click(Sender: TObject);
var emf:TMetaFile;
    r:Trect;
begin
refresh;
emf:=TMetaFile.Create;
emf.LoadFromFile('e:\emf.emf');
Canvas.Draw(0,0,emf);
r:=Rect(0,0,1,1);

emf.free;
end;

procedure TForm1.Button3Click(Sender: TObject);
var emf:TMetaFile;
    XF:TXForm;
begin
refresh;
XF.eM11:=0.707;
XF.eM12:=0.707;
XF.eM21:=-0.707;
XF.eM22:=0.707;
XF.eDx:=100;
XF.eDy:=100;
emf:=TMetaFile.Create;
emf.LoadFromFile('e:\emf.emf');
SetGraphicsMode(Canvas.Handle,GM_ADVANCED);
SetWorldTransForm(Canvas.Handle,XF);
Canvas.Draw(0,0,emf);
emf.free;
end;

--
Regards
Boris Novgorodov

 
 
 

Get EMF Text

Post by Ignacio Vazque » Mon, 30 Jun 2003 23:15:36



Quote:> EMR_EXTTEXTOUTW or EMR_EXTTEXTOUTA (btw what's the difference between
those
> two types?).

A refers to a single-byte string (i.e., AnsiString), and W refers to a
double-byte string (i.e., WideString). This convention is used all through
the Windows API.

Cheers,
  Ignacio

 
 
 

Get EMF Text

Post by Johannes Palme » Mon, 30 Jun 2003 23:46:02


Thanks Boris and Ignacio, you've been a great help.

Johannes

 
 
 

Get EMF Text

Post by andr » Tue, 01 Jul 2003 02:46:54


this is good for text, but how read the images?

TIA


> > I have a EMF file and want to get all text out of the file and convert
it
> to
> > a String so that I can use it in a TMemo.

> procedure TForm1.Button1Click(Sender: TObject);
> var emf:TMetaFile;
>     emc:TMetaFileCanvas;
> begin
> emf:=TMetaFile.Create;
> emc:=TMetaFileCanvas.Create(emf,0);
> emc.TextOut(0,0,'Aaaabbbb');
> emc.Ellipse(10,10,200,100);
> emc.TextOut(100,90,'123 456 789');
> emc.Free;
> emf.SaveToFile('e:\emf.emf');
> Canvas.Draw(0,0,emf);
> emf.Free;
> end;

> function EnumProc(DC:HDC;lpHTable:PHANDLETABLE;
>     EMFR:PENHMETARECORD; nObj,lpData:Integer):Integer;stdcall;
> var
>     ws:WideString;
>     rec:PEMRExtTextOut;
>     r:Trect;
>     p:Tpoint;
> begin
> if (EMFR.iType=EMR_EXTTEXTOUTW) then begin
>   rec:=PEMRExtTextOut(EMFR);
>   setlength(ws,rec.emrtext.nChars);
>   p:=rec.emrtext.ptlReference;
>   r:=rec.rclBounds;
>   Form1.Memo1.Lines.Add(Format('BasePoint:%d:%d',[p.x,p.y]));
>   Form1.Memo1.Lines.Add(Format('BoundsRect:%d:%d - %d:%d',
>     [r.left,r.top,r.right,r.bottom]));

>     ws[1],Length(ws)*2);
>   Form1.Memo1.Lines.Add('String: '+ws);
>   if ws[1]='A' then
>     SetTextColor(form1.canvas.Handle,clLime);
> end;
> PlayEnhMetafileRecord(Form1.Canvas.Handle,lpHTable^,emfr^,nobj);
> Result:=1;
> end;

> procedure TForm1.Button2Click(Sender: TObject);
> var emf:TMetaFile;
>     r:Trect;
> begin
> refresh;
> emf:=TMetaFile.Create;
> emf.LoadFromFile('e:\emf.emf');
> Canvas.Draw(0,0,emf);
> r:=Rect(0,0,1,1);

> emf.free;
> end;

> procedure TForm1.Button3Click(Sender: TObject);
> var emf:TMetaFile;
>     XF:TXForm;
> begin
> refresh;
> XF.eM11:=0.707;
> XF.eM12:=0.707;
> XF.eM21:=-0.707;
> XF.eM22:=0.707;
> XF.eDx:=100;
> XF.eDy:=100;
> emf:=TMetaFile.Create;
> emf.LoadFromFile('e:\emf.emf');
> SetGraphicsMode(Canvas.Handle,GM_ADVANCED);
> SetWorldTransForm(Canvas.Handle,XF);
> Canvas.Draw(0,0,emf);
> emf.free;
> end;

> --
> Regards
> Boris Novgorodov

 
 
 

Get EMF Text

Post by Mike Shkolni » Wed, 02 Jul 2003 19:46:36


Instead next line:

ws[1], Length(ws)*2);
better to use next:

t := PEMRExtTextOut(EMFR)^;
s := WideCharLenToString(PWideChar(PChar(EMFR) + t.EMRText.offString),
t.EMRText.nChars);

--
With best regards, Mike Shkolnik

http://www.scalabium.com



> > I have a EMF file and want to get all text out of the file and convert
it
> to
> > a String so that I can use it in a TMemo.

> procedure TForm1.Button1Click(Sender: TObject);
> var emf:TMetaFile;
>     emc:TMetaFileCanvas;
> begin
> emf:=TMetaFile.Create;
> emc:=TMetaFileCanvas.Create(emf,0);
> emc.TextOut(0,0,'Aaaabbbb');
> emc.Ellipse(10,10,200,100);
> emc.TextOut(100,90,'123 456 789');
> emc.Free;
> emf.SaveToFile('e:\emf.emf');
> Canvas.Draw(0,0,emf);
> emf.Free;
> end;

> function EnumProc(DC:HDC;lpHTable:PHANDLETABLE;
>     EMFR:PENHMETARECORD; nObj,lpData:Integer):Integer;stdcall;
> var
>     ws:WideString;
>     rec:PEMRExtTextOut;
>     r:Trect;
>     p:Tpoint;
> begin
> if (EMFR.iType=EMR_EXTTEXTOUTW) then begin
>   rec:=PEMRExtTextOut(EMFR);
>   setlength(ws,rec.emrtext.nChars);
>   p:=rec.emrtext.ptlReference;
>   r:=rec.rclBounds;
>   Form1.Memo1.Lines.Add(Format('BasePoint:%d:%d',[p.x,p.y]));
>   Form1.Memo1.Lines.Add(Format('BoundsRect:%d:%d - %d:%d',
>     [r.left,r.top,r.right,r.bottom]));

>     ws[1],Length(ws)*2);
>   Form1.Memo1.Lines.Add('String: '+ws);
>   if ws[1]='A' then
>     SetTextColor(form1.canvas.Handle,clLime);
> end;
> PlayEnhMetafileRecord(Form1.Canvas.Handle,lpHTable^,emfr^,nobj);
> Result:=1;
> end;

> procedure TForm1.Button2Click(Sender: TObject);
> var emf:TMetaFile;
>     r:Trect;
> begin
> refresh;
> emf:=TMetaFile.Create;
> emf.LoadFromFile('e:\emf.emf');
> Canvas.Draw(0,0,emf);
> r:=Rect(0,0,1,1);

> emf.free;
> end;

> procedure TForm1.Button3Click(Sender: TObject);
> var emf:TMetaFile;
>     XF:TXForm;
> begin
> refresh;
> XF.eM11:=0.707;
> XF.eM12:=0.707;
> XF.eM21:=-0.707;
> XF.eM22:=0.707;
> XF.eDx:=100;
> XF.eDy:=100;
> emf:=TMetaFile.Create;
> emf.LoadFromFile('e:\emf.emf');
> SetGraphicsMode(Canvas.Handle,GM_ADVANCED);
> SetWorldTransForm(Canvas.Handle,XF);
> Canvas.Draw(0,0,emf);
> emf.free;
> end;

> --
> Regards
> Boris Novgorodov

 
 
 

Get EMF Text

Post by Johannes Palme » Fri, 04 Jul 2003 02:10:32


Thanks Mike,

can I use your code for EMR_EXTTEXTOUTW as well as for EMR_EXTTEXTOUTA ?

Johannes



> Instead next line:

> ws[1], Length(ws)*2);
> better to use next:

> t := PEMRExtTextOut(EMFR)^;
> s := WideCharLenToString(PWideChar(PChar(EMFR) + t.EMRText.offString),
> t.EMRText.nChars);

> --
> With best regards, Mike Shkolnik

> http://www.scalabium.com