Board index » delphi » Printing on an old fashioned proprinter in generic text

Printing on an old fashioned proprinter in generic text

I have a stupid problem... I have to print on a generic text only
proprinter from a Delphi
program. I virtually don't know where the printer is located so I would
like to avoid to
directly open the printer port and write to using writeln().
Any suggestion?

Thanks in advance, Matteo
mgel...@etnoteam.it

 

Re:Printing on an old fashioned proprinter in generic text


Quote
Matteo Gelosa wrote:

> I have a stupid problem... I have to print on a generic text only
> proprinter from a Delphi
> program. I virtually don't know where the printer is located so I would
> like to avoid to
> directly open the printer port and write to using writeln().
> Any suggestion?

In 16 bit Delphi, use SpoolFile(), in 32 Bit Delphi, use:

procedure PrintFile(const sFileName: string);
const
  BufSize = 16384;
type
  TDoc_Info_1 = record
    pDocName: pChar;
    pOutputFile: pChar;
    pDataType: pChar;
  end;
var
  Count, BytesWritten: integer;
  hPrinter: THandle;
  Device : array[0..255] of char;
  Driver : array[0..255] of char;
  Port   : array[0..255] of char;
  hDeviceMode: THandle;
  DocInfo: TDoc_Info_1;
  f: file;
  Buffer: Pointer;
begin
  Printer.PrinterIndex := -1;
  Printer.GetPrinter(Device, Driver, Port, hDeviceMode);
  if not WinSpool.OpenPrinter(@Device, hPrinter, nil) then exit;
  DocInfo.pDocName := 'MyDocument';
  DocInfo.pOutputFile := nil;
  DocInfo.pDatatype := 'RAW';
  if StartDocPrinter(hPrinter, 1, @DocInfo) = 0 then
  begin
    WinSpool.ClosePrinter(hPrinter);
    exit;
  end;
  if not StartPagePrinter(hPrinter) then
  begin
    EndDocPrinter(hPrinter);
    WinSpool.ClosePrinter(hPrinter);
    exit;
  end;
  Assign(f, sFileName);
  Reset(f, 1);
  GetMem(Buffer, BufSize);
  while not eof(f) do
  begin
    Blockread(f, Buffer^, BufSize, Count);
    if Count > 0 then
    begin
      if not WritePrinter(hPrinter, Buffer, Count, BytesWritten) then
      begin
        EndPagePrinter(hPrinter);
        EndDocPrinter(hPrinter);
        WinSpool.ClosePrinter(hPrinter);
        FreeMem(Buffer, BufSize);
        exit;
      end;
    end;
  end;
  FreeMem(Buffer, BufSize);
  EndDocPrinter(hPrinter);
  WinSpool.ClosePrinter(hPrinter);
end;

Joe
--
Joe C. Hecht
(Borland Delphi Developer Support)
Join the Delphi Online Discussion Forum at
http://www.borland.com/techsupport/delphi/

Other Threads