ObjectPascal
AssignPrn
Procedure
Treats the printer as a text file - an easy way of printing text Printers unit
 procedure AssignPrn ( var FileHandle : TextFile ) ;
Description
The AssignPrn procedure assigns the printer to a FileHandle. This means that subsequent text writing to this file gets rerouted to the printer. This provides a simpl and easy way of dumping out text to a printer.
Notes
Warning : The AssignPrn mechanism is useful for simple programs, but lacks any control over printing for a real application.
Related commands
AssignFile Assigns a file handle to a binary or text file
CloseFile Closes an open file
ReWrite Open a text or binary file for write access
Write Write data to a binary or text file
WriteLn Write a complete line of data to a text file
 
Example code : Print a few words to the printer
var
  myFile : TextFile;
  printDialog : TPrintDialog;

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

  // If the user has selected a printer (or default), then print!
  if printDialog.Execute then
  begin
    // Try to open a printer file
    AssignPrn(myFile);

    // Now prepare to write to the printer
    ReWrite(myFile);

    // Write a couple of well known words to this file -
    // they will be printed instead
    WriteLn(myFile, 'Hello');
    WriteLn(myFile, 'World');

    // Close the file
    CloseFile(myFile);
  end;
end;
Show full unit code
   After the user selects a printer, the following text is printed in a small font at the top left of the page:
  
   Hello
   World