One of the easiest ways to output text to the printer is to use the AssignPrn procedure, which enables you to assign a text file to the printer and use the WriteLn procedure to print text on the printer. When you assign a text file to the printer, you must call the Rewrite procedure to begin printing. When you're done printing, close the printer by calling CloseFile. Finally, to use the AssignPrn procedure and other printing related types, classes, and routines, you have to add the Printers unit to the uses list.
Here's an example of how you can use the AssignPrn procedure to output text to the printer:
procedure TMainForm.AssignPrintingClick(Sender: TObject); var F: TextFile; begin AssignPrn(F); { you need the Printers unit for this } try Rewrite(F); { must Rewrite when using AssignPrn } WriteLn(F, 'Hello from Delphi.'); finally CloseFile(F); end; end;
Figure 27-1 shows the output of the above code.
Figure 27-1: The result of AssignPrn printing