C # printing and designing labels (bar codes) (I ),

Source: Internet
Author: User

C # printing and designing labels (bar codes) (I ),

I believe that companies of a relatively small scale have entered or are entering the informatization process. Especially in manufacturing enterprises, a bar code printing function is indispensable. Most of these applications use banma printers, so we will encounter the problem of printing. I have been engaged in ERP, MES and other systems for many years, and have learned some of the company's practices. Let's talk about some of the bar code printing practices. If there is any error, please leave it empty.

1. some small enterprises use the label design software to make templates and print them in the label design software. This method does not need to write code, but is not suitable for most companies, because enterprise data is dynamic and cannot be modified and printed manually, the only way out is to solve the problem by code.

2. first, complete the template, replace the dynamically changed content with the variable name, and dynamically Replace the variable in the code, then output the command to the printer. When I started this blog, I wrote a document on how ZPL prints Chinese information (for your reference ).

3. the printer is printed in drawing mode, also called GDI printing. This type of printing can be used to draw labels. when running the report, The result output bitmap is sent to the printer. (This requires a newer printer)

All of the above practices have their disadvantages. 1st is manual, with a heavy workload; the second is to understand the banma printing instructions (newcomers cannot take over); the third is a newer approach, which is not adopted by most companies; I want to introduce another method, which is close to the second type, but developers do not need to understand the banma command (EPL/ZPL ), and even the old zebra printer can be used, it has its superiority in speed and quality. The most important thing is: 1. few codes (actually some of the main methods are blocked); 2. supports ZPL and EPL. supports Chinese/Japanese printing; 4. ignore the printer connection type; 5. supports WINFORM and WEBFORM printing. In a project, you only need to reference and write code to achieve the desired result. Next we will first talk about how to print this bar code, and the design will be included in the next article. If you are interested, please pay attention to it later.

 

Because it is a DEMO, it is very simple to do.

 

 

The output.

Next we will analyze the code.

The code is quite simple.

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.txt Prolot. text. trim (). length> 15) {MessageBox. show ("the batch number length cannot exceed 15 characters", "system prompt", MessageBoxButtons. OK, MessageBoxIcon. A Sterisk); return;} if (this. chkProlot () {string strPath = Application. startupPath. toString (); string str2 = ""; str2 = strPath + @ "\ Label \ FontLib. xml "; // set the font // string barFontlist =; PrintersAndPrintType type = new PrintersAndPrintType (); if (type. showDialog ()! = DialogResult. cancel) // deselect the printer and language, and use the default value, that is, EPLII and default printer {string strSql = "PRODUCT_GETBARCODEDATA" + this. nPrintIdTmp + "','" + this. labelType + "'"; ITPrintClass class2 = new ITPrintClass (); // class2.ChineseFontName = ""; // class2.BeginPrint (); // directly send to the default printer class2.BeginPrintAt (type. ITPrinterName); // specify the printer if (string. compare (type. ITPrinterType, "ZPLII") = 0) {class2.PrinterType = tagITPri NterType. ZPLII;} if (str2! = Null) & (str2.Length> 0) {// obtain the content of the file that defines the tag class2.LoadFontLibIndexFromText (this. getDefineLabelXmlText (str2);} string strFile = strPath + @ "\ Label \" + LabelFile; // Add the path information class2.SetBarcodeDefineXmlText (this. getDefineLabelXmlText (strFile); // tag definition file if (strSql. length> 0) {class2.PrintDefinedBarcodeLabel (this. getSqlDataXmlText (strSql); // retrieved from the database} 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.txt Prolot. text + "'", connection); // check the validity of the production batch number 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 (); // tag definition file name corresponding to the batch number 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, "system prompt", MessageBoxButtons. OK, MessageBoxIcon. error); return false ;}}}}View Code

 

Let's take a look at the code. Is it very simple? Here we mainly provide a few print information (print language, printer. There is also the label definition document/font path). It is easy to print the desired barcode without having to know the banma language. So the question is, how can we design this template? Please pay attention to the next article, which is quite easy. As a program developer, you must find a way to reduce the workload and get enough spare time to do what you like.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.