C # Implementation of ZPL Barcode printing class Complete Example _c# tutorial

Source: Internet
Author: User

This example describes the ZPL barcode printing class implemented by C #. Share to everyone for your reference, specific as follows:

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 Help class///</summary> public class Zebrahelper {* * * hit 
      Print Chinese first refer to the Fnthex32.dll DLL control general installation method (for reference only): The following is the 32 system's registration BAT file to save the following code as "register. bat" and put it into the DLL directory, the DLL registration is automatically completed (Win98 not supported). @echo Start registering copy Fnthex32.dll%windir%\system32\ regsvr32%windir%\system32\fnthex32.dll/s @echo Fnthex32.dll registered successfully @pause the following is the registration bat file for the 64 system @echo start registering copy Fnthex32.dll%windir%\syswow64\ regs Vr32%windir%\syswow64\fnthex32.dll/s @echo Fnthex32.dll registered successfully @pause * * zebrahelper en = new
      Zebrahelper ();
      StringBuilder builder = new StringBuilder (); Builder. Appendline (en.
      Zpl_start ()); Builder. Appendline (en.
      Zpl_pageset (40, 80)); BuildEr. Appendline (en.
      Zpl_drawchtext ("Upper good if the water heavy", "Song Body", 40, 40, 0, 32, 0, 1, 0)); Builder. Appendline (en.
      Zpl_drawbarcode (40, 150, 3, 2, 40, "111112222233333")); Builder. Appendline (en.
      Zpl_drawentext ("111112222233333", "A", 205, "N", 30, 50)); Builder. Appendline (en.
      Zpl_drawrectangle (20,20,2,700,700)); Builder. Appendline (en.
      Zpl_end ()); String a = Builder.
      ToString (); Print en.
     Cmddos ("C:\\c.txt", a);
      */public string Zpl_start () {StringBuilder builder = new StringBuilder (); Builder. Appendline ("^xa"); The beginning of the instruction block builder. Appendline ("^md30"); MD is the depth return builder that sets color bands.
    ToString ();
      public string Zpl_end () {StringBuilder builder = new StringBuilder (); Builder. Appendline ("^xz"); The end of the instruction block return builder.
    ToString (); ///<summary>///Set Print label paper margins///</summary>///<param name= "printx" > label paper margin x coordinates </param >///<param name= "printy" > label paper margin y-coordinate </param>///<returns></returns> public string zpl_pageset (int printx, int printy) {STR
      Ingbuilder builder = new StringBuilder (); Builder. Appendline ("^LH" + Printx + "," + printy); Defines a barcode paper margin return builder.
    ToString ();
    ///<summary>///printing by bar set///</summary>///<param name= "width" > Stripe width </param> <param name= "height" > Height </param>///<returns> return ZPL command </returns> public string zpl_se
      Tlabel (int width, int height) {//zpl Barcode set command: ^pw640^ll480 string sreturn = "^pw{0}^ll{1}"; return string.
    Format (Sreturn, width, height);  ///<summary>///Print rectangle///</summary>///<param name= "px" > Start x Coordinate </param>/// <param name= "py" > Start y coordinates </param>///<param name= "thickness" > Border width </param>///<param nam E= "width" > Rectangle width, 0 for printing a vertical line </param>///<param name= "height"; rectangle height, 0 means print a horizontal line </param>///<returns> return ZPL command </returns> public string zpl_drawrectangle (int px, I NT py, int thickness, int width, int height) {//zpl rectangle command: ^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>///Print English///</summary>///<param name= "Entext" > to be printed text </param> <param name= "Zebrafont" > Printer font a-z</param>///<param name= "px" > start x coordinates </param>///< param name= "py" > Start y coordinates </param>///<param name= "Orient" > Rotation angle n = normal,r = rotated-degrees (clockwise ), I = inverted 180 degrees,b = read from bottom up, 270 degrees</param>///<param-name= "height" > Font height </ param>///<param name= "width" > Font width </param>///<returns> return ZPL command </returns> public s Tring Zpl_drawentext (String entext,string Zebrafont, int px, int py, string Orient, int Height, int Width) {//zpl Print english command: ^fo50,50^a0n,32,25^fdzebra^fs str
      ing Sreturn = "^fo{1},{2}^a" + Zebrafont + "{3},{4},{5}^FD{0}^FS"; return string.
    Format (Sreturn, Entext, px, py, Orient, Height, Width); ///<summary>///Chinese processing, returning ZPL command///</summary>///<param name= "Chinesetext" > Pending change Chinese content
    ;/param>///<param name= "fontname" > Font name </param>///<param name= "startx" >x coordinates </param>
    <param name= "Starty" >y coordinates </param>///<param name= "Orient" > Rotation angle 0,90,180,270</param> <param name= ' height ' > font height </param>///<param name= ' width ' > font width, usually 0</param>///<pa Ram Name= "IsBold" >1 thicker, 0 normal </param>///<param name= "Isitalic" >1 italic, 0 normal </param>///<retu Rns></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>///Print barcode (128 yards)///</summary>///<param name= "px" > start x coordinates </param> <param name= "py" > Start y coordinates </param>///<param name= "width" > base unit width 1-10</param>/// ;p Aram Name= "ratio" > Width ratio 2.0-3.0 increment 0.1</param>///<param name= "barheight" > Barcode height </param>///&L T;param name= "Barcode" > Barcode content </param>///<returns> return ZPL command </retUrns> public string Zpl_drawbarcode (int px, int py, int width, int ratio, int barheight, string barcode) { ZPL Print English command: ^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>///Chinese processing///</summary>///<param name= "chinesetext" > Change Chinese content </param&
    Gt
    <param name= "FontName" > Font name </param>///<param name= "Orient" > Rotation angle 0,90,180,270</param> <param name= ' height ' > font height </param>///<param name= ' width ' > font width, usually 0</param>///<pa Ram Name= "IsBold" >1 thicker, 0 normal </param>///<param name= "Isitalic" >1 italic, 0 normal </param>///<para
    M name= "Returnpicdata" > returned picture characters </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>///Chinese processing///</summary>///<param name= "Chinesetext" > to be changed Chinese content </param>  <param name= "FontName" > Font name </param>///<param name= "FileName" > returned picture Word rename characters life </param>/// <param name= "Orient" > Rotation angle 0,90,180,270</param>///<param name= "height" > Font height </param>///& Lt;param name= "width" > font width, usually 0</param>///<param name= "IsBold" >1 thickened, 0 normal </param>///<pa Ram Name= "Isitalic" >1 italic, 0 normal </param>///<param name= "Returnpicdata" > returned picture characters </param>/// ;returns></returns> [DllImport ("Fnthex32.dll")] public static extern int Getfonthex (Strin G Chinesetext, String fontname, String FileName, inT Orient, int Height, int Width, int isbold, int isitalic,
    StringBuilder returnpicdata); #region Extended///<summary>///millimeter-pixel rounding///</summary>///<param name= "mm" > mm &LT;/PARAM&G
    T <param name= "dpi" > Print dpi such as 300</param>///<returns></returns> public double mm2px (DOUBL
      e mm, double dpi) {double px = (mm/25.4) * DPI;
    Return Math.Round (px, 0, Midpointrounding.awayfromzero); ///<summary>///pixel to millimeter rounding///</summary>///<param name= "px" > Pixel </param>// /<param name= "dpi" > Print dpi such as 300</param>///<returns></returns> public double px2mm (double
      px, double dpi) {//Pixel conversion to millimeter formula: (Width pixel/level dpi) *25.4;
      Double mm = (px/dpi) * 25.4;
    return Math.Round (mm, 0, Midpointrounding.awayfromzero);
///<summary>///generates ZPL commands and executes    </summary>///<param name= "path" &GT;ZPL file path </param>///<param name= "zpl" &GT;ZPL command </  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 encoding format if (file.exists (path)) {SW.
        Write (ZPL); Sw.
        Flush (); Sw.
      Close (); } fs.
      Close ();
    Runcmd ("print/d:com1" + Path + "");
    ///<summary>///Run dos command///</summary>///<param name= "command" ></param> <returns></returns> private bool Runcmd (String command) {//instance a process class that initiates an independent process Proc
      ESS p = new Process ();
      The process class has a StartInfo property, this is the ProcessStartInfo class, includes some properties and methods, we use several of his attributes: p.StartInfo.FileName = "cmd.exe";/Set Program name P.startinfo.arguments = "/C" + command;//Set Program execution parameters P.startinfo.useshellexecute = FAlse;//closes the shell using P.startinfo.redirectstandardinput = true;//REDIRECT Standard input p.startinfo.redirectstandardoutput = true;
      REDIRECT standard output P.startinfo.redirectstandarderror = true;//redirect Error output P.startinfo.createnowindow = true;//settings do not display window P.standardinput.writeline (command); You can also enter the commands to be executed in this way//p.standardinput.writeline ("exit");
      But remember to add exit or else the next line of code will execute in the machine try {p.start ();//START process return true;
      The catch {} finally {if (P!= null) p.close ();
    return false;

 } #endregion}}

Read more about C # Interested readers can view the site topics: "C # Programming Thread Usage Tips summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, summary of C # array manipulation techniques, and an introductory course on C # object-oriented programming

I hope this article will help you with C # programming.

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.