using LabelManager2; using System; using System.Drawing.Printing; using System.Windows.Forms; namespace MaterialPrint { public partial class Form2 : Form { ApplicationClass lbl; Document doc; DataHelper dh = new DataHelper(); public Form2() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } private void Print_Click(object sender, EventArgs e) { try { //根据箱数和箱内容量计算打印数 Num.Text = (int.Parse(OutBoxNum.Text) * int.Parse(InnerBoxNum.Text)).ToString(); //外箱流水 int OutBoxSerial = int.Parse(OutBoxCode.Text.Substring(OutBoxCode.Text.Length - 6, 6)); //外箱前缀 string OutBoxPrefix = OutBoxCode.Text.Substring(0, 8); //内箱数量 int InnerBox = int.Parse(InnerBoxNum.Text); //打印的最后一个流水 int LastNum = (int.Parse(Rn.Text) + int.Parse(Num.Text)); //打印的总数 int PrintNum = int.Parse(Num.Text); //当前箱的打印数量 int BoxCount = 0; for (int i = int.Parse(Rn.Text); i < LastNum; i = i + 8) { int temp = i; for (int j = 0; j < ((LastNum - i) / 8 > 0 ? 8 : (LastNum - i)); j++) { BoxCount = BoxCount + 1; string outboxbarcode = OutBoxPrefix + OutBoxSerial; switch (doc.Variables.FormVariables.Item(j + 1).Name) { case "RN1": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN2": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 1).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN3": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 2).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN4": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 3).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN5": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 4).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN6": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 5).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN7": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 6).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; case "RN8": doc.Variables.FormVariables.Item(j + 1).Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 7).ToString())); dh.ExecuteSql("insert into baroutboxbind(bob_id,bob_outboxcode,bob_barcode,bob_indate) values(baroutboxbind_seq.nextval,'" + outboxbarcode + "','" + doc.Variables.FormVariables.Item(j + 1).Value + "',sysdate)", "insert"); break; default: break; } if (BoxCount % InnerBox == 0) { BoxCount = 0; OutBoxSerial = OutBoxSerial + 1; } } //doc.Printer.SwitchTo(PrinterList.Text); //doc.PrintDocument(1); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private static string lpad(int length, string number) { while (number.Length < length) { number = "0" + number; } number = number.Substring(number.Length - length, length); return number; } private void Form1_Load(object sender, EventArgs e) { Console.WriteLine("4181231".Substring(0, 3)); return; try { lbl = new ApplicationClass(); doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.lab"); PrintDocument print = new PrintDocument(); string sDefault = print.PrinterSettings.PrinterName;//默认打印机名 foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称 { PrinterList.Items.Add(sPrint); if (sPrint == sDefault) PrinterList.SelectedIndex = PrinterList.Items.IndexOf(sPrint); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { System.Diagnostics.Process[] pro = System.Diagnostics.Process.GetProcessesByName("lppa"); for (int i = 0; i < pro.Length; i++) { pro[i].Kill(); } } } }