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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM