使用 .NET WinForm 打印预览 ZPL II 命令,然后将其发送到 Zebra 打印机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7412242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print preview ZPL II commands using .NET WinForm before sending it to Zebra printer
提问by hasan lamaa
I have an .NET Windows application that prints commands to Zebra printer using ZPL II or EPL2. Is there any way to print preview the data in a form before printing it directly from Zebra printer?
我有一个 .NET Windows 应用程序,它使用 ZPL II 或 EPL2 将命令打印到 Zebra 打印机。在直接从 Zebra 打印机打印数据之前,有什么方法可以打印预览表格中的数据吗?
回答by Abel
Have a look at the Labelary web service, which allows you to convert ZPL to an image programmatically.
查看Labelary Web 服务,它允许您以编程方式将 ZPL 转换为图像。
Just build a URL containing the ZPL that you want to render, get the image back from the web server, and show the image to the user from within your application.
只需构建一个包含您要呈现的 ZPL 的 URL,从 Web 服务器取回图像,然后从您的应用程序中向用户显示图像。
string zpl = "YOUR ZPL HERE";
string url = "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/" + zpl;
using (WebClient client = new WebClient()) {
client.DownloadFile(url, "zpl.png");
}
There's also a ZPL viewerlets you edit and view ZPL directly on a web page.
还有一个ZPL 查看器可让您直接在网页上编辑和查看 ZPL。
回答by Vaccano
I needed the ability to show the label in my Application. So I hooked up Fiddler and figured out what the communication was to get the image of the label.
我需要能够在我的应用程序中显示标签。所以我联系了 Fiddler 并弄清楚了获得标签图像的通信是什么。
I got it running in LinqPad. The HTTP stuff can be cleaned up a bit, but I thought I would post the code for others to use:
我让它在 LinqPad 中运行。HTTP 的东西可以清理一下,但我想我会发布代码供其他人使用:
void Main()
{
string printerIpAddress = "10.92.0.167";
string zpl="^XA^CFD^CVY^PON^FWN^LS0^LT0^LH15,17^FS^FO0,2^FO14,3^FH^FDHi^FS^XZ";
// post the data to the printer
var imageName = PostZplAndReturnImageName(zpl, printerIpAddress);
// Get the image from the printer
var image = LoadImageFromPrinter(imageName, printerIpAddress);
Console.WriteLine(image);
}
public static string PostZplAndReturnImageName(string zpl, string printerIpAddress)
{
string response = null;
// Setup the post parameters.
string parameters = "data="+ zpl;
parameters = parameters + "&" + "dev=R";
parameters = parameters + "&" + "oname=UNKNOWN";
parameters = parameters + "&" + "otype=ZPL";
parameters = parameters + "&" + "prev=Preview Label";
parameters = parameters + "&" + "pw=";
// Post to the printer
response = HttpPost("http://"+ printerIpAddress +"/zpl", parameters);
// Parse the response to get the image name. This image name is stored for one retrieval only.
HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(response);
var imageNameXPath = "/html[1]/body[1]/div[1]/img[1]/@alt[1]";
var imageAttributeValue = doc.DocumentNode.SelectSingleNode(imageNameXPath).GetAttributeValue("alt","");
// Take off the R: from the front and the .PNG from the back.
var imageName = imageAttributeValue.Substring(2);
imageName = imageName.Substring(0,imageName.Length - 4);
// Return the image name.
return imageName;
}
public static string HttpPost(string URI, string Parameters)
{
System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
req.Proxy = new System.Net.WebProxy();
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending.
//Post'ed Faked Forms should be name=value&
byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write (bytes, 0, bytes.Length); //Push it out there
os.Close ();
System.Net.WebResponse resp = req.GetResponse();
if (resp== null) return null;
System.IO.StreamReader sr =
new System.IO.StreamReader(resp.GetResponseStream());
return sr.ReadToEnd().Trim();
}
public static Image LoadImageFromPrinter(string imageName, string printerIpAddress)
{
string url = "http://"+ printerIpAddress +"/png?prev=Y&dev=R&oname="+ imageName +"&otype=PNG";
var response = Http.Get(url);
using (var ms = new MemoryStream(response))
{
Image image = Image.FromStream(ms);
return image;
}
}
public static class Http
{
public static byte[] Get(string uri)
{
byte[] response = null;
using (WebClient client = new WebClient())
{
response = client.DownloadData(uri);
}
return response;
}
}
This has the following References:
这有以下参考资料:
HtmlAgilityPack
System.Drawing
System.Net
and the following Uses:
以及以下用途:
HtmlAgilityPack
System.Drawing
System.Drawing.Imaging
System.Net
NOTE: I could not find a way to communicate with a serial/non-IP Addressed printer. (Though that does not mean that there is not a way.)
注意:我找不到与串行/非 IP 寻址打印机通信的方法。(虽然这并不意味着没有办法。)
回答by tresf
If you're ok with using a Chrome Plugin, please try Simon Binkert's Zpl Printer. This plugin is based on the Labelary Web Servicelinked in previous comments.
如果您可以使用 Chrome 插件,请尝试使用 Simon Binkert 的Zpl 打印机。该插件基于之前评论中链接的Labelary Web 服务。
See also: https://stackoverflow.com/a/33066790/3196753
另见:https: //stackoverflow.com/a/33066790/3196753
Note, although this plugin runs inside Google Chrome, it can work with any application on the computer capable of sending raw commands.
请注意,尽管此插件在 Google Chrome 中运行,但它可以与计算机上能够发送原始命令的任何应用程序一起使用。
It can be configured to listen on localhost or dedicated IP address, so it can be used to emulate a ZPL printer on the PC and local network. If you need a local printer or printer share, you can set up a raw printer queue on the PC which points to it on hostname/ip and port and other desktop applications will be able to send ZPL to it.
它可以配置为侦听本地主机或专用 IP 地址,因此可用于在 PC 和本地网络上模拟 ZPL 打印机。如果您需要本地打印机或打印机共享,您可以在 PC 上设置一个原始打印机队列,该队列在主机名/IP 和端口上指向它,其他桌面应用程序将能够向它发送 ZPL。
Note, if you need to make it available to other computers on the network, make sure to change 127.0.0.1in the Printer Settings to a valid LAN IP address.
请注意,如果您需要将其提供给网络上的其他计算机,请确保127.0.0.1在打印机设置中将其更改为有效的 LAN IP 地址。
回答by Ovi Tisler
The only way to preview the label is on the printer's web page.
预览标签的唯一方法是在打印机的网页上。
If you go to the printer's directory listing http://<printer IP>/dirand click on the saved label (or create a new one) then you can click "Preview Label"
如果您转到打印机的目录列表http://<printer IP>/dir并单击保存的标签(或创建一个新标签),则可以单击"Preview Label"


