Sending raw data to FedEx Label printer

前端 未结 6 1107
离开以前
离开以前 2020-12-17 04:43

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

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 05:19

    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();
        }
    }
    

提交回复
热议问题