C# Winform 使用 BarTender打印条码

- 1. 使用软件BarTender 设计打印模板

贴一个入门级使用教程:https://blog.csdn.net/ononeway/article/details/119912602

我在项目中使用的是txt文本格式保存的字段以及数据,第一行是绑定数据的字段,第二行是字段对应的数据。

点击查看代码
"ITEM_CODE","ITEM_DESC","UOM_NAME","VENDOR_CODE","VENDOR_NAME","ORG_CODE","ORG_NAME","LOT_NUMBER","SERIAL_NO","DISABLE_DATE","SHIP_DATE","QTY","CARTON_CODE","ATTRIBUTE1","ATTRIBUTE2","ATTRIBUTE3","ATTRIBUTE4","ATTRIBUTE5","ATTRIBUTE6","ATTRIBUTE7","ATTRIBUTE8","ATTRIBUTE9","BarCode"
"","","","","","","","","","","","0","","15L方便桶","202308150009","15L","500","2273","C202104150001","成品条码"," xxxxxx3号","13983135628",""

这是完成模板效果图:

image

右键属性选择绑定数据的字段,以及设置文本的格式。
image

- 2. Winfrom集成打印

  • 1.第一步:需要将绑定数据的字段输出到文本中保存下来
    MODEL_NAME:文本模板的名称
    BarCodeList:打印数据的集合,支持多个打印
    PrintReNum:打印份数

下面代码的整体思路就是,先输出字段在文本中,然后再换行将数据输出到文本中。

点击查看代码
private int PrintToTxt(string MODEL_NAME, List<ZR.Model.BarTender.BarCodePram> BarCodeList, int PrintReNum)
        {
            if (PrintReNum <= 0) return -1;
            string FilePath = AppDomain.CurrentDomain.BaseDirectory.Replace("bin", @"Report\BarCode") + "$" + MODEL_NAME + ".txt";
            StringBuilder sb = new StringBuilder();

            Type t = typeof(ZR.Model.BarTender.BarCodePram);
            PropertyInfo[] myPropertyInfo = t.GetProperties();
            string Colums = string.Empty;
            for (int i = 0; i < myPropertyInfo.Length; i++)
            {

                if (i == myPropertyInfo.Length - 1)
                {
                    Colums += "\"" + myPropertyInfo[i].Name + "\"";
                }
                else
                {
                    Colums += "\"" + myPropertyInfo[i].Name + "\",";
                }
            }
            sb.AppendLine(Colums);
            foreach (ZR.Model.BarTender.BarCodePram pram in BarCodeList)
            {
                string row = string.Empty;
                for (int i = 0; i < myPropertyInfo.Length; i++)
                {

                    if (i == myPropertyInfo.Length - 1)
                    {
                        row += "\"" + myPropertyInfo[i].GetValue(pram, null) + "\"";
                    }
                    else
                    {
                        row += "\"" + myPropertyInfo[i].GetValue(pram, null) + "\",";
                    }
                }
                for (int j = 0; j < PrintReNum; j++)
                {
                    sb.AppendLine(row);
                }
            }
            int ret = SaveToTxt(sb, FilePath);
            return ret;
        }
  • 2.第二步:调用打印方法打印

btFormat.PrintOut(false, true); //第二个false设置打印时是否跳出打印属性

点击查看代码
public void Print(string PrintName, int PrintCount = 1, int PrintNum = 1)
        {
            try
            {
                if (btApp == null)
                {
                    btApp = new BarTender.Application();
                }
                btFormat = btApp.Formats.Open(ReportPath + PrintName, false, "");
                btFormat.InputDataFileSetup.TextFile.FileName = (ReportPath + "$" + PrintName + ".txt");
                btFormat.PrintSetup.UseDatabase = true;
                btFormat.PrintSetup.IdenticalCopiesOfLabel = PrintCount;  //设置同序列打印的份数
                btFormat.PrintSetup.NumberSerializedLabels = PrintNum;  //设置需要打印的序列数 

                if (btFormat.PrintSetup.UseDatabase)
                {
                    btFormat.RecordRange = "1...";
                }
                //BarTenderSetParms(btFormat, Parms);
                int A = btFormat.PrintOut(false, true); //第二个false设置打印时是否跳出打印属性
                btFormat.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); //退出时是否保存标签

                #region 处理打印无效果的问题

                Process p = new Process();
                p.StartInfo.FileName = "bartend.exe";
                //列印btw檔案並最小化程序
                p.StartInfo.Arguments = $@"/AF={ReportPath + PrintName} /P /min=SystemTray";
                p.EnableRaisingEvents = true;
                int pageCount = Convert.ToInt32(PrintCount);
                for (int i = 0; i < pageCount; i++)
                {
                    p.Start();
                }

                #endregion  
            }

点击打印之后弹出打印框点击打印即可

如果是打印的虚拟打印机上面会提示出入名称

- 3.最终效果

测试Demo地址: https://github.com/IMLYQ/BartenderTest

posted @ 2023-08-15 13:58  我本梁人  阅读(2098)  评论(0编辑  收藏  举报