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