Delphi User's Guide

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:

Example Code

procedure TForm1.Print1Click(Sender: TObject);
var wPages, No : Integer;
   wPages := TXTextControl1.CurrentPages;
   for No := 1 To wPages do begin
      TXTextControl1.PrintDevice := Printer.Canvas.Handle;
      if No <> wPages then

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.