Over the past week I've also had to come to terms with the facilities in Printer4Lazarus and, I think, very successfully.
Because I always print to a .PDF file and my 'default' printer is set to that I haven't bothered with printer selection (as yet).
I've created a procedure called 'PrintLine' which takes a number of arguments, viz :
const
FontList : array[1..10] of string[20] = ('ZapfCalligr BT',
'Optima',
'Windsor BT',
'Windsor Lt BT',
'CroissantD',
'CopprplGoth BT',
'Arial',
'Dauphin',
'Futura Md BT',
'Symbol');
DPM : single = 23.62204724;
[...]
procedure PrintLine(X,Y :single; S : String; FN : byte; Col : word; PT : byte );
Var
Ht : byte;
begin
// X = H Pos in mm, Y - V Pos in mm S - Text FN = FontList Index Col = Colour P Point Size
with printer do
begin
Canvas.Font.Name := FontList[FN];
Canvas.Font.Size := PT;
Canvas.Font.Color := Col;
Ht := Abs(Canvas.TextHeight('I'));
Canvas.TextOut(Trunc(X*DPM),Trunc(Y*DPM - Ht),S);
if Y > prBottom then
prPageEnd := true;
end;
end;
As you can see, I've also declared an Array of FontList which allows 'on the fly' selection of typeface, and a constant - DPM - (Dots Per MM) since I find it more convenient to specify the position on page in mm.
In my case, using CutePDF set at 600dpi, the DPM setting is 23.62204724.
Again, because I want to specify the position at the base line of the text, I use [Ht] to calculate the height of the text string being output and take that away from the specified position because 'TextOut' expects the location to be the top of the string.
I haven't yet implemented a 'FontStyle' but that is on the 'things to do' list.
To print Currency Values I've also created a PrintVal procedure to print with right alignment, viz:
procedure PrintVal(V : single; X,Y : word; PT : byte);
Var
Col : word;
S : string[10];
W : single;
begin
Str(Abs(V):8:2,S);
StripLeadingSpace(S);
if V < 0 then
begin
Col := Red;
S :='£('+S;
end
else
begin
Col := clBlack;
S := '£'+S;
end;
Printer.Canvas.Font.Size :=PT;
W := Abs(Printer.Canvas.TextWidth(S)/DPM);
PrintLine(X-W,Y,S,1,Col,PT);
if V < 0 then
PrintLine(X,Y,')',1,Col,PT);
end;
This also handles the
£(20.00) format for negative values discussed in another thread.
These procedures are called from other procedures which do the basic calls which are necessary, the basics of one of which is :
procedure TForm1.PrintSuppListClick(Sender: TObject);
begin
PrintSuppList.Caption := 'PRINTING';
InitReport;
Report := 2;
with Printer do
try
BeginDoc;
PrintReportHeader;
// Print details .........
if prPageEnd then
StartNewPage;
finally
EndDoc;
end;
sleep(1500);
PrintSuppList.Caption := 'Print Supplier List';
end;
Which you can see also tells the user that 'PRINTING' is being done - the Sleep(1500) simply keeps that notice visible for the time it takes for the printer dialogue to become active
otherwise the user might think that the command hadn't been actioned.