private void btnPrint_Click(object sender, EventArgs e) { LabelManager2.Application labelapp = null; LabelManager2.Document labDoc = null; string filePathName = System.Windows.Forms.Application.StartupPath + "\\Document1.lab"; if (!File.Exists(filePathName)) { MessageBox.Show("请准备好列印模板"); return; } try { labelapp = new LabelManager2.Application(); labelapp.Documents.Open(filePathName, false); //调用设计好的模板 labDoc = labelapp.ActiveDocument; labDoc.Variables.FormVariables.Item("Date").Value = System.DateTime.Now.ToString("yyyy-MM-dd"); //往变量里面写入数据 labDoc.PrintDocument(1); //列印份数 } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { labelapp.Documents.CloseAll(true); labelapp.Quit(); labelapp = null; labDoc = null; } }
/// <summary> /// 关闭打印对象的方法,因为是单件模式的对象,所以请最后再关闭 /// </summary> public static void closeApp() { try { if (app != null) { bcEngine = null; app.Quit(); } } catch { } }
private bool executePrintJob(JobFile job) { string strFile = Path.Combine(Properties.Settings.Default.LabFilesPath, job.LabelDocument); if (!System.IO.File.Exists(strFile)) { Log(".Lab file: " + strFile + " not found: ", EnumLogType.Error); return(false); } var lbl = new LabelManager2.Application(); lbl.Documents.Open(strFile, false); if (lbl.ActiveDocument == null) { Log("Could not get active document from Labfile ( CodeSoft Version? )", EnumLogType.Error); return(false); } var doc = lbl.ActiveDocument; //doc.Printer.SwitchTo(targetPrinterName, targetPrinterPort); //Get all the name of the printer Strings var allPrinterVars = lbl.PrinterSystem().Printers(enumKindOfPrinters.lppxAllPrinters); //To obtain the printer name printer can be fixed directly to the name value // string printName = allPrinterVars.Item(2); PrintDocument prtdoc = new PrintDocument(); //use default system printer //string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName; string usedprinter = ""; string strDefaultPrinter = Properties.Settings.Default.PrinterName; if (job.UseDefaultPrinter == false) { doc.Printer.SwitchTo(job.PrinterName); usedprinter = job.PrinterName; } else { usedprinter = strDefaultPrinter; } ////Gets the default printer name //bool foundPrinter = false; //for (int j = 0; j < allPrinterVars.Count; j++) //{ // string[] arryString = allPrinterVars.Item(j).Split(','); // if (!string.IsNullOrEmpty(job.PrinterName)) // { // if (arryString[0] == job.PrinterName) // { // foundPrinter = true; // doc.Printer.SwitchTo(job.PrinterName, arryString[1], true); // usedprinter = job.PrinterName; // break; // } // } // if (arryString[0] == strDefaultPrinter) // { // foundPrinter = true; // doc.Printer.SwitchTo(strDefaultPrinter, arryString[1], true); // usedprinter = strDefaultPrinter; // break; // } //} var ci = new CultureInfo("en-US"); Log("start processing document", EnumLogType.Debug); foreach (var vitem in job.Variables) { string beginswith = vitem.Name.Substring(0, vitem.Name.Length - 1); //when % is in variable name, overwrite all matching vars if (vitem.Name.EndsWith("%")) { var varcnt = doc.Variables.Count; for (int i = 1; i <= varcnt; i++) { var vx = doc.Variables.Item(i); if (vx != null) { if (vx.Name.StartsWith(beginswith, true, ci)) { vx.Value = vitem.Value; } } } } else { //try to find exact value var varInDoc = doc.Variables.Item(vitem.Name); if (varInDoc != null) { if (string.IsNullOrEmpty(vitem.Value) || vitem.Printable == false) { doc.Variables.Remove(vitem.Name); } else { doc.Variables.Item(vitem.Name).Value = vitem.Value; } } } //look for images if (vitem.Name.EndsWith("%")) { var varcnt = doc.DocObjects.Images.Count; for (int i = 1; i <= varcnt; i++) { var aimgInDoc = doc.DocObjects.Images.Item(i); if (aimgInDoc != null) { if (aimgInDoc.Name.StartsWith(beginswith, true, ci)) { if (vitem.Printable) { aimgInDoc.Printable = 1; } else { aimgInDoc.Printable = 0; } } } } } else { var imgInDoc = doc.DocObjects.Images.Item(vitem.Name); if (imgInDoc != null) { if (vitem.Printable) { imgInDoc.Printable = 1; } else { imgInDoc.Printable = 0; } } } //look for texts if (vitem.Name.EndsWith("%")) { var varcnt = doc.DocObjects.Texts.Count; for (int i = 1; i <= varcnt; i++) { var atextInDoc = doc.DocObjects.Texts.Item(i); if (atextInDoc != null) { if (atextInDoc.Name.StartsWith(beginswith, true, ci)) { if (vitem.Printable) { atextInDoc.Printable = 1; } else { atextInDoc.Printable = 0; } } } } } else { var textInDoc = doc.DocObjects.Texts.Item(vitem.Name); if (textInDoc != null) { if (vitem.Printable) { textInDoc.Printable = 1; } else { textInDoc.Printable = 0; } } } //look for barcodes if (vitem.Name.EndsWith("%")) { var varcnt = doc.DocObjects.Barcodes.Count; for (int i = 1; i <= varcnt; i++) { var abarcodeInDoc = doc.DocObjects.Barcodes.Item(i); if (abarcodeInDoc != null) { if (abarcodeInDoc.Name.StartsWith(beginswith, true, ci)) { if (vitem.Printable) { abarcodeInDoc.Printable = 1; } else { abarcodeInDoc.Printable = 0; } } } } } else { var barcodeInDoc = doc.DocObjects.Barcodes.Item(vitem.Name); if (barcodeInDoc != null) { if (vitem.Printable) { barcodeInDoc.Printable = 1; } else { barcodeInDoc.Printable = 0; } } } Log("element not found in document " + vitem.Name, EnumLogType.Debug); } Log("end processing document", EnumLogType.Debug); //doc.PrintLabel(job.NrOfCopies); Log("printing " + job.NrOfCopies + " items on " + usedprinter, EnumLogType.Info); doc.HorzPrintOffset = job.MoveX; doc.VertPrintOffset = job.MoveY; doc.Rotate(job.Rotate); doc.PrintDocument(job.NrOfCopies); // PrintDocument pd = new PrintDocument(); // pd.Print(); lbl.Documents.CloseAll(); lbl.Quit(); //DataTable dt = codeInfo_DAL.GetData(this.cbb.SelectedValue.ToString()); //if (dt != null && dt.Rows.Count > 0) //{ // foreach (DataRow dr in dt.Rows) // { //codesoftLabel variables in the template // doc.Variables.FormVariables.Item("var0").Value = dr["CodeID"].ToString(); // doc.Variables.FormVariables.Item("var1").Value =dr["Name"].ToString(); // // doc.PrintDocument(3); // doc.PrintLabel(1, 1, 1, 1, 1, ""); // } // //Continuous batch print labels. FormFeed()The parameters such as the output variable must be after the execution, output to the printer. // doc.FormFeed(); // lbl.Quit(); //} return(true); }