Print directly

How to print directly to the printer.

Contributor: JAVIER CELUCE

A two months ago Gareet Wilson answer this question and I tested that work


The best way I have found is to do the following, which uses the Windows

printing routines, but sends data directly to the printer (you may have

to changes things around, but this is a good outline}. I figured out a

lot of this from "Three Printing Techniques for Windows 95 Console

Applications," by David Tamashiro in the C/C++ User's Journal, January


**First, include the following:


  PrinterHandle:THandle; {the handle to the printer}

uses WinSpool, Printers;

type TDocInfo1=packed record {the replacement for DOC_INFO_1}

  lpszDocName: PAnsiChar;

  lpszOutputFile: PAnsiChar;

  lpszDatatype: PAnsiChar;


**Then, open a printer according to its name:


  CTitle:array[0..31] of Char;

  CMode:array[0..4] of Char;



  StrPLopy(CTitle, 'My Title'); {setup our title buffer}

  StrPCopy(CMode, 'RAW'); {put "RAW" in our mode buffer}

  FillChar(DocInfo, SizeOf(DocInfo), 0); {fill the DocInfo structure

with zero's}

  with DocInfo do


    lpszDocName:=CTitle; {set the title of our document}

    lpszOutputFile:=nil; {specify no output file}

    lpszDatatype:=CMode; {set the mode, which we have specified as



  OpenPrinter('Printer Name Here', PrinterHandle, nil);

  StartDocPrinter(PrinterHandle, 1, @DocInfo);


**Now, print your text:


  Count:DWord; {the number of bytes written}


  WritePrinter(PrinterHandle, PChar(printText), Length(printText),


**When you are finished printing, tidy things up:

  EndPagePrinter(PrinterHandle); {end the page}

  EndDocPrinter(PrinterHandle); {end the document}

  if PrinterHandle<>0 then {if we have a printer handle}


    ClosePrinter(PrinterHandle); {close the printer}

    PrinterHandle:=0; {show that we have closed the printer}


I think that about covers it. Hope this helps.


Share this article!

Follow us!

Find more helpful articles: