Working with printer

hello Guys, I am trying to work with printer but I am having some problens with it. As You can see I have a line with QTd
Codigo and locação. I would like to print them aligned like this example:


     Qtd                   Cod             Locação
   5631              748547       415a5177B1
      56                   784               748596
    123                     74                 744aB

 With Printer do
  Begin
    Orientation := poLandscape;
    BeginDoc;
    Canvas.Pen.Width := 2;
    Canvas.Font.Name := 'Times New Roman';

    Canvas.Font.Size := 12;
    Tam := Canvas.TextWidth('a');
    Col := 10;
    Canvas.TextOut(10,250 ,'  Qtd                             Código             Locação');
    Canvas.TextOut(Col,290,  Qtd);
    Col := Col + (Tam * 16);
    Canvas.TextOut(Col,290,  Cod);
    Col := Col + (Tam * 20);
    Canvas.TextOut(Col,290,  Aloc);
   EndDoc
LVL 1
hidrauAsked:
Who is Participating?
 
pcsentinelCommented:
You could also use DrawText

something like
var
  lRowHeight: integer;
  lFlags: LongInt;
  li: integer;
  lRect1: TRect;
  lRect2: TRect;
  lRect3: TRect;
begin
  with Printer.Canvas do
  begin
     Font.Name := 'Times New Roman';
     Font.Size := 12;
     lRowHeight:=TextHeight('A');
     inc(lRowHeight,5); //add a gap between the rows
     lFlags:=DT_RIGHT      +DT_SINGLELINE;
     lRect1:=Rect(0,50,100,50+lRowHeight;     --first line first column
     lRect2:=Rect(101,50,200,50+lRowHeight;    --first line second column
     lRect3:=Rect(201,50,300,50+lRowHeight;      --first line third column
     for li:=0 to NumRecs do
     begin
       DrawText(Handle,pChar(Records[li].Qtd),lRect1,lFlags);    
       DrawText(Handle,pChar(Records[li].Cod),lRect1,lFlags);    
       DrawText(Handle,pChar(Records[li].Aloc),lRect1,lFlags);
       lRect1.Top:=-lRect1.Top+lRowHeight;    
       lRect1.Bottom:=-lRect1.Top+lRowHeight;    
       lRect2.Top:=-lRect2.Top+lRowHeight;    
       lRect2.Bottom:=-lRect2.Top+lRowHeight;    
       lRect3.Top:=-lRect3.Top+lRowHeight;    
       lRect3.Bottom:=-lRect3.Top+lRowHeight;    
    end;
  end;
end;


you could add the column headers in using
     lFlags:=DT_DT_CENTER      +DT_SINGLELINE;

to get them positioned in the middle of the columns


regards
0
 
Ivanov_GCommented:
1) You can set the font to Courier (all letters and space have the same width) and align with spaces.

2) Use some reporting tools, like Rave Reports.
0
 
hidrauAuthor Commented:
I can use rave reports or quickreport, unfortunately I have to develop this in delphi 5

And quickreport doesn't work correctly with a printer drive for a kind of printer I am working.

I am gonna try with courier, but do you know how can I align?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
pcsentinelCommented:
You need to subtract the Textwidth of your output

i.e.

say
Column 1 starts at 0 and goes to 100
Column 2 starts at 101 and goes to 200
Column 3 starts at 201 and goes to 300

Use TextRect

with Canvas do
begin
  lRect:=Rect(0,20,100,50) -- assuming a 30 pixel row height
  TextRect(lRect,lRect.Right-TextWidth(Qtd),Qtd);
  lRect:=Rect(101,20,200,50) -- assuming a 30 pixel row height
  TextRect(lRect,lRect.Right-TextWidth(Qtd),Cod);
  lRect:=Rect(201,20,300,50) -- assuming a 30 pixel row height
  TextRect(lRect,lRect.Right-TextWidth(Qtd),Aloc);
end;

you obviously need to increment the top and bottom of the rects for the rows


regards
0
 
Ivanov_GCommented:
hidrau, with Courier both strings "   38" and "00038", because it is fixed width. Just add spaces to the strings.
0
 
Pierre CorneliusCommented:
Use the SetTextAlign API function to set the alignment of the DC. Here's a demo:

procedure PrnAlignDemo(dc: HDC);
var data: array[1..3, 1..4] of string;
    col, row: integer;
    TxtExt: TSize;
begin
  data[1, 1]:= 'QTD';
  data[2, 1]:= 'COD';
  data[3, 1]:= 'Locação';
  data[1, 2]:= '5631';
  data[2, 2]:= '748547';
  data[3, 2]:= '415a5177B1';
  data[1, 3]:= '56';
  data[2, 3]:= '784';
  data[3, 3]:= '748596';
  data[1, 4]:= '123';
  data[2, 4]:= '74';
  data[3, 4]:= '744aB';

  GetTextExtentPoint32(dc, 'gMgMgMgMgMgMgM', 14, TxtExt);
  SetTextAlign(dc, TA_RIGHT);

  for row:= 1 to 4 do
    for col:= 1 to 3 do
    begin
      TextOut(dc,
              col * round(TxtExt.cx *1.1),
              row * round(TxtExt.cy *1.1),
              pchar(data[col, row]),
              length(data[col, row]));
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Printer.BeginDoc;
  PrnAlignDemo(Printer.Canvas.Handle);
  Printer.EndDoc;
end;


Regards
Pierre
0
 
hidrauAuthor Commented:
Thanks guys for you help.

I am gonna open another question with printing image with canvas

Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.