首页 新闻 会员 周边

asp.net调用 BarTender软件打印功能

2
[已关闭问题] 关闭于 2014-02-26 09:01

   很久没有在博客里留下脚印,最近段时间在研究BarTender软件在程序当中,在线打印、在线修改、预览的功能,刚开始,弄的头都大了
    现在经历过,会发现其实也不难(在线修改,主要是通过后台修改数据,转化为图片的格式,查看)

   下面主要和大家分享下代码,有不足之处,还希望能够批评指正

 BarTender.Format btFormat;
        BarTender.Application btApp;

    btFormat = new BarTender.Format();
            btApp = new BarTender.Application();

//加载
            if (!IsPostBack)
            {

                sLabel = ConfigurationManager.AppSettings["LabelRepository"];
                sPath = Server.MapPath(Request.ApplicationPath);
                Image1.ImageUrl = @"~/" + sLabel + "/BarCodeTable.jpg";

                barcodetable barcode = db.GetBarCodeTableById(iBarId);
                this.TextBox1.Text = barcode.Name;
                this.TextBox2.Text = barcode.Number;
                this.TextBox3.Text = barcode.UPS;
                this.TextBox4.Text = barcode.Tracking;
                this.TextBox5.Text = barcode.Amount;
                this.TextBox6.Text = barcode.Visa;

            }

 

//修改

 try
            {
                db.UpdateBarCodeTable(iBarId, sName, sNumber, sTacking, sUPS, sAmount, sVisa);
                sLabel = ConfigurationManager.AppSettings["LabelRepository"];
                sPath = Server.MapPath(Request.ApplicationPath);

                btFormat = btApp.Formats.Open(@sPath + sLabel + "\\BarCodeTable.btw", false, "");
                btFormat.PrintSetup.NumberSerializedLabels = 1;
                btFormat.SetNamedSubStringValue("Name", sName);
                btFormat.SetNamedSubStringValue("Number", sNumber);
                btFormat.SetNamedSubStringValue("UPS", sUPS);
                btFormat.SetNamedSubStringValue("Tracking", sTacking);
                btFormat.SetNamedSubStringValue("Amount", sAmount);
                btFormat.SetNamedSubStringValue("Visa", sVisa);
                btFormat.SaveAs(@sPath + sLabel + "\\BarCodeTable.btw", true); 

                btFormat.ExportToFile(@sPath + sLabel + "\\BarCodeTable.jpg", "jpg", BarTender.BtColors.btColors24Bit, BarTender.BtResolution.btResolutionPrinter, BarTender.BtSaveOptions.btSaveChanges);
                Image1.ImageUrl = @"~/" + sLabel + "/BarCodeTable.jpg";

                   btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

//打印

      btFormat = btApp.Formats.Open(@sPath + sLabel + "\\BarCodeTable.btw", false, "");       

//同一个条码,打印5个

//btFormat.PrintSetup.IdenticalCopiesOfLabel = 5;   

//条码递增+1

  //btFormat.PrintSetup.NumberSerializedLabels = 4;

  btFormat.PrintOut(false, false);       

     btApp.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);

以上为主要代码,但是还有些内容,现在还在摸索当中。

C#
问题补充:

针对barTender打印做个补充,以上代码可以在本地实现本地条码打印。

            对于近段时间,根据客户的需求,开发一个“供应商通过远程访问服务器,能够实现手动输入数据,获取本地的打印机驱动,本地端打印条码”的功能

           实现这样的功能,asp.net+BarTender条码软件(同时支持50个用户的操作)

  第一,引用BarTenderPrintClient.js ,WebLabelPrintSample.js

第二,

  public void Print()
    {
        try
        {
            TaskManager taskManager = (TaskManager)Application["TaskManager"];

            // Make sure the task manager is valid and that engines are running.
            if ((taskManager != null) && (taskManager.TaskEngines.AliveCount != 0))
            {
                LabelFormat labelFormat = CreateLabelFormatFromPageData(_listLabelFormats.SelectedLabelFormatRepositoryFullPath);
                if (labelFormat != null)
                {
                    labelFormat.PrintSetup.IdenticalCopiesOfLabel = Convert.ToInt32(txtNumber.Text); ;
                    // labelFormat.PrintSetup.NumberOfSerializedLabels = copies;
                    string sScanCode = softService.GetItemScanCode(txtCode.Text, txtDesc.Text).Substring(0, 13);
                    labelFormat.SubStrings.SetSubString("CSPickITScanCode", sScanCode);
                    labelFormat.SubStrings.SetSubString("ItemCode", txtCode.Text);
                    labelFormat.SubStrings.SetSubString("description", txtDesc.Text);
                    labelFormat.SubStrings.SetSubString("Vendor", txtVendor.Text);
                    labelFormat.SubStrings.SetSubString("PO", txtPo.Text);
                    labelFormat.SubStrings.SetSubString("NetWeight", txtNet.Text);
                    labelFormat.SubStrings.SetSubString("quantity", txtCount.Text);
                    labelFormat.SubStrings.SetSubString("Batch", txtBatch.Text);
                    labelFormat.SubStrings.SetSubString("Number", txtCount.Text);

                    PrintLabelFormatTask taskPrint = new PrintLabelFormatTask(labelFormat);
                    taskPrint.PrintTimeout = Int32.Parse(ConfigurationManager.AppSettings["PrintTimeout"]);

                    if (_listPrinters.LastPrintType == "client")
                    {
                        // Set PrintTimeout to 0 to ensure print job status monitoring does not interfere with client printing
                        taskPrint.PrintTimeout = 0;
                        // Since the client print needs to post-process the print job, sign up for the succeeded event.
                        taskPrint.Succeeded += new EventHandler<TaskEventArgs>(TaskPrint_Succeeded);
                    }
                    taskManager.TaskQueue.QueueTaskAndWait(taskPrint, 60000);


                    Services.InsertBarCodeRecord(sScanCode, txtCode.Text.Trim(), txtDesc.Text.Trim(), txtVendor.Text, txtPo.Text, txtNet.Text, txtCount.Text, txtBatch.Text, txtNumber.Text);

                    //Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>window.location='BasicConfig.aspx'</script>");

                    Services.InsertLog("条码打印", "打印", "按照Item打印");

                    // Get messages
                    foreach (Message message in taskPrint.Messages)
                    {
                        string formattedMessage = message.Text.Replace("\n", "<br/>");
                        _alert.AddMessage(formattedMessage + "<br/>");
                    }
                }
            }
            else
            {
                ShowMessage("Unable to print. Please make sure you have BarTender installed, activated as <br />Enterprise Print Server edition, and that print engines are running. See the <br />'Manage Print Engines' menu task.");
            }
        }
        catch (Exception ex)
        {
            ShowMessage(ex.Message);
        }
    }

    void TaskPrint_Succeeded(object sender, TaskEventArgs e)
    {
        PrintLabelFormatTask taskPrint = sender as PrintLabelFormatTask;

        if (taskPrint != null)
        {
            _hiddenClientPrintCode.Value = taskPrint.PrintCode;

            string printerName = JavaScriptSupport.EscapeSpecialCharacters(_listPrinters.LastPrinterName);
            Page.ClientScript.RegisterStartupScript(GetType(), "SampleClientPrint", string.Format("SampleClientPrint('{0}','{1}','{2}','{3}');", printerName, _hiddenClientPrintCode.ClientID, _alert.MessageClientID, _alert.PanelClientID), true);
        }
    }

    LabelFormat CreateLabelFormatFromPageData(string labelFormatFullPath)
    {
        LabelFormat labelFormat = null;
        bool isLabelDataValid = true;

        // Check if we are server or client printing
        String lastPrinter = Request.Form.Get(_listPrinters.ListUniqueID);
        if (string.IsNullOrEmpty(lastPrinter))
        {
            ShowMessage("No printers were selected. Unable to print.");
        }
        else
        {
            try
            {
                labelFormat = new LabelFormat(_listLabelFormats.SelectedLabelFormatRepositoryFullPath);

            }
            catch (System.IO.FileNotFoundException)
            {
                string message = string.Format("The selected format '{0}' does not exist. It may have been moved, renamed or deleted.", _listLabelFormats.SelectedLabelFormatName);
                ShowMessage(message);
                isLabelDataValid = false;
            }

            if (isLabelDataValid)
            {
                // UpdateControls(labelFormat);

                // For each control get control values into the label format object.
                //UpdateFormatData(labelFormat);
                //_promptControl.UpdateFormatData(labelFormat);
                //_queryPromptsControl.UpdateFormatData(labelFormat);
                //isLabelDataValid = _copyControl.UpdateFormatData(labelFormat);

                labelFormat.PrintSetup.EnablePrompting = false;

                if (_listPrinters.LastPrintType == "server")
                {
                    labelFormat.PrintSetup.PrintToFile = false;
                    labelFormat.PrintSetup.PrinterName = _listPrinters.LastPrinterName;
                }
                else
                {
                    string compatibleServerPrinter = _listPrinters.LastCompatibleServerPrinter;
                    labelFormat.PrintSetup.PrintToFile = true;
                    labelFormat.PrintSetup.PrinterName = compatibleServerPrinter;
                    labelFormat.PrintSetup.PrintToFileLicense = Request.Form.Get(_listPrinters.PrintLicenseUniqueID);
                    string tempFullPath = (string)Application["TempFolderFullPath"];
                    labelFormat.PrintSetup.PrintToFileName = System.IO.Path.Combine(tempFullPath, Guid.NewGuid().ToString() + ".prn");
                }
            }
        }

        return (isLabelDataValid ? labelFormat : null);
    }


    void ShowMessage(string msg)
    {
        _alert.Message = msg;
    }


    public string PrintToFileLicenseJavaScript
    {
        get
        {
            return string.Format("SetPrintLicense(document.getElementById('{0}').value,'{1}','{2}','{3}');", _listPrinters.ListClientID, _listPrinters.PrintLicenseClientID, _alert.MessageClientID, _alert.PanelClientID);
        }
    }

    LabelFormat GetLabelFormatDataFromFile(string labelFormatFullPath)
    {
        LabelFormat labelFormat = null;

        TaskManager taskManager = (TaskManager)Application["TaskManager"];

        // Make sure the task manager is valid and that engines are running.
        if ((taskManager != null) && (taskManager.TaskEngines.AliveCount != 0))
        {
            // Create a task to get all label format properties and submit to the task queue
            // for processing.
            GetLabelFormatTask getFormatTask = new GetLabelFormatTask(labelFormatFullPath);
            taskManager.TaskQueue.QueueTaskAndWait(getFormatTask, 60000);

            // Return the label format object that has all label format data set.
            labelFormat = getFormatTask.LabelFormat;
        }
        return labelFormat;
    }

 

    string CreateControlName(string prefix, string labelFormatFileName, int rowNum)
    {
        return prefix + labelFormatFileName + rowNum.ToString();
    }

 

 

   针对以上的代码,最后做个总结:

          与barTender条码输入集成开发,特别是实现远程打印的功能

  TaskManager taskManager = (TaskManager)Application["TaskManager"];            if ((taskManager != null) && (taskManager.TaskEngines.AliveCount != 0))
            {
                LabelFormat labelFormat = CreateLabelFormatFromPageData(_listLabelFormats.SelectedLabelFormatRepositoryFullPath);
                if (labelFormat != null)
                {
                    labelFormat.PrintSetup.IdenticalCopiesOfLabel = Convert.ToInt32(txtNumber.Text); ;

                    labelFormat.SubStrings.SetSubString("CSPickITScanCode", sScanCode);

                    PrintLabelFormatTask taskPrint = new PrintLabelFormatTask(labelFormat);
                    taskPrint.PrintTimeout = Int32.Parse(ConfigurationManager.AppSettings["PrintTimeout"]);

                                 taskManager.TaskQueue.QueueTaskAndWait(taskPrint, 60000);

以上几行代码就ok了

Soul 今天的主页 Soul 今天 | 菜鸟二级 | 园豆:206
提问于:2012-06-20 17:06
< >
分享
所有回答(6)
0

请问lz有做过客户端打印条码的吗。如果精度要求比较高,必须要专业的条码打印软件,看lz研究这个,不知道有没有做过。

清海扬波 | 园豆:825 (小虾三级) | 2012-07-05 15:03

我用的是Bartender条码打印

支持(0) 反对(0) Soul 今天 | 园豆:206 (菜鸟二级) | 2012-07-23 10:23

@Soul 今天: LZ能不能把示例给我一份。

支持(0) 反对(0) 清海扬波 | 园豆:825 (小虾三级) | 2012-07-23 11:58
0

请教楼主,能不能把代码也给一份我,最主要的是要引用的那个文件在哪里弄来的?我一直找一直找就是找不到啊。谢谢!

农家小子 | 园豆:202 (菜鸟二级) | 2012-12-29 08:44
0

您好,您知道用java怎样调用bartender吗?新手,谢谢关照!

@结合律 | 园豆:202 (菜鸟二级) | 2013-05-10 15:36

参考思路

支持(0) 反对(0) Soul 今天 | 园豆:206 (菜鸟二级) | 2013-05-14 13:49
0

你好,使用ExportToFile只是把模板保存为图片,现在我连接到数据库之中,即想把实际打印出来的内容导出图片,应该如何处理?

加号 | 园豆:202 (菜鸟二级) | 2013-12-04 11:28
0

你好,BarTenderPrintClient.js ,WebLabelPrintSample.js 这两个文件是从哪里找到的!!

zcl_1989 | 园豆:202 (菜鸟二级) | 2016-05-25 14:44
0

有哪个方法实现打印预览么

尔等鼠辈 | 园豆:202 (菜鸟二级) | 2020-06-19 10:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册