Cannot Print Control Codes using TPrinter

[email protected] wrote:

> Mike Needham wrote:

> > I want to be able to send out control codes to dot matrix printers to set
> > them in bold, wide character etc. Delphi seems to filter all control codes
> > (eg. Chr(27)  + 'E') out of the text and just prints a dot in the sheet of
> > paper.

> > Would much appreciate any suggestions on this one.

> > Mike Needham
> > [email protected]

> Hello, I've been using this kind of procedure and it seems to work O.K. Here's
> what I did:

> f : textfile;
> AssignFile( f, 'LPT1');
> Write( f, #27 + 'E' );
> ...

> Please explain your problem in a more detailed way. I think I didn't got it all...

> Anyway, I've been experiencing a lot of trouble trying to use line printers with
> 132 columns of text and 'Generic Text' and 'Epson 1170' (among others) Win95
> drivers. I only can get the first line printed in paper and the printer stops. Any
> clue?

Under Win16, you can use the SpoolFile function, or the Passthrough
escape if the printer supports it. This is detailed in TI3196 - Direct
Commands to Printer - Passthrough/Escape available from the Borland web
site.  Under Win32, you should use WritePrinter. The following is
an example of opening a printer and writing a raw string of data to the
printer. Note that you must send the correct printer name, such as "HP
LaserJet 5MP" for the function to succeed. You are also responsible for
embedding any necessary control codes that the printer may require.

uses WinSpool;

procedure WriteRawStringToPrinter(PrinterName: String; S: String);
   Handle: THandle;
   N: DWORD;
   DocInfo1: TDocInfo1;
   if not OpenPrinter(PChar(PrinterName), Handle, nil) then
      ShowMessage('error ' + IntToStr(GetLastError));
   with DocInfo1 do begin
     pDocName := PChar('test doc');
     pOutputFile := nil;
     pDataType := 'RAW';

   StartDocPrinter(Handle, 1, @DocInfo1);
   WritePrinter(Handle, PChar(S), Length(S), N);

procedure TForm1.Button1Click(Sender: TObject);
  WriteRawStringToPrinter('HP', 'Test This');

Joe C. Hecht
[email protected]