SATO打印机打印C#.net windows应用程序 [英] SATO printer printing C#.net windows application

查看:519
本文介绍了SATO打印机打印C#.net windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从C#.net Windows应用程序中打印SATO打印机中的标签。通常我是通过将控件放在一个表单中并使用我能够在普通打印机中打印的打印文档和图形进行打印。但是在安装SATO打印机驱动程序之后,我甚至看不到打印预览。

How can I print labels in SATO printer from my C#.net windows application. Normally I was printing by placing the controls in a form and using print document and graphics I was able to print in normal printers. But after installing SATO printer driver even I could not see the print preview.

推荐答案

Sato打印机如果基于以太网可以只使用通过IP和PORT发送给它的ASCII字符串(9100)。下面的代码示例:



Sato Printers if ethernet based can just use a ASCII string sent to it through IP and PORT(9100). Example of code below:

public static bool SendPrintJob(string sendString, string ipAddress, int port)
        {
            bool bReturn = true;
            sendString.ThrowIfNullOrEmpty("sendString");
            ipAddress.ThrowIfNullOrEmpty("ipAddress");

            if (port <= 0)
                port = 9100;

            Socket sock = null;

            try
            {
                IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse(ipAddress), port);
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IAsyncResult results = sock.BeginConnect(remoteIP, null, null);
                bool success = results.AsyncWaitHandle.WaitOne(5000, true);

                if (!sock.Connected)
                {
                    //Raise error message to user that printer failed to print
                    bReturn = false;

                    //Socket error occured
                    Log.WriteToErrorLog("Error in socket connection: Could not connect to printer.", "", "Socket Error: SendPrintJob");
                }
                else
                {
                    byte[] sendData = Encoding.ASCII.GetBytes(sendString);
                    int result = sock.Send(sendData, sendData.Length, 0);

                    if (result == 0)
                    {
                        //Raise error message to user that printer failed to print
                        bReturn = false;

                        //Socket error occured
                        Log.WriteToErrorLog("Error in socket transmission: Could not send data to printer.", "", "Socket Error: SendPrintJob");
                    }
                }

                return bReturn;
            }
            finally
            {
                if (sock != null)
                    sock.Close();
            }
        }


这篇关于SATO打印机打印C#.net windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆