?
Solved

Working with printer

Posted on 2006-04-04
7
Medium Priority
?
217 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:hidrau
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 12

Assisted Solution

by:Ivanov_G
Ivanov_G earned 200 total points
ID: 16375745
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
 
LVL 1

Author Comment

by:hidrau
ID: 16375807
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
 
LVL 11

Expert Comment

by:pcsentinel
ID: 16376168
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Accepted Solution

by:
pcsentinel earned 1000 total points
ID: 16376311
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
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 16378737
hidrau, with Courier both strings "   38" and "00038", because it is fixed width. Just add spaces to the strings.
0
 
LVL 14

Assisted Solution

by:Pierre Cornelius
Pierre Cornelius earned 800 total points
ID: 16379370
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
 
LVL 1

Author Comment

by:hidrau
ID: 16380270
Thanks guys for you help.

I am gonna open another question with printing image with canvas

Alex
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question