程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成的ZPL條碼打印類完全實例

C#完成的ZPL條碼打印類完全實例

編輯:C#入門知識

C#完成的ZPL條碼打印類完全實例。本站提示廣大學習愛好者:(C#完成的ZPL條碼打印類完全實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的ZPL條碼打印類完全實例正文


本文實例講述了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().WordStr("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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved