Printing a
String List with Page-break Management,
Titles
and Other Details
This routine can be used to print the contents of a
TMemo component, or any other TStrings list. The routine manages margins and page breaks, and adds page headers with the page number and an optional title. Printing can use any installed font.
With the constants shown, each line is indented 0.7" (18 mm), and top and bottom margins are 0.5" (13 mm). No line-wrapping takes place, and 20% leading is added between lines. Portrait orientation is assumed, with a sheet height of 11" (279 mm). |
uses
Printers;
function InchToPrnPixelsX (I : single) : integer;
// convert I inches into printer horizontal pixels
begin
Result:= round(I * GetDeviceCaps(Printer.Handle, LOGPIXELSX));
end;
function InchToPrnPixelsY (I : single) : integer;
// convert I inches into printer vertical pixels
begin
Result:= round(I * GetDeviceCaps(Printer.Handle, LOGPIXELSY));
end;
function PrintStringList (SL : TStrings; AFont : TFont; Title : string)
: boolean;
// basic print string list SL to default printer, using font Font,
// except in black
const
LeftM = 0.7; // inches
VertM = 0.5;
var
LeftX,
LineH,
Y,
I, P : integer;
procedure NewPage;
begin
if P > 0 then
Printer.NewPage;
inc(P);
// blank space at top of page
Y:= InchToPrnPixelsY(VertM);
// print page header
Printer.Canvas.TextOut(LeftX, Y, Format('Page %d: %s', [P, Title]));
inc(Y, 2 * LineH);
end;
begin { PPrintStringList }
Result:= false;
if SL.Count > 0 then begin
Printer.Orientation:= poPortrait;
Printer.BeginDoc;
try
with Printer.Canvas do begin
Font:= AFont;
Font.Color:= clBlack;
Brush.Color:= clWhite;
LineH:= round(1.2 * (-Font.Height));
P:= 0;
LeftX:=
InchToPrnPixelsX(LeftM);
NewPage;
for I:= 0 to SL.Count - 1 do begin
if Y + LineH > PInchToPrnPixelsY(11 - VertM) then
NewPage;
TextOut(LeftX, Y, SL.Strings[I]);
inc(Y, LineH);
end;
end;
Result:= true;
finally
Printer.EndDoc;
end;
end;
end; |
|
Example of use: |
|
with Form1.Memo1 do
PrintStringList(Lines, Font, 'My Title'); |
|