问题
i need to print a string directly into the printer
i found this code by searching
uses WinSpool, Printers
type
TDoc_Info_1 = record
pDocName: pChar;
pOutputFile: pChar;
pDataType: pChar;
end;
procedure PrintSimpleText(sPrinter, sText: String);
var
sTitle: String;
hPrinter: THandle;
PrnDocInfo: TDoc_Info_1;
lst: TStringList;
i: Integer;
n: Cardinal;
sTextLine: String;
bFound: Boolean;
begin
lst := TStringList.Create;
try
lst.Text := sText; //with CRLF
//new doc
sTitle := 'Raw print';
ZeroMemory(@PrnDocInfo, SizeOf(TDoc_Info_1));
PrnDocInfo.pDocName := PChar(sTitle);
PrnDocInfo.pDataType := 'RAW';
//find printer (if is installed in windows)
bFound := False;
for i:=1 to Printer.Printers.Count do
begin
if Pos(sPrinter, Printer.Printers.Strings[i-1])>0 then
begin
bFound := True;
sPrinter := Printer.Printers.Strings[i-1];
Printer.PrinterIndex := i-1; //set printer
Break;
end;
end;
if bFound then
begin
// open the printer
if OpenPrinter(PChar(sPrinter), hPrinter, nil) then
begin
//start
StartDocPrinter(hPrinter, 1, @PrnDocInfo);
if StartPagePrinter(hPrinter) then
begin
//print by line
for i := 1 to lst.Count do
begin
sTextLine := lst.Strings[i-1];
if not WritePrinter(hPrinter, PChar(sTextLine), Length(sTextLine), n) then
Break;
end;
//end of page
EndPagePrinter(hPrinter);
//end
EndDocPrinter(hPrinter);
end;
ClosePrinter(hPrinter);
end;
end;
finally
lst.Free;
end;
end;
and this run like this :
procedure TForm1.Button1Click(Sender: TObject);
begin
PrintSimpleText('pdfFactory Pro', 'Tis is a'#13#10'text');
showmessage('aaaa');
end;
1)
but by clicking Button1
it just show a message!!!
is it required to send a custom header with the string for print?
or what is the problem here?
2) also if you think this is not a good way tell me a better solution! i need to submit a string like this to the printer
------------------------------------------------------
your card number is 1111 1111 1111 1111
your name is mr xxxx xxxxxxx
your nationality code is 9999999999
------------------------------------------------------
your password is : 555555
-----------------------------------------------------
at first i tried to save string into a text file and send it to pronter but printer printed the file name at the top of the file
then i tried to create a bitmap image and send it to the machine but the printer is a dot matrix and don't understand the image!!
UPDATE:
this code work perfectly on my pc i think printer is detected and working fine.
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
ShellExecute(Handle, 'print', PChar(OpenDialog1.FileName), nil, nil, SW_HIDE) ;
end;
回答1:
this can do your job too
Printer.BeginDoc;
Printer.Canvas.TextOut(0,0,'Place any text here');
Printer.EndDoc;
also with canvas you can edit styling too
Printer.Canvas.Font.size:=18;
Printer.Canvas.Font.style := [fsbold];
回答2:
Example how to write some text directly to the default printer like to the text file.
uses ..., Printers;
...
procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
lst: TextFile;
begin
AssignPrn(lst);
Rewrite(lst);
try
Writeln(lst, ' your card number is 1111 1111 1111 1111');
Writeln(lst, ' your name is mr xxxx xxxxxxx');
Writeln(lst, ' your nationality code is 9999999999');
Writeln(lst, '------------------------------------------------------');
finally
CloseFile(lst);
end;
end;
Especially useful with matrix printers.
回答3:
If the printer is available over the network:
procedure Print(const AText: string);
var
F: TStreamWriter;
begin
F := TStreamWriter.Create('\\printserver\printername', False, TEncoding.Default);
try
F.Write(AText);
finally
F.Free;
end;
end;
(Tested with Delphi 2009)
回答4:
When need send commands directly to printer can use the class "Printer". To writing one string use the code:
procedure TForm1.btTextoNormalClick(Sender: TObject);
begin
ComandoAnsiString := 'Example text';
if not OpenPrinter(PChar(driverName), HandleImp, nil) then
Memo1.Lines.Add('Erro: Impressora não encontrada')
else
begin
Documento.pDocName := PChar('Minha impressão');
Documento.pOutputFile := nil;
Documento.pDataType := 'RAW';
StartDocPrinter(HandleImp, 1, @Documento);
StartPagePrinter(HandleImp);
WritePrinter(HandleImp, PAnsiChar(ComandoAnsiString), Length(ComandoAnsiString), CaracteresImpressos);
EndPagePrinter(HandleImp);
EndDocPrinter(HandleImp);
ClosePrinter(HandleImp);
end;
end;
To get the driverName use the code in a ComboBox:
cbbDriver.Items := Printer.Printers;
来源:https://stackoverflow.com/questions/25371291/print-a-string-directly-to-printer