C# 標籤(條碼)的列印與設計(一)

來源:互聯網
上載者:User

標籤:des   winform   style   blog   http   io   color   ar   os   

      相信目前稍有規模的公司已進入或正在進入資訊化之中,尤其在製造企業中,少不了一個條碼的列印功能,而這類應用大多是使用斑馬印表機,所以就會遇到了怎麼列印的問題了。本人也已經從事ERP,MES等系統多年,也有去瞭解過一些公司的做法。知道條碼的列印的一些做法,下面我們來談一談,如有錯誤之處,請大家不吝指出。

  1.一些規模小的企業是用標籤設計軟體做好模板,在標籤設計軟體中列印,這種辦法不用寫代碼,但對大多數公司來說並不適合,因為企業的資料動態比較多,如果純手工修改列印肯定不能接受,於是唯一的出路只能是代碼解決問題。

      2.首先做好模板,然後替換其中動態變化的內容為變數名,在代碼中動態替換變數,再把指令輸出至印表機而在一開這個部落格的時候我就寫了一篇ZPL如何列印中文資訊的隨筆(大家可以去參考一下)。

  3.還有就是用繪圖方式列印至印表機的,也叫GDI列印,這種可以用報表工具畫好標籤,運行報表時,把結果輸出位元影像,再發送至印表機。(這種需要較新的印表機)

上面的這些做法都有其缺點。第1是手動,工作量大;第二是需要瞭解斑馬列印指令(新人不便接手);第三是較新的做法,大多公司不是採取這種方式;而我要介紹的是另一種做法,而這種做法比較接近第二類,但又不需要開發人員瞭解斑馬指令(EPL/ZPL),而且就算再老的斑馬印表機也能用,在速度和品質上都有其優越性。最最主要的是:1.代碼相當的少(實際是封了主要一些方法);2.支援ZPL,EPL兩種語言;3.支援中文/日文列印;4.無須理會印表機的連線類型;5.支援WINFORM和WEBFORM的列印。在項目之中只需要簡單的引用和書寫代碼即可達到你想要的結果。 下面我們首先談一下如何列印這個條碼,而列印這一塊將會放到下一篇隨筆,有興趣的後續可以留意一下。

 

由於是一個DEMO,做得非常的簡單。

 

 

列印出來的結果。

下面我們來分析一下代碼。

代碼是相當的簡單。

namespace FormExample{    public partial class Form2 : Form    {//Created by zhuhl on 2014-11-16        private int nPrintIdTmp;        private int LabelType;        private string LabelFile = string.Empty;                public Form2()        {            InitializeComponent();        }        private void btnPrint_Click(object sender, EventArgs e)        {            if (this.txtProlot.Text.Trim().Length > 15)            {                MessageBox.Show("批號長度不能超過15位","系統提示",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);                return;            }            if (this.ChkProlot())            {                string strPath = Application.StartupPath.ToString();                string str2 = "";                str2 = strPath + @"\Label\FontLib.xml"; //設定字型                // string barFontlist = ;                PrintersAndPrintType type = new PrintersAndPrintType();                if (type.ShowDialog() != DialogResult.Cancel) // 取消選印表機及語言,直接取預設,即EPLII 和 預設印表機                {                    string strSql = "PRODUCT_GETBARCODEDATA ‘" + this.nPrintIdTmp + "‘,‘" + this.LabelType + "‘";                    ITPrintClass class2 = new ITPrintClass();                    // class2.ChineseFontName = "宋體";                    // class2.BeginPrint(); // 直接發送到預設印表機                    class2.BeginPrintAt(type.ITPrinterName); //指定印表機                                    if (string.Compare(type.ITPrinterType, "ZPLII") == 0)                    {                        class2.PrinterType = tagITPrinterType.ZPLII;                    }                    if ((str2 != null) && (str2.Length > 0))                    {                        //擷取定義標籤的檔案的內容                        class2.LoadFontLibIndexFromText(this.GetDefineLabelXmlText(str2));                    }                    string strFile = strPath + @"\Label\" + LabelFile;//添加路徑資訊                    class2.SetBarcodeDefineXmlText(this.GetDefineLabelXmlText(strFile)); //標貼定義檔                    if (strSql.Length > 0)                    {                        class2.PrintDefinedBarcodeLabel(this.GetSqlDataXmlText(strSql));//從資料庫取的                    }                    else                    {                        class2.PrintDefinedBarcodeLabel(null);                    }                    class2.EndPrint();                    class2 = null;                }            }                               }        private string GetDefineLabelXmlText(string filepath)        {            XmlDocument xmlDoc = new XmlDocument();            xmlDoc.Load(filepath);            StringWriter w = new StringWriter();            XmlTextWriter writer = new XmlTextWriter(w);            writer.Formatting = Formatting.Indented;            xmlDoc.Save(writer);            writer.Close();            return w.ToString();        }        private string GetSqlDataXmlText(string strSql)        {            XmlDocument document = new XmlDocument();                       document.LoadXml("<BarcodeSqlData/>");            SqlConnection connection = new SqlConnection(this.ConnectionString);            SqlCommand command = new SqlCommand(strSql, connection);            connection.Open();            SqlDataReader reader = command.ExecuteReader();            while (reader.Read())            {                XmlElement newChild = document.CreateElement("FieldData");                int num2 = reader.FieldCount - 1;                for (int i = 0; i <= num2; i++)                {                    newChild.SetAttribute(reader.GetName(i), Convert.ToString(RuntimeHelpers.GetObjectValue(reader.GetValue(i))).Trim());                }                document.DocumentElement.AppendChild(newChild);            }            reader.Close();            connection.Close();            StringWriter w = new StringWriter();            XmlTextWriter writer = new XmlTextWriter(w);            writer.Formatting = Formatting.Indented;            document.Save(writer);            writer.Close();            return w.ToString();        }        protected string ConnectionString        {            get            {                string str = "SERVER";                string str2 = "USER";                string str3 = "PASSWORD";                string str4 = "DATABASE";                string str5 = string.Empty;                // str5 = "Persist Security Info=True;Password=" + str3 + ";User ID=" + str2;                str5 = "Persist Security Info=True;Password=" + str3 + ";User ID=" + str2;                return (str5 + ";Initial Catalog=" + str4 + ";Data Source=" + str + ";Connect Timeout=60");            }        }        private bool ChkProlot()        {            string str2 = "";            SqlConnection connection = new SqlConnection(this.ConnectionString);            SqlCommand command = new SqlCommand("ZZLABEL_TEST ‘" + this.txtProlot.Text + "‘", connection); //檢測生產批號的有效性            bool flag = true;            connection.Open();            try            {                SqlDataReader reader = command.ExecuteReader();                while (reader.Read())                {                    flag = false;                    str2 = reader.GetString(1).Trim();                    this.nPrintIdTmp = reader.GetInt32(2);                }                reader.Close();                reader = new SqlCommand("PRODUCT_GETLABELFILE ‘" + str2 + "‘,‘" + this.nPrintIdTmp.ToString() + "‘", connection).ExecuteReader();//這個批號對應的標籤定義檔案名稱                while (reader.Read())                {                    LabelFile = reader.GetString(0).Trim();                    LabelType = reader.GetInt32(1);                }                reader.Close();                connection.Close();                return true;            }            catch (Exception ex)            {                MessageBox.Show(ex.Message, "系統提示", MessageBoxButtons.OK, MessageBoxIcon.Error);                return false;            }        }         }}
View Code

 

大家看一下代碼,是否非常的簡單呢? 這裡主要是提供幾個列印資訊便可(列印語言,印表機。還有就是標籤定義文檔/字型檔路徑),根本不太需要瞭解斑馬的語言就可以輕易列印出來想要的條碼。那麼問題來了,到底如何設計這個模板呢? 敬請留意下一篇文章,也是相當的容易的。做為程式開發人員,肯定要想辦法減輕工作量,以獲得足夠多的業餘時間做自己喜歡做的事。

C# 標籤(條碼)的列印與設計(一)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.