public static void save_testfile() { XmlSerializer xsSubmit = new XmlSerializer(typeof(JobFile)); var subReq = new JobFile(); subReq.LabelDocument = "Document1.lab"; subReq.PrinterName = "TestPrinter"; subReq.UseDefaultPrinter = true; subReq.NrOfCopies = 1; subReq.MoveX = 0; subReq.MoveY = 0; subReq.Rotate = 0; subReq.Variables = new List <Variable>(); subReq.Variables.Add(new Variable() { Name = "var0", Value = "var 0 valule", Printable = true }); subReq.Variables.Add(new Variable() { Name = "var1", Value = "var 1 value", Printable = true }); var xml = ""; using (var sww = new Utf8StringWriter()) { using (XmlWriter writer = XmlWriter.Create(sww)) { xsSubmit.Serialize(writer, subReq); xml = sww.ToString(); System.IO.File.WriteAllText(Path.Combine(Properties.Settings.Default.SourcesFolderPath, "samplefile.xml"), xml); } } }
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); }