C#-uses ZPL language to complete barcode generation and printing

Source: Internet
Author: User

Recently, due to the needs of the company project, I studied a new technology for me-barcode generation and printing. Since no prior contact with this knowledge, so just started a little confused and small excitement, just step by step, the problem will always be solved. Now to sum up to do the bar code this piece of harvest.

The production of bar code is not difficult, is to use some string processing functions to generate a string of letters or numbers of the combination. This is mainly about how to print the generated barcode value. First we have to contact a new language ZPL (zebraprogramming Language) look at the name zebra know about the zebra; indeed, ZPL is the language of the zebra company itself (Zebra's business is mainly to make Zebra barcode printers). Today most barcode printers are able to recognize ZPL instructions, we can write a template with the ZPL instruction, and then automatically generate their own barcode values (strings) in a certain format format into a new string. You can then pass the content to the printer.

To take a demo to illustrate the details of the operation:

This is a template I wrote: (Name is Barcodepath, save format is.) TXT or. prn format, and be sure to note that uppercase. )

---------------------------------------------------------------------------------------------------------

^xa

^pw1000

^md30

^lh0,0

#CN_DATA1 #;| Song Body |20|15|^fo150,0

#CN_DATA2 #;| Song Body |20|15|^fo650,0

^fo70,0^by2.0,3.0^bcn,120,y,n,n,a^fd#id1#^fs

^fo570,0^by2.0,3.0^bcn,120,y,n,n,a^fd#id2#^fs

^pq1,0,1,y

^xz

ZPL language is very well understood, are some fixed instructions, understand on the line.

---------------------------------------------------------------------------------------------------------

^xa and ^xz: Each represents the beginning and end of a block of instructions, and is something fixed.

^PW: Print width, if the width is small, then the effect of printing is not complete.

^MD: Set the depth of the label color, the value range is -30 to 30, the above instructions will color to the deepest.

^LH: Set the margin of bar code paper, this template is not set.

---------------------------------------------------------------------------------------------------------

The following is an example of a string abc^edf~l000001^l000002 (ABC, EDF is Chinese, L000001, L000002 is a barcode value)

#CN_DATA1 #: A placeholder for ABC.

#CN_DATA2 #: A placeholder for EDF.

XXFarEastFont-Arial |20|15|: Refers to the font for the song body, the character height of 20, width of 15

^FO: Sets the coordinates (70,0) of the position in the upper-left corner of the barcode.

^by: Set bar code display style, this is the most important part of the template, 2.0 is the bar code zoom level (acceptable value: 1-10 points), this number is played in small bar code, 3.0 is the ratio of the bars in the bar code (acceptable values: 2.0 to 3.0, increment 0.1, invalid for a fixed percentage of the bar code), 120 is the bar code height.

^BCN: Is the instruction for printing code128.

^FD: Set the content to be printed

^fs: Indicates a newline

#ID1 #: Refers to L000001

#ID2 #: Refers to L000002

---------------------------------------------------------------------------------------------------------

^pq50,10,1,y: Print a total of 50 labels per serial number only one card. Print the number of each group is 10, but do not pause between each group.

^pq50,10,1,n: Print a total of 50 labels per serial number only one card. Print each quantity is 10, and each group is paused after printing.

This can be written as a ^pq1,0,1,y, that is, one print at a time, not paused. Because the total number of printed sheets can be controlled in the program.

---------------------------------------------------------------------------------------------------------

Suggestion: Write a template need to try, you can first write a print demo, according to the effect of printing, continuous debugging changes to the template to achieve the desired effect.

The template should be about the same here. The following shows how to print.

You first need to introduce a encapsulated DLL file SMT.ZEBRA.dll, which is mainly done by printing operations, encapsulating a Zebraprinter class inside of him, including methods Printlabex

             Description:
        //     print barcode, RFID and other types of tags////
        Parameters:
        //   N_strtemplatefilepath:
        //     label template file name, Does not contain a path, such as: "TEMPLATE." TXT "////   n_strlabels:
        //     Tag data collection string, multiple label lines with ' | ' segmentation, single label row data internal use ' ^ ' (field) segmentation or ' ~ ' (text or barcode) segmentation, such as "Research and Development department ^51296829~8019^8020|11g~51296829^8001^8012" noted the following printing points:
        //     1. Print two lines of labels (commonly known as two) 2. The first line of labels prints two text ("R/d", "51296829"), 2 barcodes ("8019", "8020")
        //     3. The second line of labels prints 1 text ("11G"), 3 barcodes ("51296829", "8001 "," 8012 ")
        //   N_strprintername:
        //     printer name, etc., such as" Zdesigner 888-tt "public
bool Printlabex ( String N_strtemplatefilepath, String n_strlabels, string n_strprintername);

Client code:

        private void Button1_Click (object sender, EventArgs e) {try {//Real

                An example of a printing class Zebraprinter printer = new Zebraprinter ();
                Defines two tag values string str1 = "L000001";
                String str2 = "L000002"; Format to a new string str1 = String.   Concat ("", "^", "" "," ~ ", str1," ^ ", str2); Double-row Barcode//str = string.      Concat ("", "~", str); Single-row barcode//printpath: template name//combobox1.text: Printer name string printpath = "Barco
                DePath.TXT "; Printer.

            Printlabex (Printpath, str1, Combobox1.text); The catch (Exception ex) {MessageBox.Show (ex).
            message); } private void Smtzebrawinformzpl_load (object sender, EventArgs e) {//Get the installed on the local computer There are printers, saved to the collection strcollects printersettings.stringcollection strcollects= Printersettings.installedprinters; String strprintername = String.
            Empty; Iterate through the collection, loading all printers into the Drop-down list comboBox1 foreach (String strName in strcollects) {Strprinte
                Rname = Strname.tostring ();
            COMBOBOX1.ITEMS.ADD (Strprintername);
        } Combobox1.selectedindex = 0x00;
 }



Printing effect:


Where there are problems welcome to point out.

Code download

Oノo════════════════════════════╲

│ヽ. Programming learning ing ....                             Welcome to the Exchange. │

│http://blog.csdn.net/u010028869. ヽ│

                                              ╲═════════════════════════════ヾ

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.