How to Print TStrings

The TMemo and TListBox components don’t have any print functions although sometimes they would come in very handy. Well, here’s the solution. All you have to do is open a text file on the printer and print anything you want.

  uses
    Printers;
  ...
  var
    iI  : Integer;
    fOut: TextFile;
  begin
    try
      AssignPrn(fOut);
      Rewrite(fOut);
      { Print the document title... }
      with Printer.Canvas.Font do begin
        Name := 'Courier New';
        Size := 10;
        Style := [fsBold];
      end;
      WriteLn(fOut, Form1.Caption); { Document title }
      WriteLn;

      { ...and the TStrings }
      Printer.Canvas.Font.Style := []; { The printer font style }
      for iI := 0 to Memo1.Lines.Count - 1 do
        WriteLn(fOut, Memo1.Lines[iI]);
   finally
     (* You have to close the printer file as fast as you can because only one
      * printer file can be open in the system at one time.
      *)
     CloseFile(fOut);
    end;
  end;

Tags: , , , , , , , ,

No Comments Yet

You can be the first to comment!

Leave a comment

OpenID Login

Standard Login