Professional Documents
Culture Documents
There are in fact two Printer objects, depending on whether you have a CLX or VCL application.
The differences are minor.
const
TOTAL_PAGES = 4; // How many pages to print
var
printDialog : TPrintDialog;
begin
// Create a printer selection dialog
printDialog := TPrintDialog.Create(Form1);
Starting to print
The Printer object is permanently available to your code (you must use the Printers unit to get
access to its methods and fields though). With this object, printing proceeds in an orderly fashion.
// Use the Printer function to get access to the global TPrinter object.
// Set to landscape orientation
Printer.Orientation := poLandscape;
// Start printing
Printer.BeginDoc;
This starts a print job, with a landscape page layout, and a title, and the user specified number of
copies. Note that we are ignoring collation - we always print all page 1 copies before page 2 etc.
var
page, startPage, endPage : Integer;
And we now set these variables from the print dialog:
// Finish printing
Printer.EndDoc;
The functions highlighted in red illustrate printing graphics and text operations. We have drawn a
line across the page after moving the line start point to the appropriate position. And we have written
out the page number and size values. Text is also positioned by graphical coordinate.
Both graphical and textual operations are performed on the Printer Canvas. This is very important. It
allows you to have a very similar or identical block of code that displays to a form canvas as it does
to a printer canvas. This lets you print what you display.
Notice that we handle the page numbering, and page throws (Printer.NewPage) ourselves.
Finally, when all pages have been printed, the last page throw is performed when we close the print
job using Printer.EndDoc.
Threading is somewhat beyond the remit of this article, but is included in the final, complete code
given below. You can copy and paste this code into Delphi, as long as you follow the instructions at
the top of the code:
// 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;
interface
uses
Printers, // Unit containing the Printer command
SysUtils, Graphics, Windows,
Forms, Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm} // Include form definitions
// Now that the user has pressed cancel, we abort the printing
if Printer.Printing then
begin
Printer.Abort;
ShowMessage('Printing aborted');
end;
// End this thread
endThread(0);
end;
begin
// Create a printer selection dialog
printDialog := TPrintDialog.Create(Form1);
// Use the Printer function to get access to the global TPrinter object.
// Set to landscape orientation
Printer.Orientation := poLandscape;
// Set the printjob title - as it it appears in the print job manager
Printer.Title := 'Test print for Delphi';
// Start printing
Printer.BeginDoc;
// Finish printing
Printer.EndDoc;
end;
end;
end.