欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现的ZPL条码打印类完整实例

程序员文章站 2024-02-07 16:30:10
本文实例讲述了c#实现的zpl条码打印类。分享给大家供大家参考,具体如下: using system; using system.collections.gen...

本文实例讲述了c#实现的zpl条码打印类。分享给大家供大家参考,具体如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.io;
using system.diagnostics;
using system.drawing;
using system.drawing.drawing2d;
using system.runtime.interopservices;
namespace zebra
{
  /// <summary>
  /// zpl帮助类
  /// </summary>
  public class zebrahelper
  {
    /*
     * 打印中文先引用fnthex32.dll
      dll控件常规安装方法(仅供参考):
      下面是32系统的注册bat文件
      可将下面的代码保存为“注册.bat“,放到dll目录,就会自动完成dll注册(win98不支持)。
      @echo 开始注册
      copy fnthex32.dll %windir%\system32\
      regsvr32 %windir%\system32\fnthex32.dll /s
      @echo fnthex32.dll注册成功
      @pause
      下面是64系统的注册bat文件
      @echo 开始注册
      copy fnthex32.dll %windir%\syswow64\
      regsvr32 %windir%\syswow64\fnthex32.dll /s
      @echo fnthex32.dll注册成功
      @pause
     *
     *
      zebrahelper zh = new zebrahelper();
      stringbuilder builder = new stringbuilder();
      builder.appendline(zh.zpl_start());
      builder.appendline(zh.zpl_pageset(40, 80));
      builder.appendline(zh.zpl_drawchtext("上善若水 厚德载物", "宋体", 40, 40, 0, 32, 0, 1, 0));
      builder.appendline(zh.zpl_drawbarcode(40, 150, 3, 2, 40, "111112222233333"));
      builder.appendline(zh.zpl_drawentext("111112222233333", "a", 30, 205, "n", 30, 50));
      builder.appendline(zh.zpl_drawrectangle(20,20,2,700,700));
      builder.appendline(zh.zpl_end());
      string a = builder.tostring();
      //打印
      zh.cmddos("c:\\c.txt", a);
     */
    public string zpl_start()
    {
      stringbuilder builder = new stringbuilder();
      builder.appendline("^xa"); //指令块的开始
      builder.appendline("^md30"); //md是设置色带颜色的深度
      return builder.tostring();
    }
    public string zpl_end()
    {
      stringbuilder builder = new stringbuilder();
      builder.appendline("^xz"); //指令块的结束
      return builder.tostring();
    }
    /// <summary>
    /// 设置打印标签纸边距
    /// </summary>
    /// <param name="printx">标签纸边距x坐标</param>
    /// <param name="printy">标签纸边距y坐标</param>
    /// <returns></returns>
    public string zpl_pageset(int printx, int printy)
    {
      stringbuilder builder = new stringbuilder();
      builder.appendline("^lh" + printx + "," + printy); //定义条码纸边距 80 10
      return builder.tostring();
    }
    /// <summary>
    /// 打印凭条设置
    /// </summary>
    /// <param name="width">凭条宽度</param>
    /// <param name="height">凭条高度</param>
    /// <returns>返回zpl命令</returns>
    public string zpl_setlabel(int width, int height)
    {
      //zpl条码设置命令:^pw640^ll480
      string sreturn = "^pw{0}^ll{1}";
      return string.format(sreturn, width, height);
    }
    /// <summary>
    /// 打印矩形
    /// </summary>
    /// <param name="px">起点x坐标</param>
    /// <param name="py">起点y坐标</param>
    /// <param name="thickness">边框宽度</param>
    /// <param name="width">矩形宽度,0表示打印一条竖线</param>
    /// <param name="height">矩形高度,0表示打印一条横线</param>
    /// <returns>返回zpl命令</returns>
    public string zpl_drawrectangle(int px, int py, int thickness, int width, int height)
    {
      //zpl矩形命令:^fo50,50^gb300,200,2^fs
      string sreturn = "^fo{0},{1}^gb{3},{4},{2}^fs";
      return string.format(sreturn, px, py, thickness, width, height);
    }
    /// <summary>
    /// 打印英文
    /// </summary>
    /// <param name="entext">待打印文本</param>
    /// <param name="zebrafont">打印机字体 a-z</param>
    /// <param name="px">起点x坐标</param>
    /// <param name="py">起点y坐标</param>
    /// <param name="orient">旋转角度n = normal,r = rotated 90 degrees (clockwise),i = inverted 180 degrees,b = read from bottom up, 270 degrees</param>
    /// <param name="height">字体高度</param>
    /// <param name="width">字体宽度</param>
    /// <returns>返回zpl命令</returns>
    public string zpl_drawentext(string entext,string zebrafont, int px, int py, string orient, int height, int width)
    {
      //zpl打印英文命令:^fo50,50^a0n,32,25^fdzebra^fs
      string sreturn = "^fo{1},{2}^a" + zebrafont + "{3},{4},{5}^fd{0}^fs";
      return string.format(sreturn, entext, px, py, orient, height, width);
    }
    /// <summary>
    /// 中文处理,返回zpl命令
    /// </summary>
    /// <param name="chinesetext">待转变中文内容</param>
    /// <param name="fontname">字体名称</param>
    /// <param name="startx">x坐标</param>
    /// <param name="starty">y坐标</param>
    /// <param name="orient">旋转角度0,90,180,270</param>
    /// <param name="height">字体高度</param>
    /// <param name="width">字体宽度,通常是0</param>
    /// <param name="isbold">1 变粗,0 正常</param>
    /// <param name="isitalic">1 斜体,0 正常</param>
    /// <returns></returns>
    public string zpl_drawchtext(string chinesetext, string fontname, int startx, int starty, int orient, int height, int width, int isbold, int isitalic)
    {
      stringbuilder sresult = new stringbuilder();
      stringbuilder hexbuf = new stringbuilder(21 * 1024);
      int count = zebrahelper.getfonthex(chinesetext, fontname, orient, height, width, isbold, isitalic, hexbuf);
      if (count > 0)
      {
        string send = "^fo" + startx.tostring() + "," + starty.tostring() + "^xgoutstr" + ",1,2^fs ";
        sresult.appendline(hexbuf.tostring().replace("outstr01", "outstr") + send);
      }
      return sresult.tostring();
    }
    /// <summary>
    /// 打印条形码(128码)
    /// </summary>
    /// <param name="px">起点x坐标</param>
    /// <param name="py">起点y坐标</param>
    /// <param name="width">基本单元宽度 1-10</param>
    /// <param name="ratio">宽窄比 2.0-3.0 增量0.1</param>
    /// <param name="barheight">条码高度</param>
    /// <param name="barcode">条码内容</param>
    /// <returns>返回zpl命令</returns>
    public string zpl_drawbarcode(int px, int py, int width, int ratio, int barheight, string barcode)
    {
      //zpl打印英文命令:^fo50,260^by1,2^bcn,100,y,n^fdsmjh2000544610^fs
      string sreturn = "^fo{0},{1}^by{2},{3}^bcn,{4},n,n^fd{5}^fs";
      return string.format(sreturn, px, py, width, ratio, barheight, barcode);
    }
    /// <summary>
    /// 中文处理
    /// </summary>
    /// <param name="chinesetext">待转变中文内容</param>
    /// <param name="fontname">字体名称</param>
    /// <param name="orient">旋转角度0,90,180,270</param>
    /// <param name="height">字体高度</param>
    /// <param name="width">字体宽度,通常是0</param>
    /// <param name="isbold">1 变粗,0 正常</param>
    /// <param name="isitalic">1 斜体,0 正常</param>
    /// <param name="returnpicdata">返回的图片字符</param>
    /// <returns></returns>
    [dllimport("fnthex32.dll")]
    public static extern int getfonthex(
      string chinesetext,
      string fontname,
      int orient,
      int height,
      int width,
      int isbold,
      int isitalic,
      stringbuilder returnpicdata);
    /// <summary>
    /// 中文处理
    /// </summary>
    /// <param name="chinesetext">待转变中文内容</param>
    /// <param name="fontname">字体名称</param>
    /// <param name="filename">返回的图片字符重命</param>
    /// <param name="orient">旋转角度0,90,180,270</param>
    /// <param name="height">字体高度</param>
    /// <param name="width">字体宽度,通常是0</param>
    /// <param name="isbold">1 变粗,0 正常</param>
    /// <param name="isitalic">1 斜体,0 正常</param>
    /// <param name="returnpicdata">返回的图片字符</param>
    /// <returns></returns>
    [dllimport("fnthex32.dll")]
    public static extern int getfonthex(
               string chinesetext,
               string fontname,
               string filename,
               int orient,
               int height,
               int width,
               int isbold,
               int isitalic,
               stringbuilder returnpicdata);
    #region 扩展
    /// <summary>
    /// 毫米转像素 取整
    /// </summary>
    /// <param name="mm">毫米</param>
    /// <param name="dpi">打印dpi 如300</param>
    /// <returns></returns>
    public double mm2px(double mm, double dpi)
    {
      double px = (mm / 25.4) * dpi;
      return math.round(px, 0, midpointrounding.awayfromzero);
    }
    /// <summary>
    /// 像素转毫米 取整
    /// </summary>
    /// <param name="px">像素</param>
    /// <param name="dpi">打印dpi 如300</param>
    /// <returns></returns>
    public double px2mm(double px, double dpi)
    {
      //像素转换成毫米公式:(宽度像素/水平dpi)*25.4;
      double mm = (px / dpi) * 25.4;
      return math.round(mm, 0, midpointrounding.awayfromzero);
    }
    /// <summary>
    /// 生成zpl命令 且执行
    /// </summary>
    /// <param name="path">zpl文件路径</param>
    /// <param name="zpl">zpl命令</param>
    public void cmddos(string path,string zpl)
    {
      filestream fs = new filestream(path, filemode.create, fileaccess.write);
      streamwriter sw = new streamwriter(fs, encoding.default);//ansi编码格式
      if (file.exists(path))
      {
        sw.write(zpl);
        sw.flush();
        sw.close();
      }
      fs.close();
      runcmd("print /d:com1 " + path + " ");
    }
    /// <summary>
    /// 运行dos命令
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    private bool runcmd(string command)
    {
      //实例一个process类,启动一个独立进程
      process p = new process();
      //process类有一个startinfo属性,這個是processstartinfo类,包括了一些属性和方法,下面我們用到了他的几个属性:
      p.startinfo.filename = "cmd.exe";//设定程序名
      p.startinfo.arguments = "/c " + command;//设定程式执行参数
      p.startinfo.useshellexecute = false;//关闭shell的使用
      p.startinfo.redirectstandardinput = true;//重定向标准输入
      p.startinfo.redirectstandardoutput = true;//重定向标准输出
      p.startinfo.redirectstandarderror = true;//重定向错误输出
      p.startinfo.createnowindow = true;//设置不显示窗口
      //p.standardinput.writeline(command);//也可以用这种方式输入要执行的命令
      //p.standardinput.writeline("exit");//不过要记得加上exit要不然下一行程式执行的時候会当机
      try
      {
        p.start();//开始进程
        return true;
      }
      catch
      {
      }
      finally
      {
        if (p != null)
          p.close();
      }
      return false;
    }
    #endregion
  }
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#程序设计之线程使用技巧总结》、《c#操作excel技巧总结》、《c#中xml文件操作技巧汇总》、《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#数组操作技巧总结》及《c#面向对象程序设计入门教程

希望本文所述对大家c#程序设计有所帮助。