4

I'm working on a .NET WinForms app that needs to print a FEDEX shipping label. As part of the FedEx api, I can get raw label data for the printer.

I just don't know how to send that data to the printer through .NET (I'm using C#). To be clear, the data is already pre formatted into ZPL (Zebra printer language) I just need to send it to the printer without windows mucking it up.

user7116
  • 63,008
  • 17
  • 141
  • 172
Sukhbir S. Dadwal
  • 463
  • 1
  • 8
  • 11

5 Answers5

9

C# doesn't support raw printing, you'll have to use the win32 spooler, as detailed in this KB article How to send raw data to a printer by using Visual C# .NET.

Hope this helps.

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
1

I think you just want to send the ZPL (job below) directly to your printer.

private void SendPrintJob(string job)
{
    TcpClient client = null;
    NetworkStream ns = null;
    byte[] bytes;
    int bytesRead;

    IPEndPoint remoteIP;
    Socket sock = null;

    try
    {
        remoteIP = new IPEndPoint( IPAddress.Parse(hostName), portNum );
        sock = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream,
            ProtocolType.Tcp);
        sock.Connect(remoteIP);


        ns = new NetworkStream(sock);

        if (ns.DataAvailable)
        {
            bytes = new byte[client.ReceiveBufferSize];
            bytesRead = ns.Read(bytes, 0, bytes.Length);
        }

        byte[] toSend = Encoding.ASCII.GetBytes(job);
        ns.Write(toSend, 0, toSend.Length);

        if (ns.DataAvailable)
        {
            bytes = new byte[client.ReceiveBufferSize];
            bytesRead = ns.Read(bytes, 0, bytes.Length);
        }
    }
    finally
    {           
        if( ns != null )            
            ns.Close();

        if( sock != null && sock.Connected )
            sock.Close();

        if (client != null)
            client.Close();
    }
}
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
0

I've been working with a printer and ZPL for a while now, but with a Ruby app. Sending the ZPL out to the printer via socket works fine.

To check that it works, I often telnet to the printer and type ^XA^PH^XZ to feed a single label. Hope that helps.

bluish
  • 26,356
  • 27
  • 122
  • 180
0

A little late, but you can use this CodePlex Project for easy ZPL printing http://sharpzebra.codeplex.com/

0

Zebra printers don't use a spooler, it isn't raw printing. It's a markup called ZPL. It's text based, not binary.

Beachhouse
  • 4,972
  • 3
  • 25
  • 39