using LabelManager2; using Seagull.BarTender.Print; using System; using System.Drawing.Printing; using System.Windows.Forms; namespace MaterialPrint { public partial class Form1 : Form { Engine engine = new Engine(true); LabelFormatDocument format = null; public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; } private void Print_Click(object sender, EventArgs e) { try { //打印的最后一个流水 int LastNum = (int.Parse(Rn.Text) + int.Parse(Num.Text)); //打印的总数 int PrintNum = int.Parse(Num.Text); 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++) { switch (format.SubStrings[j].Name) { case "RN1": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp).ToString())); break; case "RN2": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 1).ToString())); break; case "RN3": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 2).ToString())); break; case "RN4": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 3).ToString())); break; case "RN5": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 4).ToString())); break; case "RN6": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 5).ToString())); break; case "RN7": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 6).ToString())); break; case "RN8": format.SubStrings[j].Value = (Prefix.Text + Date.Value.ToString("yyMMdd") + lpad(4, (temp + 7).ToString())); break; default: break; } } format.PrintSetup.PrinterName = PrinterList.Text; format.PrintSetup.IdenticalCopiesOfLabel = 1; format.Print(); //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(); format = engine.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.btw"); //doc = lbl.Documents.Open(System.Windows.Forms.Application.StartupPath + @"\SN.btw"); 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(); } } } }