Printing label and Barcodes using ZPL printer from C#.Net
Here I am printing Label using PrintLabel Method and printing Barcode using PrintBarcode method. We can use this code windows,web or any other applications which are using code behide as C#.Net. It's works if ZPL printer is connected via parallel port or via Bluetooth to your system.
Here I am passing ZPL language command to zpl printer (zpl printer supports EPL and ZPL commands).
Note:Code is written according to my requirements. You can change it according to your requirements. If anyone needs help then you can shoot an email to hareesh434@gmail.com with subject as Share your doubts and answers with IT world. If I can i will help you.
C#.Net Code
Below class is for passing the ZPL command to printer
public class UpcLabel
{
private string strFirstName;
private string strLastName;
private string strNoOfCopies;
private string PickUpTime;
private string Cold;
private string NewMember;
public UpcLabel()
{
}
public UpcLabel(string strFirstName, string strLastName, string strNoOfCopies, string PickUpTime, string Cold, string NewMember)
{
if (strFirstName == null || strLastName == null || strNoOfCopies == "" || strNoOfCopies=="0")
{
throw new ArgumentNullException("strFirstName");
}
this.strFirstName = strFirstName;
this.strLastName = strLastName;
this.strNoOfCopies = strNoOfCopies;
this.PickUpTime = PickUpTime;
this.Cold = Cold;
this.NewMember = NewMember;
}
public void PrintBarcode(string printerName, string pProductName, string pBarcode,string strNumOfCopies)
{
if (printerName == null)
{
throw new ArgumentNullException("printerName");
}
StringBuilder strBldr = new StringBuilder();
strBldr.AppendLine("^XA");
strBldr.AppendLine("^FO40,100");
strBldr.AppendLine("^AQ,50,30");
// sb1.AppendLine("^FDAnja^FS");
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", pProductName));
//^FO100,100^BY3
strBldr.AppendLine("^FO80,200^BY3");
//^BCN,150,Y,N,Y,N
strBldr.AppendLine("^BCN,125,Y,N,Y,N");
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", pBarcode));
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", strNumOfCopies));
strBldr.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, strBldr.ToString());
}
public void Print(string printerName)
{
if (printerName == null)
{
throw new ArgumentNullException("printerName");
}
StringBuilder sb1=new StringBuilder();
//^XA=Indicates Starting of Zpl
sb1.AppendLine("^XA");
sb1.AppendLine("^LL350");//^FS
sb1.AppendLine("^PW930");//^FS
sb1.AppendLine("^FO10,10");
sb1.AppendLine("^AQ,80,80");
// sb1.AppendLine("^FDAnja^FS");
//FOa,b
//a=Postion from x-axis
//b=Position from y-axis
//Aa,b,c
//a=Font size Like Q,V,R,0
//b=Font width
//c=Font Height
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.strFirstName));
sb1.AppendLine("^FO10,68");
sb1.AppendLine("^AQ,80,80");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.strLastName));
sb1.AppendLine("^FO10,150");
sb1.AppendLine("^AQ,50,50");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.PickUpTime));
sb1.AppendLine("^FO240,150");
sb1.AppendLine("^AQ,50,50");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture,"^FD{0}^FS",this.Cold));
//^PQ2= Indicates number of copies to print
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", this.strNoOfCopies));
//sb1.AppendLine("^PQ2");
//^XZ=Indicates ending of ZPL page
sb1.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, sb1.ToString());
//for (int counter = 0; counter <Convert.ToInt32(this.strNoOfCopies); counter++)
//{
// RawPrinterHelper.SendStringToPrinter(printerName, sb1.ToString());
// System.Threading.Thread.Sleep(500);
//}
if (this.NewMember != "")
{
StringBuilder strb= new StringBuilder();
strb.AppendLine("^XA");
strb.AppendLine("^LL350");//^FS
strb.AppendLine("^PW930");//^FS
strb.AppendLine("^FO20,30");
strb.AppendLine("^AQ,80,80");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.NewMember));
strb.AppendLine("^FO20,150");
strb.AppendLine("^AQ,50,50");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.PickUpTime));
strb.AppendLine("^FO240,150");
strb.AppendLine("^AQ,50,50");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.Cold));
//^PQ2= Indicates number of copies to print
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", "1"));
//sb1.AppendLine("^PQ2");
//^XZ=Indicates ending of ZPL page
strb.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, strb.ToString());
}
}
}
For Printing
{
private string strFirstName;
private string strLastName;
private string strNoOfCopies;
private string PickUpTime;
private string Cold;
private string NewMember;
public UpcLabel()
{
}
public UpcLabel(string strFirstName, string strLastName, string strNoOfCopies, string PickUpTime, string Cold, string NewMember)
{
if (strFirstName == null || strLastName == null || strNoOfCopies == "" || strNoOfCopies=="0")
{
throw new ArgumentNullException("strFirstName");
}
this.strFirstName = strFirstName;
this.strLastName = strLastName;
this.strNoOfCopies = strNoOfCopies;
this.PickUpTime = PickUpTime;
this.Cold = Cold;
this.NewMember = NewMember;
}
public void PrintBarcode(string printerName, string pProductName, string pBarcode,string strNumOfCopies)
{
if (printerName == null)
{
throw new ArgumentNullException("printerName");
}
StringBuilder strBldr = new StringBuilder();
strBldr.AppendLine("^XA");
strBldr.AppendLine("^FO40,100");
strBldr.AppendLine("^AQ,50,30");
// sb1.AppendLine("^FDAnja^FS");
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", pProductName));
//^FO100,100^BY3
strBldr.AppendLine("^FO80,200^BY3");
//^BCN,150,Y,N,Y,N
strBldr.AppendLine("^BCN,125,Y,N,Y,N");
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", pBarcode));
strBldr.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", strNumOfCopies));
strBldr.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, strBldr.ToString());
}
public void Print(string printerName)
{
if (printerName == null)
{
throw new ArgumentNullException("printerName");
}
StringBuilder sb1=new StringBuilder();
//^XA=Indicates Starting of Zpl
sb1.AppendLine("^XA");
sb1.AppendLine("^LL350");//^FS
sb1.AppendLine("^PW930");//^FS
sb1.AppendLine("^FO10,10");
sb1.AppendLine("^AQ,80,80");
// sb1.AppendLine("^FDAnja^FS");
//FOa,b
//a=Postion from x-axis
//b=Position from y-axis
//Aa,b,c
//a=Font size Like Q,V,R,0
//b=Font width
//c=Font Height
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.strFirstName));
sb1.AppendLine("^FO10,68");
sb1.AppendLine("^AQ,80,80");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.strLastName));
sb1.AppendLine("^FO10,150");
sb1.AppendLine("^AQ,50,50");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.PickUpTime));
sb1.AppendLine("^FO240,150");
sb1.AppendLine("^AQ,50,50");
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture,"^FD{0}^FS",this.Cold));
//^PQ2= Indicates number of copies to print
sb1.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", this.strNoOfCopies));
//sb1.AppendLine("^PQ2");
//^XZ=Indicates ending of ZPL page
sb1.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, sb1.ToString());
//for (int counter = 0; counter <Convert.ToInt32(this.strNoOfCopies); counter++)
//{
// RawPrinterHelper.SendStringToPrinter(printerName, sb1.ToString());
// System.Threading.Thread.Sleep(500);
//}
if (this.NewMember != "")
{
StringBuilder strb= new StringBuilder();
strb.AppendLine("^XA");
strb.AppendLine("^LL350");//^FS
strb.AppendLine("^PW930");//^FS
strb.AppendLine("^FO20,30");
strb.AppendLine("^AQ,80,80");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.NewMember));
strb.AppendLine("^FO20,150");
strb.AppendLine("^AQ,50,50");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.PickUpTime));
strb.AppendLine("^FO240,150");
strb.AppendLine("^AQ,50,50");
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^FD{0}^FS", this.Cold));
//^PQ2= Indicates number of copies to print
strb.AppendLine(string.Format(CultureInfo.InvariantCulture, "^PQ{0}", "1"));
//sb1.AppendLine("^PQ2");
//^XZ=Indicates ending of ZPL page
strb.AppendLine("^XZ");
RawPrinterHelper.SendStringToPrinter(printerName, strb.ToString());
}
}
}
public class RawPrinterHelper
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;//szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
public void PrintLabel(string strFirstName, string strLastName, string strNoOfCopies, string PickUpTime, string Cold,string NewMember="")
{
UpcLabel lbl = new UpcLabel(strFirstName, strLastName, strNoOfCopies, PickUpTime, Cold, NewMember);
//Printer name
lbl.Print("ZDesigner QLn220 (ZPL)");
}
public void PrintBarcode(string pProductName,string pLocation,string pNoOfCopies)
{
UpcLabel upcLabel = new UpcLabel();
//Printer name
upcLabel.PrintBarcode("ZDesigner QLn220 (ZPL)", pProductName, pLocation, pNoOfCopies);
}
{
UpcLabel lbl = new UpcLabel(strFirstName, strLastName, strNoOfCopies, PickUpTime, Cold, NewMember);
//Printer name
lbl.Print("ZDesigner QLn220 (ZPL)");
}
public void PrintBarcode(string pProductName,string pLocation,string pNoOfCopies)
{
UpcLabel upcLabel = new UpcLabel();
//Printer name
upcLabel.PrintBarcode("ZDesigner QLn220 (ZPL)", pProductName, pLocation, pNoOfCopies);
}
You can change it according to your requirements. Impresion de etiquetas en transfer
ReplyDeleteI am using Zebra ZT410R 203dpi printer and I am trying to print. Print is going and printing is not working.
DeleteI am using Zebra ZT410R 203dpi printer and I am trying to print. Print is going and printing is not working.
ReplyDeleteCode is not working for asp.net webapplication.
ReplyDeleteIn my local ots working fine.but when i host how do i find default printer in the client machine and print.
Also i have multiple labels to generate and print at one click.how is it possible.
Please help
asa ni ipang botang oi bugo
ReplyDelete