C# 将纯文本发送到默认打印机(Zebra 打印机)

本文关键字:打印机 默认 Zebra 文本 | 更新日期: 2023-09-27 18:34:02

文本发送到默认打印机的最佳选择是什么?

打印机是斑马,文本是一串 ZPL。

许多例子都是字体大小,图形,点(x,y)。非常令人困惑。

但是我需要发送字符串,打印机会完成它的工作。

C# 将纯文本发送到默认打印机(Zebra 打印机)

如果使用

LPT 或 COM 端口连接,则可以使用 p/invoke 直接打开端口以OpenFile,否则需要使用打印票证 API 来创建RAW格式的作业。 有关帮助程序类的帮助程序类,请参阅 http://support.microsoft.com/?kbid=322091,该类调用相应的平台函数以允许来自 C# 的 RAW 打印作业。

您的 Zebra 打印机是否在网络上?

如果是这样,这将起作用-

// Printer IP Address and communication port
string ipAddress = "10.3.14.42";
int port = 9100;
// ZPL Command(s)
string ZPLString =
"^XA" +
"^FO50,50" +
"^A0N50,50" +
"^FDHello, World!^FS" +
"^XZ";
try
{
    // Open connection
    using (System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient())
    {
        client.Connect(ipAddress, port);
        // Write ZPL String to connection
        using (System.IO.StreamWriter writer = new System.IO.StreamWriter(client.GetStream()))
        {
            writer.Write(ZPLString);
            writer.Flush();
        }
    }
}
catch (Exception ex)
{
     // Catch Exception
}

我也成功地将这个库用于 USB。