C# 打印程序调用模板打印 扫描二维码没有值
在程序中一句一句调试,都有值,模板也调用成功,可是打印出来的目标没有值,这是为什么?打印预览没有东西出来。
private void btnPrint_Click(object sender, EventArgs e)
{
#region 打印
if (!string.IsNullOrEmpty(txtWLBM.Text.ToString().Trim())
&& !string.IsNullOrEmpty(txtDyfs.Text.ToString().Trim()) && !string.IsNullOrEmpty(txtZsmf.Text.ToString().Trim()))
{
lblWarm.Text = "正在调用打印模版";
int lengh = Convert.ToInt32(txtZsmf.Text.ToString().Trim());
lblMessage.Text = lengh.ToString();
string err = "";
DataTable dtb = new DataTable();
if (!string.IsNullOrEmpty(txtQstm.Text.ToString().Trim()))
{
String wlbm = txtWLBM.Text.ToString().Trim();
String qstm = txtQstm.Text.ToString().Trim();
if (wlbm.Substring(0, wlbm.Length - 1) == qstm.Substring(0, qstm.Length - 5))
{
dtb.Columns.Add("ljg", typeof(string));
dtb.Columns.Add("DYFS", typeof(string));
//dtb.Columns.Add("ZSMF", typeof(string));
}
for (int i = 1; i<= lengh; i++)
{
qstm = (long.Parse(qstm)+ 1).ToString();
//在范围内则打印
DataRow dr = dtb.NewRow();
dr["ljg"] = qstm;
dr["DYFS"] = txtDyfs.Text.ToString().Trim();
//dr["ZSMF"] = txtZsmf.Text.ToString().Trim();
dtb.Rows.Add(dr);
}
}
else {
dtb.Columns.Add("ljg", typeof(string));
dtb.Columns.Add("DYFS", typeof(string));
//dtb.Columns.Add("ZSMF", typeof(string));
for (int i = 1; i <=lengh; i++)
{
string code = "";//存放当前序号
int len = (i).ToString().Length;//序号的长度
if (len < 4)
{
for (int l = 1; l <= 4 - len; l++)
{
code = code + "0";//当前序号的零
}
}
code = code + (i).ToString();//当前序号
//在范围内则打印
DataRow dr = dtb.NewRow();
dr["ljg"] = txtWLBM.Text.ToString().Trim() + code;//物料编码+4位流水号
dr["DYFS"] = txtDyfs.Text.ToString().Trim();
//dr["ZSMF"] = txtZsmf.Text.ToString().Trim();
dtb.Rows.Add(dr);
}
lblTitle.Text = dtb.ToString();
}
string MACmodel = @"LJG_QRcode.lab";//模板
Application.DoEvents();
Printer Macpts = new Printer();
Macpts.PrintMAC(dtb, "", "", "", Convert.ToInt32(txtDyfs.Text.ToString().Trim()), MACmodel, out err);
lblWarm.Text = err;
}
else
{
lblWarm.Text = " 打印失败!";
lblWarm.ForeColor = Color.Red;
}
#endregion
}
public bool PrintMAC(DataTable dtb, string barcode, string Prefix, string SerNum, int ptnum, string model, out string error)
{
Kill("lppa");
model = AppDomain.CurrentDomain.BaseDirectory + model;//取当前应用程序域运行的目录
LabelManager2.ApplicationClass PrintApp = new LabelManager2.ApplicationClass();
try
{
LabelManager2.Document PrintDoc = PrintApp.ActiveDocument;
PrintDoc = PrintApp.Documents.Open(model, false);
if (PrintDoc == null)
{
error = "模版未找到";
return false;
}
if (!string.IsNullOrEmpty(barcode))
{
PrintDoc.Variables.Counters.Item(barcode).Prefix = Prefix;
PrintDoc.Variables.Counters.Item(barcode).Value = SerNum;
}
for (int j = 0; j < dtb.Rows.Count; j++) //行
{
for (int i = 0; i < dtb.Columns.Count; i++) //列
{
try
{
string col = dtb.Columns[i].ColumnName.ToString();
string txt = dtb.Rows[j][col].ToString();
PrintDoc.Variables.Item("ljg").Value = txt;//给模板的变量传值
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
//打印预览
PrintPreviewDialog dialog = new PrintPreviewDialog();
dialog.Document = this.PrintDoc;
dialog.ShowDialog();
PrintDoc.PrintDocument(1);
}
error = "打印成功";
return true;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
finally
{
PrintApp.Documents.CloseAll(false);
PrintApp.Quit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
把代码贴出来先。
可以先打印预览是否正常