views:

1861

answers:

5

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.

Thanks ya'll

Sukhbir Dadwal
Software Engineer
Seattle, WA

+4  A: 

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 Davis
A: 

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
+1  A: 

Raw printing helper class from MSDN

Steven A. Lowe
A: 

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.

A: 

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