C# 標簽(條碼)的打印與設計(一),
相信目前稍有規模的公司已進入或正在進入信息化之中,尤其在制造企業中,少不了一個條碼的打印功能,而這類應用大多是使用斑馬打印機,所以就會遇到了怎麼打印的問題了。本人也已經從事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
大家看一下代碼,是否非常的簡單呢? 這裡主要是提供幾個打印信息便可(打印語言,打印機。還有就是標簽定義文檔/字庫路徑),根本不太需要了解斑馬的語言就可以輕易打印出來想要的條碼。那麼問題來了,到底如何設計這個模板呢? 敬請留意下一篇文章,也是相當的容易的。做為程序開發人員,肯定要想辦法減輕工作量,以獲得足夠多的業余時間做自己喜歡做的事。