C#.NET 打印斑马标签

 

原理:
1、用codesoft软件制作ZPL模板文件。
2、用.net程序替换模板文件里面的变量。
3、将新形成的zpl文件传送给打印机

程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;

namespace print
{
    public partial class printmpq : Form
    {
        SqlConnection sqlconnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["websqlConnectionString"].ConnectionString);
        private string stringToPrint;

        public printmpq()
        {
            InitializeComponent();
        }
        private void printmpq_Load(object sender, EventArgs e)
        {
            textBox_item.Focus();
        }
        private void textBox_item_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13 || e.KeyValue.ToString() == Keys.Tab.ToString())
            {
                textBox_item.Text = textBox_item.Text.ToString().Trim().ToUpper();

                sqlconnection.Open();
                DataSet printds = new DataSet();
                String sqldata = "select * from supermarket_config where status = ‘Active’ and item = ‘" + textBox_item.Text.ToString().Trim().ToUpper() + "’";
                SqlDataAdapter printad = new SqlDataAdapter(sqldata, sqlconnection);
                printad.Fill(printds, "print");

                if (printds.Tables["print"].Rows.Count > 0)
                {
                    textBox_mpq.Text = printds.Tables["print"].Rows[0]["mpq"].ToString();
                }
                else
                {
                    MessageBox.Show("Supermarket尚未设置此料号的!");
                    textBox_item.Text = "";
                    textBox_item.Focus();
                    sqlconnection.Close();
                    return;
                }

                sqlconnection.Close();
                textBox_mpq.Focus();
          }
        }

        private void button_print_Click(object sender, EventArgs e)
        {

            if (textBox_item.Text.Trim() == "" || textBox_mpq.Text.Trim() == "" || textBox_mpq.Text.ToString().Trim() == "0")
            {
                MessageBox.Show("No Item Or No MPQ");
                sqlconnection.Close();
                return;
            }
            StreamReader sr = File.OpenText(@"d:supermarketprint-mpq.zpl");
            StringBuilder output = new StringBuilder();
            String rl = "";

            while ((rl = sr.ReadLine()) != null)
            {
                rl = rl.Replace("supermarket_item", textBox_item.Text.ToString().Trim().ToUpper());
                rl = rl.Replace("supermarket_qty",textBox_mpq.Text.ToString().Trim());
                rl = rl.Replace("supermarket_datecode", textBox_datecode.Text.ToString().Trim());
                output.Append(rl + System.Environment.NewLine);
            }
            String zplname = DateTime.Now.ToString("MMddyyyy") + DateTime.Now.ToString("hh-mm-ss") + Guid.NewGuid().ToString();
            //MessageBox.Show(@"d:supermarketzpl" + zplname + "Print-End.zpl");
            StreamWriter sw = File.CreateText(@"d:supermarketzpl" + zplname + "Print-mpq.zpl");
            sw.WriteLine(output.ToString());
            sw.Close();
            sr.Close();

            printDocument1.DocumentName = @"d:supermarketzpl" + zplname + "Print-mpq.zpl";
            Str
eamReader srprint = File.OpenText(@"d:supermarketzpl" + zplname + "Print-mpq.zpl");

            stringToPrint = srprint.ReadToEnd();

            printDocument1.Print();
            textBox_item.Text = "";
            textBox_mpq.Text = "";
            textBox_datecode.Text = "";
            textBox_item.Focus();
        }
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            int charactersOnPage = 0;
            int linesPerPage = 0;
            e.Graphics.MeasureString(stringToPrint, this.Font,
                e.MarginBounds.Size, StringFormat.GenericTypographic,
                out charactersOnPage, out linesPerPage);
            e.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,
                e.MarginBounds, StringFormat.GenericTypographic);
            stringToPrint = stringToPrint.Substring(charactersOnPage);
            e.HasMorePages = (stringToPrint.Length > 0);
        }

        private void linkLabel_web_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://shant128/supermarket/print_mpq.aspx");
        }

        private void textBox_mpq_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                textBox_datecode.Focus();
                //button_print.Focus();
            }
        }

        private void textBox_datecode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                button_print.Focus();
            }

        }
 
    }
}