Author: Alejandro Castro
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:
1 var
2 xPrn: TRawPrint;
3 begin
4 xPrn := TRawPrint.Create; // create an instance
5 xPrn.PrinterName := 'Name of the Generic printer';
6 if xPrn.Open then
7 begin // if I can open the printer
8 xPrn.Condensed := True; // print to 16 cpi
9 xPrn.InitPrinter;
10 xPrn.Print(2, 10, 'My Text'); // I set the row, column and text to print
11 xPrn.LaserPrinter := True; // I can do it with laser printers;
12 xPrn.write('Another Text'); // I dont need to specify the row and column
13 xPrn.NewPage; // Form Feed
14 xPrn.Close: // I close the printer
15 end;
16 xPrn.Free; // release the instance
17 end;
18
19 //The class:
20
21 unit URaw;
22 {
23 Unit to print raw text
24 Author: Alejandro Castro
25 Date 16/Jul/2000
26 }
27
28 interface
29
30 uses SysUtils, Windows, WinSpool;
31
32 type
33 TRawPrint = class(TObject)
34
35 private
36 xIsOpen: Boolean;
37 xHandle: THandle;
38 xBytesWritten: DWord;
39 xDocInfo: TDocInfo1;
40 xIsMatrix: Boolean; // is a matrix printer ?
41 function ReadLasPrt: Boolean;
42 procedure WriteLasPrt(const Value: Boolean);
43 function ReadMatPrt: Boolean;
44 procedure WriteMatPrt(const Value: Boolean);
45
46 public
47 Row: Integer; // current row
48 Column: Integer; // current column
49 RowsPage: Integer; // no. of rows per page
50 Document: string; // name of the document for winspool
51 PrinterName: string; // name of the raw printer
52 Condensed: Boolean; // print on condensed mode
53 SeqCondensed: string; // sequence of chars for print to 16 cpi
54 SeqNormal: string; // sequence of chars for print to 10 cpi
55
56 constructor Create;
57 function Open: Boolean; // open the printer
58 function Close: Boolean; // close the printer
59 function InitPrinter: Boolean;
60 function write(xText: string): Boolean;
61 procedure SetPos(xRow, xCol: Integer);
62 procedure Go(xRow, xCol: Integer); // force to move the head of the printer
63 procedure GoTop; // go to the begining of the next page or form
64 procedure NewPage; // form feed
65 procedure Print(xRow, xCol: Integer; xText: string);
66 // print xText on the row, col
67 property MatrixPrinter: Boolean read ReadMatPrt write WriteMatPrt;
68 property LaserPrinter: Boolean read ReadLasPrt write WriteLasPrt;
69
70 end;
71
72 implementation
73
74 constructor TRawPrint.Create;
75 begin
76 Row := 0;
77 Column := 0;
78 RowsPage := 66;
79 xIsOpen := False;
80 Condensed := False;
81 Document := 'Alfra';
82 PrinterName := '';
83 MatrixPrinter := True;
84
85 end;
86
87 function TRawPrint.ReadMatPrt: Boolean;
88 begin
89 Result := xIsMatrix;
90 end;
91
92 procedure TRawPrint.WriteMatPrt(const Value: Boolean);
93 begin
94 xIsMatrix := Value;
95 SeqNormal := #18;
96 SeqCondensed := #15;
97 end;
98
99 procedure TRawPrint.WriteLasPrt(const Value: Boolean);
100 begin
101 xIsMatrix := not Value;
102 SeqNormal := #27 + '&l6D' + #27 + '(s0p10H';
103 SeqCondensed := #27 + '&l6D' + #27 + '(s0p16.66H';
104 end;
105
106 function TRawPrint.ReadLasPrt: Boolean;
107 begin
108 Result := not xIsMatrix;
109 end;
110
111 function TRawPrint.Open: Boolean;
112 begin
113 Result := False;
114 if not xIsOpen then
115 begin
116 if PrinterName <> '' then
117 begin
118 if Document = '' then
119 Document := 'Alfra';
120
121 with xDocInfo do
122 begin
123 pDocName := PChar(Document);
124 pOutputFile := nil;
125 pDatatype := 'RAW';
126 end;
127 Result := OpenPrinter(PChar(PrinterName), xHandle, nil);
128 if Result then
129 begin
130 Row := 0;
131 Column := 0;
132 if StartDocPrinter(xHandle, 1, @xDocInfo) = 0 then
133 begin
134 Result := False;
135 ClosePrinter(xHandle);
136 end;
137 end;
138 end;
139 xIsOpen := Result;
140 end;
141 end;
142
143 function TRawPrint.Close: Boolean;
144 begin
145 if xIsOpen then
146 Result := ClosePrinter(xHandle);
147 end;
148
149 procedure TRawPrint.SetPos(xRow, xCol: Integer);
150 begin
151 Column := xCol;
152 Row := xRow;
153 end;
154
155 function TRawPrint.InitPrinter: Boolean;
156 begin
157 Column := 0;
158 Row := 0;
159 if Condensed then
160 write(SeqCondensed + #13)
161 else
162 write(SeqNormal + #13);
163
164 Result := True;
165 end;
166
167 procedure TRawPrint.Go(xRow, xCol: Integer);
168 var
169 i: Integer;
170 begin
171 if Row > xRow then
172 GoTop;
173
174 i := Row;
175 while i < write(#10);
176 inc(i);
177 Row := i;
178
179 if Column > xCol then
180 begin
181 write(#13);
182 Column := 0;
183 end;
184
185 i := Column;
186 if i <> xCol then
187 write(Format('%-*s', [xCol - Column, '']));
188
189 Column := xCol;
190 end;
191
192 procedure TRawPrint.GoTop;
193 begin
194 Go(RowsPage, 0);
195 Column := 0;
196 Row := 0;
197 end;
198
199 procedure TRawPrint.Print(xRow, xCol: Integer; xText: string);
200 begin
201 go(xRow, xCol);
202 if write(xText) then
203 Column := Column + xBytesWritten;
204 end;
205
206 procedure TRawPrint.NewPage;
207 begin
208 write(#12 + #13);
209 Column := 0;
210 Row := 0;
211 end;
212
213 function TRawPrint.write(xText: string): Boolean;
214 var
215 xBuffer: string;
216 begin
217 Result := False;
218 xBuffer := xText;
219
220 if xIsOpen then
221 Result := WritePrinter(xHandle, @xBuffer[1], Length(xBuffer), xBytesWritten);
222 end;
223
224 end.
Component Download: http://www.baltsoft.com/files/dkb/attachment/URaw.zip
|