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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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?
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
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ivanov_GCommented:
hidrau, with Courier both strings "   38" and "00038", because it is fixed width. Just add spaces to the strings.
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
hidrauAuthor Commented:
Thanks guys for you help.

I am gonna open another question with printing image with canvas

Alex
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.