Send TextFile to Printer

TECH TIP: Send TextFile to Printer

Question: “I designed a application that exports the report to a .txt file using ReportTextFile device. How can I Send the text file to the printer?”

The following procedure will send the .txt file to the printer using Delphi’s TPrinter object.

uses
  Printers;

procedure SendTextFileToPrinter(aFileName: String);
var
  lsLines: TStringList;
  lOutputFile: TextFile;
  liIndex: Integer;
begin

  lsLines := TStringList.Create;

  try
    lsLines.LoadFromFile(aFileName);

    AssignPrn(lOutputFile);
    Rewrite(lOutputFile);

    for liIndex := 0 to lsLines.Count-1 do
      Writeln(lOutputFile, lsLines[liIndex]);

    CloseFile(lOutputFile);

  finally
    lsLines.Free;
  end;


end;