【Wonder原创】栈板标签列印
一、画面效果如下:
二、打印效果
三、后台代码:
1、全局变量
public static int _Cur_index; //当前页码
public static string[] _BoardList; //需要打印的栈板号数组
2、自定义方法
//重置全局变量
private void ClearVar()
{
_Cur_index = 0;
_BoardList = null;
}
核心打印方法:
核心打印方法
private void BarcodePrint(PrintPageEventArgs e, DataTable dt)
{
string CodeNo="", PN="", qty="0", Sdate="",Bindex="";
foreach (DataRow dr in dt.Rows)
{
CodeNo = dr["CodeNo"].ToString();
PN = dr["PN"].ToString();
Sdate = dr["DDate"].ToString();
Bindex = dr["bindex"].ToString();
qty = (Convert.ToInt32(qty) + Convert.ToInt32(dr["qty"].ToString())).ToString();
}
Graphics g = e.Graphics;
Font fnt = new Font("Arial", 22);
float xWidth = 1000F, yWidth = 700F;
float xStart = 60F, yStart = 60F;
float y = yStart;
float space = 25F;
#region 文本
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + xWidth, y);//line (0,0) 横线
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart, y + yWidth);//line (0,0) 竖线
g.DrawLine(System.Drawing.Pens.Black, xStart + 230, y, xStart + 230, y + yWidth);//line (1,0) 竖线
g.DrawLine(System.Drawing.Pens.Black, xStart + xWidth, y, xStart + xWidth, y + yWidth);//line (3,0) 竖线
y += space;
y += fnt.Height / 2;
g.DrawString("Car Number", fnt, System.Drawing.Brushes.Black, xStart, y);//"Car Number"
y += space;
y += fnt.Height + fnt.Height / 2;
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + xWidth, y);//line (0,1) 横线
g.DrawLine(System.Drawing.Pens.Black, xStart + 500, y, xStart + 500, yWidth + yStart);//line (2,1) 竖线
y += space;
g.DrawString("Company", fnt, System.Drawing.Brushes.Black, xStart, y);//"Company"
g.DrawString("Xxxx Xxxxxxxxx", fnt, System.Drawing.Brushes.Black, xStart + 250, y);//"Xxxx Xxxxxxxxx"
y += fnt.Height;
g.DrawString("Name", fnt, System.Drawing.Brushes.Black, xStart, y);//"Name"
g.DrawString("Corp.", fnt, System.Drawing.Brushes.Black, xStart + 320, y);//"Corp."
y += fnt.Height;
y += space;
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + 500, y);//line (0,2) 横线
y += space;
y += fnt.Height / 2;
g.DrawString("Material Code", fnt, System.Drawing.Brushes.Black, xStart, y);//"Material Code"
g.DrawString(CodeNo, fnt, System.Drawing.Brushes.Black, xStart + 230, y);//CodeNo:"LJ01-07716A"
y += fnt.Height + fnt.Height / 2;
y += space;
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + 500, y);//line (0,3) 横线
y += space;
y += fnt.Height / 2;
g.DrawString("Recipe", fnt, System.Drawing.Brushes.Black, xStart, y);//"Recipe"
g.DrawString(PN, fnt, System.Drawing.Brushes.Black, xStart + 230, y);//PN:"PL-15-324"
y += fnt.Height + fnt.Height / 2;
y += space;
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + 500, y);//line (0,4) 横线
y += space;
y += fnt.Height / 2;
g.DrawString("Q'ty", fnt, System.Drawing.Brushes.Black, xStart, y);//"Q'ty"
g.DrawString(qty, fnt, System.Drawing.Brushes.Black, xStart + 230, y);//QTY:"9000"
y += fnt.Height + fnt.Height / 2;
y += space;
g.DrawLine(System.Drawing.Pens.Black, xStart, y, xStart + 500, y);//line (0,5) 横线
y += space;
y += fnt.Height / 2;
g.DrawString("Production Date", fnt, System.Drawing.Brushes.Black, xStart, y);//"Production Date"
g.DrawString(Sdate, fnt, System.Drawing.Brushes.Black, xStart + 230, y);//Date:"2010-08-27"
y += fnt.Height + fnt.Height / 2;
g.DrawLine(System.Drawing.Pens.Black, xStart, yStart + yWidth, xStart + xWidth, yStart + yWidth);//line (0,6) 横线
g.DrawString(Bindex, new Font("Arial", 10), System.Drawing.Brushes.Black, xWidth + xStart + 10, yStart + yWidth + 10);//Bindex
#endregion
#region Barcode
float x = xStart+ 500F+10F;
y = yStart + fnt.Height * 2 + space * 2 + 10F;
BarcodeLib.Barcode barcode = new BarcodeLib.Barcode();
barcode.IncludeLabel = true;
barcode.Height = 35;
barcode.Width = 200;
barcode.LabelFont = new Font("Arial", 12, FontStyle.Regular);
barcode.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
barcode.Alignment = BarcodeLib.AlignmentPositions.CENTER;
for (int i = 1; i <= 18 && i<=dt.Rows.Count; i++)
{
string barcodeStr = dt.Rows[i-1][0].ToString()+i.ToString("D3");
Image image = barcode.Encode(BarcodeLib.TYPE.CODE39Extended, barcodeStr);
g.DrawImage(image, x+240*((i-1)/(dt.Rows.Count/2)), y+((i-1)%(dt.Rows.Count/2))*65);//Barcode
}
#endregion
}
3、“查询”按钮事件,查询需要打印的数据
查询按钮
private void btnQuery_Click(object sender, EventArgs e)
{
if (drpCUS.SelectedIndex == -1 || drpDept.SelectedIndex == -1)
{
MessageBox.Show("请先选择客户及部门.");
return;
}
lblBSum.Text = "0";
lblQSum.Text = "0";
try
{
DataSet ds = CDataSet.GetSTList(drpCUS.SelectedItem.ToString(), drpDept.SelectedItem.ToString(), dDate.Value.ToShortDateString(), "%");
if (ds != null)
{
CDataSet.BindToCombox(drpSTNO, ds.Tables[0], "STNO");
if (drpSTNO.Items.Count > 0)
{
ds = CDataSet.GetSTList(drpCUS.SelectedItem.ToString(), drpDept.SelectedItem.ToString(), dDate.Value.ToShortDateString(), drpSTNO.SelectedItem.ToString());
gvData.DataSource = ds.Tables[0];
int i = 0;
double qty = 0;
for (; i < ds.Tables[0].Rows.Count; i++)
{
qty += Convert.ToDouble(ds.Tables[0].Rows[i]["数量"].ToString());
}
lblBSum.Text = i.ToString();
lblQSum.Text = qty.ToString();
}
else
{
gvData.DataSource = null;
drpSTNO.Text = "";
drpSTNO.Items.Clear();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
4、“列印全部”按钮事件
列印全部按钮事件
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
StringBuilder sbStr = new StringBuilder();
ClearVar();
foreach (DataGridViewRow gr in gvData.Rows)
{
sbStr.Append(gr.Cells[7].Value.ToString());
sbStr.Append(",");
}
_BoardList = sbStr.ToString().TrimEnd(',').Split(',');
if (_BoardList != null && _BoardList.Length > 0)
{
printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();//隐藏“正在打印。。。”小窗口
printDoc.DefaultPageSettings.Landscape = true;//横向打印
printDoc.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
5、PrintDocument 响应PrintPage事件
打印每一页都会触发这个事件,注意 HasMorePages 达到分页列印效果。
PrintPage事件
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
try
{
DataSet ds = CDataSet.GetBoard_SEC(_BoardList[_Cur_index].ToString());
if (ds != null && ds.Tables.Count > 0)
{
//BarcodePrint(e, "LJ01-07716A1", "PL-15-324", "9000", "2010-8-27");
BarcodePrint(e, ds.Tables[0]);
}
_Cur_index++;
if (_Cur_index < _BoardList.Length)
e.HasMorePages = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}