Mega Code Archive

 
Categories / Delphi / Printing
 

A Class to print Raw text to printer

Title: A Class to print Raw text to printer Question: A complete way to print raw text to printer Answer: Is very common the needed to print raw text. Here is a complete class for do it. There is a requirement: to install the Generic/Only text printer of Windows. Here an example: var xPrn: TRawPrint; begin xPrn:=TRawPrint.Create; // create an instance xPrn.PrinterName:='Name of the Generic printer'; if xPrn.Open then begin // if I can open the printer xPrn.Condensed:=True; // print to 16 cpi xPrn.InitPrinter; xPrn.Print(2,10,'My Text'); // I set the row, column and text to print xPrn.LaserPrinter:=True; // I can do it with laser printers; xPrn.Write ('Another Text'); // I dont need to specify the row and column xPrn.NewPage; // Form Feed xPrn.Close: // I close the printer end; xPrn.Free; // release the instance end; The class: ///////////////////////////////////////////////////////////////// unit URaw; { Unit to print raw text Author: Alejandro Castro Date 16/Jul/2000 } interface uses SysUtils, Windows, WinSpool; type TRawPrint = class(TObject) private xIsOpen: Boolean; xHandle: THandle; xBytesWritten: DWord; xDocInfo: TDocInfo1; xIsMatrix: Boolean; // is a matrix printer ? function ReadLasPrt: Boolean; procedure WriteLasPrt(const Value : Boolean); function ReadMatPrt: Boolean; procedure WriteMatPrt(const Value : Boolean); public Row: Integer; // current row Column: Integer; // current column RowsPage: Integer; // no. of rows per page Document: String; // name of the document for winspool PrinterName: String; // name of the raw printer Condensed: Boolean; // print on condensed mode SeqCondensed: String; // sequence of chars for print to 16 cpi SeqNormal: String; // sequence of chars for print to 10 cpi constructor Create; function Open: Boolean; // open the printer function Close: Boolean; // close the printer Function InitPrinter: Boolean; Function Write(xText: String): Boolean; Procedure SetPos(xRow, xCol: Integer); Procedure Go(xRow, xCol: Integer); // force to move the head of the printer Procedure GoTop; // go to the begining of the next page or form Procedure NewPage; // form feed Procedure Print(xRow, xCol: Integer; xText: String); // print xText on the row, col property MatrixPrinter: Boolean read ReadMatPrt write WriteMatPrt; property LaserPrinter: Boolean read ReadLasPrt write WriteLasPrt; end; implementation constructor TRawPrint.Create; begin Row:=0; Column:=0; RowsPage:=66; xIsOpen:=False; Condensed:=False; Document:='Alfra'; PrinterName:=''; MatrixPrinter:=True; end; function TRawPrint.ReadMatPrt : Boolean; begin Result:=xIsMatrix; end; procedure TRawPrint.WriteMatPrt(const Value : Boolean); begin xIsMatrix:=Value; SeqNormal:=#18; SeqCondensed:=#15; end; procedure TRawPrint.WriteLasPrt(const Value : Boolean); begin xIsMatrix:=not Value; SeqNormal:=#27+'&l6D'+#27+'(s0p10H'; SeqCondensed:=#27+'&l6D'+#27+'(s0p16.66H'; end; function TRawPrint.ReadLasPrt : Boolean; begin Result:=not xIsMatrix; end; function TRawPrint.Open: Boolean; begin Result:=False; if not xIsOpen then begin if PrinterName'' then begin if Document='' then Document:='Alfra'; with xDocInfo do begin pDocName := PChar(Document); pOutputFile := nil; pDatatype := 'RAW'; end; Result:=OpenPrinter(PChar(PrinterName), xHandle, nil); if Result then begin Row:=0; Column:=0; if StartDocPrinter(xHandle, 1, @xDocInfo)=0 then Begin Result:=False; ClosePrinter(xHandle); end; end; end; xIsOpen:=Result; end; end; function TRawPrint.Close: Boolean; begin if xIsOpen then Result:=ClosePrinter(xHandle); end; Procedure TRawPrint.SetPos(xRow,xCol : Integer); begin Column:=xCol; Row:=xRow; end; Function TRawPrint.InitPrinter: Boolean; begin Column:=0; Row:=0; if Condensed then Write(SeqCondensed+#13) else Write(SeqNormal+#13); Result:=True; end; Procedure TRawPrint.Go(xRow, xCol: Integer); var i: Integer; begin if RowxRow then GoTop; i:=Row; while i Write(#10); inc(i); Row:=i; end; if ColumnxCol then begin Write(#13); Column:=0; end; i:=Column; if ixCol then Write( Format('%-*s',[xCol-Column,'']) ); Column:=xCol; end; Procedure TRawPrint.GoTop; begin Go(RowsPage,0); Column:=0; Row:=0; end; Procedure TRawPrint.Print(xRow, xCol: Integer; xText: String); begin go(xRow,xCol); if Write(xText) then Column:=Column+xBytesWritten; end; Procedure TRawPrint.NewPage; begin Write(#12+#13); Column:=0; Row:=0; end; function TRawPrint.Write(xText: String): Boolean; var xBuffer: String; begin Result:=False; xBuffer:=xText; if xIsOpen then Result:=WritePrinter(xHandle, @xBuffer[1], Length(xBuffer), xBytesWritten); end; end.