Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zebra Label Printer with C#

I'm having trouble printing a label using ZDesigner GK420T using C# .NET. I converted the following string to Bytes and passed into the printer.

^XA 
^FO3,3^AD^FDZEBRA^FS
^XZ

The expected outcome was that the printer was supposed to print 'ZEBRA' but it didn't.

My C# Code:

StringBuilder sb; sb = new StringBuilder();
if (frmPrintJob._type != 1) 
{
    sb.AppendLine("^XA"); 
    sb.AppendLine("^FO3,3^AD^FDZEBRA^FS"); 
    sb.AppendLine("^XZ"); 
} 
int intTotalPrinted = 0; 
for (int i = 1; i <= NoOfCopies; i++) 
{ 
    if (RawPrinterHelper.SendStringToPrinter(PrinterName, sb.ToString()) == true) 
        intTotalPrinted++; 
}

What am I doing wrong here? Do I need any extra code?

like image 650
Dani Mathew Avatar asked Nov 16 '25 16:11

Dani Mathew


1 Answers

First you need to clear up the following:

  • are you printing an RFID label or a barcode label
  • is the printer connected through USB or parallel port

For example the following snippet prints an RFID label on a Zebra printer, using the parallel port lpt1:

String strPath = "C:\\Zebra";
String zplStart = "CT~~CD,~CC^~CT~\r\n^XA\r\n^MMT\r\n^PW831\r\n^LL0599\r\n^LSO\r\n";
String zplMiddle = "^FT50,180^BY3^BCN,200,N,N,N^FD"; ///+barcode
String zplMiddle2 = "^FS\r\n^FT600,145^AAN,30,10,^FH\\^FD";///+barcode 

String zplMiddle3 = "^FS^^RS8,,800,5^RFW,H^FD";//+RFID

String splend1 = "^FS\r\n^RWH,H^FS\r\n^RR10^FS\r\n^PQ1\r\n^XZ";
string filePath = strPath + "\\Books" + ".zpl";
string Prefix="..." //Define tag ID Prefix
string Sufix =".."//Define tag ID suffix
RFID ="Prefix"+ barcode +Sufix;
StreamWriter strw = new StreamWriter(filePath);
strw.Write(zplStart + zplMiddle + barcode + zplMiddle2 + barcode+ zplMiddle3 + RFID+  splend1); // assemble the three parts of the ZPL code

string command = "copy " + filePath + " lpt1"; //prepare a string with the command to be sent to the printer
// The /c tells cmd that we want it to execute the command that follows, and then exit.
System.Diagnostics.ProcessStartInfo sinf = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

sinf.UseShellExecute = false;
sinf.CreateNoWindow = true;

System.Diagnostics.Process p = new System.Diagnostics.Process(); // new process            
p.StartInfo = sinf;//load start info into process. 
p.Start(); //start process (send file to printer)

The above is a sample for RFID label in your case the zpl string to feed I guess is:

string zpl="^XA^\r\nFO3,3^AD^FDZEBRA^FS\r\n^XZ";

notice I am using \r\n so as to move to next line..

like image 176
apomene Avatar answered Nov 18 '25 05:11

apomene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!