调用方式:
PrintHelper.PrintMothed("{'ProductId':'11','BarCode':'','MachineNo':'3','ProductName':'测试hi第','ProductWeight':'11'}", 1, 0);
public class PrintHelper
{
public static DotNetBarcode BarCode=new DotNetBarcode();
/// <summary>
/// 开始打印
/// </summary>
/// <param name="number"></param>
/// <param name="plContent"></param>
public static void PrintMothed(string json,int number, int plContent)
{
BarCode.Type = DotNetBarcode.Types.QRCode;
int printNum = 1;
PrintPreviewDialog prv = new PrintPreviewDialog();
PrintDocument printDocuments = new PrintDocument();
prv.Document = printDocuments;
int printCopyCount = number;
int indexPrintCopuCount = 1;
int num = 0;
int TwoNum = 2;//代表一式两份
printDocuments.PrintPage += delegate (object sender, PrintPageEventArgs e)
{
if (!string.IsNullOrEmpty(json))
{
string code = "";
string name = "";
string spec = "";
string specweight = "";
string weight = "";
string Mlength = "";
string oepr = "";
string store = "";
string group = "";
string remark1 = "";
string barcode = "";
string LSH = "";
string machine = "";
string nweight = "";
PrintModel model = json.ToObject<PrintModel>();
code = model.ProductId;
name = model.ProductName;
spec =model.ProductSpec;
if ((string.IsNullOrWhiteSpace(model.BarCode)))
{
barcode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
}
else
{
barcode = model.BarCode;
}
specweight = model.ProductSpec;
weight = model.ProductWeight.ToString();
remark1 = model.Remark1;
string machineno = model.MachineNo;
nweight = weight;// Convert.ToDecimal(txtNWeight.Text.Trim()).ToString("0.0");
if (IniFile.Instance.GetString("systemSet", "ck_labeltest", "1") == "1")
{
int x = 2, y = 20, xWidth = x + 390, yHeight = y + 140, qrW = 130, qrh = 140;
int IdOneX = x + 108; int NameOneX = x + 178; int RemarkOneX = x + 220;
int IdTwoX = x + 238; int NameTwoX = x + 298; int RemarkTwoX = x + 340;
int OneHeight = y + 20;
int TwoHeight = y + 45;
int ThreeHeight = y + 70;
int FourHeight = y + 100;
int FiveHeight = y + 225;
int SixHeight = y + 171;
int SevenHeight = y + 250;
Graphics g = e.Graphics;
Font f = new System.Drawing.Font("微软雅黑", 17, FontStyle.Bold);
Font f2 = new Font("宋体", 11, FontStyle.Bold);
Font f4 = new Font("宋体", 10, FontStyle.Bold);
Font f3 = new Font("宋体", 12, FontStyle.Bold);
Brush b = new SolidBrush(Color.Black);
string company = IniFile.Instance.GetString("systemSet", "PrintTitle", " *应讯智能科技*");
string phone = IniFile.Instance.GetString("systemSet", "Printphone", "0371-63833272");
string address = IniFile.Instance.GetString("systemSet", "Printaddress", "郑州金水区文化路丰产路21号");
string time = DateTime.Now.ToString("yyyy-MM-dd");
g.DrawString(company, f2, b, new PointF(x + 10, y - 15));
Pen pline = new Pen(Color.Black);
//g.DrawLine(pline, x, y + 5, xWidth, y + 5);
string content = code + "|" + barcode + "|" + name + "|" + spec + "|" + specweight + "|" + weight + "|" + Mlength + "|" + store + "|" + group + "|" + time + "|" + oepr + "|" + nweight + "|" + machineno;
int conlength = 79;
if (content.Length < conlength)
{
for (int a = 0; a < conlength - content.Length; a++)
{
content = content + " ";
}
}
TwoNum--;//一式两份
if (TwoNum < 0)
{
TwoNum = 1;
}
BarCode.WriteBar(content, x + 3, y + 5, qrW, qrh, e.Graphics); // string content = lingjianCode + ";" + lingjianName + ";" + gongwei + ";" + huojiaName + ";" + baorongshu + ";" + gongweiCode + ";" + wuliaodingwei;
g.DrawString(name, f4, b, new PointF(IdOneX, OneHeight));
g.DrawString(code + "|" + machine, f4, b, new PointF(IdOneX, TwoHeight));
g.DrawString(weight + "KG", f4, b, new PointF(IdOneX, ThreeHeight));
g.DrawString("电话:" + phone, f4, b, new PointF(x + 10, FourHeight));
}
if (printCopyCount == indexPrintCopuCount)
{
indexPrintCopuCount = 1;
}
else
{
indexPrintCopuCount++;
}
num++;
if (num < number)
{
e.HasMorePages = true;
return;
}
else
{
e.HasMorePages = false;
}
}
};
prv.StartPosition = FormStartPosition.CenterParent;
PrinterSettings set = new PrinterSettings();
set.PrinterName = IniFile.Instance.GetString("systemSet", "PrintKey", "L60");
printDocuments.PrinterSettings = set;
StandardPrintController spc = new StandardPrintController();
printDocuments.PrintController = spc;
printDocuments.Print();
}
}