C# 怎样实现条码标签的批量打印

fsh1985 2011-07-13 04:56:12
现在的情况是可以打印一个条码标签

调用,PrintDocument.print
如果我要批量实现条码标签的打印,应该怎么写?
代码如下:

private void button1_Click(object sender, EventArgs e) //按钮事件
{

strCon = "server=192.168.1.57;uid=sa;pwd=123;database=LonkingJiXie";
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = strCon;
sqlCon.Open();
SqlCommand sqlCom = new SqlCommand("select * from TestTM", sqlCon);

SqlDataReader sqlDR = sqlCom.ExecuteReader();


bool ifcom = sqlDR.Read();
if (ifcom)
{
WLcode = sqlDR[0].ToString();
WLname = sqlDR[1].ToString();
TMcode = sqlDR[2].ToString();
}

PrintPreviewDialog ppwDlg = new PrintPreviewDialog();

PrintDocument prtDoc = new PrintDocument();

ppwDlg.Document = prtDoc;

prtDoc.PrintPage += new PrintPageEventHandler(prtDoc_PrintPage);

if (ppwDlg.ShowDialog() != DialogResult.OK)
{
prtDoc.PrintPage -= new PrintPageEventHandler(prtDoc_PrintPage);
return;
}


}

----------------------document.pintpage事件


//printdocument.printpage 事件

Graphics g = e.Graphics;

e.Graphics.Clip = new Region(new Rectangle(10, 2, 800, 200));

g.PageUnit = GraphicsUnit.Millimeter;

Font drawFont = new Font("宋体", 14);
Font drawLGFont = new Font("方正艺黑简体", 8);
SolidBrush drawBrush = new SolidBrush(Color.Black);

g.DrawString("Lonking 龙工", drawLGFont, drawBrush, 10, 5);
SizeF FZ = g.MeasureString("Lonking 龙工", drawFont, e.MarginBounds.Width);

g.DrawString(WLname + ":", drawFont, drawBrush, 10, 5 + FZ.Height);
SizeF dd = g.MeasureString(WLname + ":", drawFont, e.MarginBounds.Width);

g.DrawString(WLcode, drawFont, drawBrush, 10 + dd.Width, 5 + dd.Height);
Pen penLine = new Pen(new SolidBrush(Color.Black), 0.2f);

//g.DrawLine(penLine, 10, 10, 190, 10);
//g.DrawLine(penLine, new PointF(20.0f, Convert.ToSingle(20.0f + dd.Height)), new PointF(Convert.ToSingle(20.0f + 2 * dd.Width), Convert.ToSingle(20.0f + dd.Height)));
//g.DrawRectangle(penLine, 10,10, 190, 50); //左,上,右,下 坐标,高、宽


Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 10);

System.Drawing.Image _CodeImage = _Code39.GetCodeImage(TMcode, Code39.Code39Model.Code39Normal, true);

g.DrawImage(_CodeImage, 5, 10 + FZ.Height);

...全文
974 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjp1234airen4385 2011-07-15
  • 打赏
  • 举报
回复
----------------------document.pintpage事件


//printdocument.printpage 事件

Graphics g = e.Graphics;

e.Graphics.Clip = new Region(new Rectangle(10, 2, 800, 200));

g.PageUnit = GraphicsUnit.Millimeter;

Font drawFont = new Font("宋体", 14);
Font drawLGFont = new Font("方正艺黑简体", 8);
SolidBrush drawBrush = new SolidBrush(Color.Black);

g.DrawString("Lonking 龙工", drawLGFont, drawBrush, 10, 5);
SizeF FZ = g.MeasureString("Lonking 龙工", drawFont, e.MarginBounds.Width);

g.DrawString(WLname + ":", drawFont, drawBrush, 10, 5 + FZ.Height);
SizeF dd = g.MeasureString(WLname + ":", drawFont, e.MarginBounds.Width);

g.DrawString(WLcode, drawFont, drawBrush, 10 + dd.Width, 5 + dd.Height);
Pen penLine = new Pen(new SolidBrush(Color.Black), 0.2f);

//g.DrawLine(penLine, 10, 10, 190, 10);
//g.DrawLine(penLine, new PointF(20.0f, Convert.ToSingle(20.0f + dd.Height)), new PointF(Convert.ToSingle(20.0f + 2 * dd.Width), Convert.ToSingle(20.0f + dd.Height)));
//g.DrawRectangle(penLine, 10,10, 190, 50); //左,上,右,下 坐标,高、宽
int i = 1;
while(sqlDR.read())
{
Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 10);

System.Drawing.Image _CodeImage = _Code39.GetCodeImage(TMcode, Code39.Code39Model.Code39Normal, true);

g.DrawImage(_CodeImage, 5, 10 + 60 * i);
i++;
}

fsh1985 2011-07-15
  • 打赏
  • 举报
回复
#5 兄弟,我调试出来了,谢了
我因为写代码不多,以为我那代码就是循环了,所以开始一直调不出来
mjp1234airen4385 2011-07-14
  • 打赏
  • 举报
回复
把打印的那段代码加到:
while(sqlDR.read())
{
//打印代码,注意要调整每一个code39标签的位置。
}
fsh1985 2011-07-14
  • 打赏
  • 举报
回复
怎样直接话用报表,有案例不?
fsh1985 2011-07-14
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mjp1234airen4385 的回复:]
把打印的那段代码加到:
while(sqlDR.read())
{
//打印代码,注意要调整每一个code39标签的位置。
}
[/Quote]

这样写没反应,打印预览时,还是只有一个标签


bool ifcom = sqlDR.Read();
if (ifcom)
{
TMcode = sqlDR[2].ToString();

Code39 _Code39 = new Code39();
_Code39.Height = 60;
_Code39.Magnify = 1;
_Code39.ViewFont = new Font("宋体", 20);
//123ABC4567890FWF
System.Drawing.Image _CodeImage = _Code39.GetCodeImage(TMcode, Code39.Code39Model.Code39Normal, false);
//pictureBox1.Image = _CodeImage;
System.IO.MemoryStream _Stream = new System.IO.MemoryStream();
_CodeImage.Save(_Stream, System.Drawing.Imaging.ImageFormat.Jpeg);

StartPrint(_Stream, "image"); //调用打印图片

}


sqlCom.Dispose();
sqlCon.Close();
flyerwing 2011-07-13
  • 打赏
  • 举报
回复
直接画,用报表不行吗?

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧