C#(ASP.NET)在服务端调用BarTender .NET SDK 打印条码
BarTender模板:
打印:
using System; using System.Collections.Generic; using System.Web; using System.Web.Services; using Seagull.BarTender.Print; namespace PrintServer { /// <summary> /// PrintServer 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 // [System.Web.Script.Services.ScriptService] public class PrintServer : System.Web.Services.WebService { [WebMethod] public string HelloWorld() { return "Hello World"; } /// <summary> /// 通用打印 /// </summary> /// <param name="printerName">打印机名称</param> /// <param name="fileName">打印文件名</param> /// <param name="inputParameters">输入参数</param> /// <param name="printNumber">重复次数</param> /// <param name="serialNumber">序列数</param> [WebMethod] public string CommonPrint(string printerName, string fileName, string[] inputParameters, int printNumber, int serialNumber) { if (printNumber < 1) return "打印次数必须大于0"; try { using (Engine btEngine = new Engine()) { btEngine.Start(); LabelFormatDocument btFormat = btEngine.Documents.Open(fileName); //寻找打印机 if (Printer.VerifyPrinter(printerName)) { btEngine.ActiveDocument.PrintSetup.PrinterName = printerName; } else { btFormat.Close(SaveOptions.DoNotSaveChanges); btEngine.Stop(); return "Error打印机名称错误!"; } int flag = 0; for (int i = 0; i < btFormat.SubStrings.Count; i++) { if (btFormat.SubStrings[i].Name.StartsWith("Bar")) { try { //输入参数的第一个值会赋给Bar001,以此类推 btFormat.SubStrings["Bar" + string.Format("{0:000}", flag + 1)].Value = inputParameters[flag]; } catch { //模板中多余的Bar标签会置空 btFormat.SubStrings["Bar" + string.Format("{0:000}", flag + 1)].Value = ""; } flag = flag + 1; } } btFormat.PrintSetup.IdenticalCopiesOfLabel = printNumber;//重复次数 btFormat.PrintSetup.NumberOfSerializedLabels = serialNumber;//序列数 btEngine.ActiveDocument.Print(); btFormat.Close(SaveOptions.DoNotSaveChanges); btEngine.Stop(); return "OK"; } } catch (Exception ex) { return "Error:" + ex.Message; } } } }
打印机寻找:
using System; using System.Collections.Generic; using System.Drawing.Printing; using System.Web; public class Printer { private static PrintDocument fPrintDocument = new PrintDocument(); /// <summary> /// 获取本机默认打印机名称 /// </summary> public static string DefaultPrinter { get { return fPrintDocument.PrinterSettings.PrinterName; } } /// <summary> /// 获取本机的打印机列表。列表中的第一项就是默认打印机。 /// </summary> private static List<string> GetLocalPrinters() { List<string> fPrinters = new List<string>(); fPrinters.Add(DefaultPrinter); foreach (string fPrinterName in PrinterSettings.InstalledPrinters) { if (!fPrinters.Contains(fPrinterName)) fPrinters.Add(fPrinterName); } return fPrinters; } //检查本地打印机中是否有Printer打印机 public static bool VerifyPrinter(string Printer) { return GetLocalPrinters().Contains(Printer); } }
服务配置:
<?xml version="1.0"?> <configuration> <system.web> <identity impersonate="true" userName="administrator" password="123@abc"/> <authentication mode="Windows"/> <compilation debug="true"/> </system.web> </configuration>