
Delphi provides a printer object that can be used to print the contents of a Text Control.
The following example sends the contents of a Text Control, which can be several pages long, to the default printer:
procedure TForm1.Print1Click(Sender: TObject);
var wPages, No : Integer;
begin
wPages := TXTextControl1.CurrentPages;
Printer.BeginDoc;
for No := 1 To wPages do begin
TXTextControl1.PrintDevice := Printer.Canvas.Handle;
TXTextControl1.PrintPage(No);
if No <> wPages then
Printer.NewPage;
end;
Printer.EndDoc;
end;
After storing the number of pages in a local variable called wPages, the printer object is initialized with the Printer.BeginDoc statement. The For .. do loop runs from 1 to wPages to print all of the pages. Inside the loop there are three lines of code which print a single page:
Everything else, like calculating the line and page breaks, is done internally by Text Control. The formatting is based on the values of two groups of properties:
These properties are normally set in a page setup dialog box.