SmartPascal
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
// 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 AssignPrn command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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