2009. január 7., szerda

How to send a raw string to the printer


Problem/Question/Abstract:

How to send a raw string to the printer

Answer:

procedure PrintRawStr(const S: ANSIString);

Uses WinSpool, Printers;

var
  sDefaultPrinter: string;
  Handle: THandle;
  dwN: DWORD;
  diDocInfo1: TDocInfo1; // Uses WinSpool
  bP: BYTE;

begin
  // Get the default printer or the printer choosen in the Printer Setup Dialog
  // if you have one in the application
  if Printer.Printers.Count > 0 then
  begin
    sDefaultPrinter := Printer.Printers[Printer.PrinterIndex]; // Uses Printers
    //uses Printers, get default printer
    bP := Pos(' on ', sDefaultPrinter);
    if bP > 0 then
      sDefaultPrinter := Copy(sDefaultPrinter, 1, bP - 1);
  end
  else
    Exit; // No printers installed on this system...

  if not OpenPrinter(PChar(sDefaultPrinter), Handle, nil) then
  begin
    case GetLastError of
      87: ShowMessage('Printer name does not exists.');
    else
      ShowMessage('Error ' + IntToStr(GetLastError)); // Uses Dialogs
    end;
    Exit; // Cannot find the printer
  end;

  with diDocInfo1 do
  begin
    pDocName := PChar('Print job raw'); // Will be seen in printer spooler
    pOutputFile := nil;
    pDataType := 'RAW';
  end;

  StartDocPrinter(Handle, 1, @diDocInfo1);
  StartPagePrinter(Handle);
  WritePrinter(Handle, PChar(S), Length(S), dwN);
  EndPagePrinter(Handle);
  EndDocPrinter(Handle);
  ClosePrinter(Handle);
end;

Nincsenek megjegyzések:

Megjegyzés küldése