我需要帮助。 我有这个代码由斑马打印,它什么都不打印。
procedure TForm1.Button3Click(Sender: TObject);
var
cm: AnsiString;
zpl : TStringList;
p: TPrinter;
i: integer;
begin
p := Printer;
zpl := TStringList.Create;
memo2.Lines.Add('^XA');
memo2.Lines.Add('^LH5,5');
memo2.Lines.Add(concat ('^FO50,20^ADN,36,20^FD', edit1.text,'^FS'));
memo2.Lines.Add(concat ('^FO35,60^ADN,15,10^FD', edit4.text,'^FS'));
memo2.Lines.Add(concat ('^FO50,115^BCN,60,N,N,N^FD', edit1.text, '^FS'));
memo2.Lines.Add('^XZ');
zpl.AddStrings (memo2.lines);
p.BeginDoc;
for i := 0 to memo2.Lines.Count-1 do
begin
zpl.Assign(memo2.lines);
end;
p.EndDoc
end;
谢谢你帮助我
我知道将这些行打印为文本,但我需要将这些行作为命令发送到Zebra打印机。
答案 0 :(得分:1)
有几种选择。您可以在RAW
模式下打开打印机,并使用WritePrinter()
编写这些命令。有一个非工作示例here及以下内容:
uses
Printers, WinSpool;
procedure Print;
var
ADevice, ADeviceName, ADevicePort: array[0..255]of Char;
PrinterHandle: THandle
DocInfo: TDocInfo1;
dwJob: cardinal;
dwBytesWritten: cardinal;
AUtf8: UTF8string;
ADeviceMode: THandle;
begin
//your printer (a windows generic printer works fine)
Printer.PrinterIndex := LocalPrinterIndex;
Printer.GetPrinter(ADevice, ADeviceName, ADevicePort, ADeviceMode);
//Need a handle to the printer
if not OpenPrinter(ADevice, FPrinterHandle, nil) then
Exit;
//Fill in the structure with info about this "document"
DocInfo.pDocName := PChar('Spooler Document Name');
DocInfo.pOutputFile := nil;
DocInfo.pDatatype := 'RAW';
//Inform the spooler the document is beginning
dwJob := StartDocPrinter(PrinterHandle, 1, @DocInfo);
if dwJob = 0 then
begin
ClosePrinter(PrinterHandle);
FPrinterHandle := 0;
Exit;
end;
//Start a page
if not StartPagePrinter(PrinterHandle) then
begin
EndDocPrinter(PrinterHandle);
ClosePrinter(PrinterHandle);
FPrinterHandle := 0;
Exit;
end;
//your zebra code...
AUtf8 := UTF8string('Hello world');
WritePrinter(PrinterHandle, @AUtf8[1], Length(AUtf8), dwBytesWritten);
//End the page
if not EndPagePrinter(PrinterHandle) then
begin
EndDocPrinter(PrinterHandle);
ClosePrinter(PrinterHandle);
FPrinterHandle := 0;
Exit;
end;
//Inform the spooler that the document is ending
if not EndDocPrinter(PrinterHandle) then
begin
ClosePrinter(PrinterHandle);
FPrinterHandle := 0;
Exit;
end;
//Tidy up the printer handle
ClosePrinter(PrinterHandle);
FPrinterHandle := 0;
end;
或者您可以为该打印机安装Generic Text Only驱动程序,并使用旧式Pascal WriteLn调用进行打印,如here所述。您需要创建自己的SelectPrinter()
方法。包括在下面:
procedure PrintLabel(LabelCommands: TStringList);
var
F: TextFile;
I: Integer;
begin
SelectPrinter('LABEL PRINTER');
AssignPrn(F);
ReWrite(F);
for I := 0 to LabelCommands.Count - 1 do
WriteLn(F,LabelCommands[I]);
CloseFile(F);
end;