Returns a reference to the global Printer object Printers unit
 function printer : TPrinter;
The printer function returns a reference to the global Printer (TPrinter) object.
This object has many methods and properties that you can use to manage printing. In particular:
BeginDoc  : Start printing
EndDoc  : Finish printing
NewPage  : Start a new page
PageHeight  : The page canvas height in pixels
PageWidth  : The page canvas width  in pixels
Canvas.Font  : Used to set font size/colour etc
Canvas.Draw  : Draw a graphic on the page
Canvas.TextOut  : Write text to the page

You should use TPrintDialog to show the user a printer selection and configuration dialog before printing, as in the example.
Related commands
TObject The base class type that is ancestor to all other classes
 Author links

 Download this web site as a Windows program.

Example code : Printing the page width and height text strings onto a page
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
  Printers,   // Unit containing the printer command
  SysUtils, Graphics,
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  printDialog : TPrintDialog;
  myPrinter   : TPrinter;

  // Create a printer selection dialog
  printDialog := TPrintDialog.Create(Form1);

  // If the user has selected a printer (or default), then print!
  if printDialog.Execute then
    // Use the Printer function to get access to the
    // global TPrinter object.
    // All references below are to the TPrinter object
    myPrinter := printer;
    with myPrinter do
      // Start printing

      // Set up a large blue font
      Canvas.Font.Size   := 20;
      Canvas.Font.Color  := clBlue;

      // Write out the page size
      Canvas.TextOut(20,  20, 'Page  width = '+IntToStr(PageWidth));
      Canvas.TextOut(20, 150, 'Page height = '+IntToStr(PageHeight));

      // Finish printing
Hide full unit code
   A page will be printed with big blue text at the top left:
   Page  width = 2400
   Page height = 3357
Delphi Programming Neil Moffatt 2002 - 2020. All rights reserved.  |  Home Page