C#使用标签软件Bartender打印标签模板

下面是C#使用标签软件Bartender打印标签模板的完整攻略:

1. 引入Bartender SDK

首先需要在C#工程中引入Bartender SDK。在 Visual Studio 中,打开项目 Solution Explorer,右键点击引用目录,选择添加引用,找到刚刚安装的 Bartender SDK 程序文件夹下的 “Interop.SeagullBarTender.dll” 文件,添加引用即可。

2. 创建Bartender对象并打开模板

using Seagull.BarTender.Print;
using Seagull.BarTender.PrintServer;
using Seagull.BarTender.Print.Database;
using Seagull.BarTender.Print.Message;
using Seagull.BarTender.Print.Server;

//Bartender 打印
void PrintLabel(string labelfileName)
{
    Engine btEngine = new Engine();
    //返回 Bartender 程序界面
    btEngine.Visible = true;
    //打开标签模板文件
    LabelFormatDocument btFormat = btEngine.Documents.Open($"{labelfileName}");
}

3. 设计打印数据源

数据源是C#向Bartender传递数据的桥梁,必须在Bartender中进行设计。在Bartender中,选择【文件】->【新建】->【数据源】来新建一个空数据源,命名为“testCSV”,然后在数据源里面添加字段,例如“Name”和 “Value”。

4. 绑定数据

在上一步中我们创建了数据源“testCSV”,现在需要在C#中给数据源中的“Name”和“Value”绑定相应的数据。代码示例如下:

//获取当前打印机
Printer printer = new Printer(printerName);
//获取数据源并绑定数据
Database btDatabase = btFormat.Databases.GetDatabase("testCSV");
//获取含 “Name” 和 “Value” 两个字段的表
Table btTable = btDatabase.Tables.GetTable("Data");
//获取表中的“Name”字段对象
Field nameField = btTable.Fields.GetField("Name");
//获取表中的“Value”字段对象
Field valueField = btTable.Fields.GetField("Value");
//向表中添加数据并进行绑定操作
btTable.Records.Add();
nameField.Value = "Name";
valueField.Value = "Value";
btFormat.PrintSetup.PrinterName = printerName;

5. 执行Print方法并打印

绑定完数据源后,我们通过执行 Print 方法来实现打印。代码示例如下:

//打印
Result result = btFormat.Print("test",out messages);
//判断打印是否成功
if (result == Result.Success)
{
    MessageBox.Show("打印成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (result == Result.Failure)
{
    MessageBox.Show("打印失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

至此,整个打印流程已经实现了。下面是两个示例说明:

示例一:

场景:使用BT打印机打印一个条形码

//打印一个条形码
private void button1_Click(object sender, EventArgs e)
{
   //调用打印方法,传入BT打印机
   PrintLabel(labelFileName,BTPrinterName);
}

void PrintLabel(string labelfileName,string printerName)
{
    //实例化Bartender引擎
    Engine btEngine = new Engine();
    //返回 Bartender 程序界面
    btEngine.Visible = true;
    //打开标签模板文件
    LabelFormatDocument btFormat = btEngine.Documents.Open($"{labelfileName}");
    //获取当前打印机
    Printer printer = new Printer(printerName);
    //获取数据源并绑定数据
    Database btDatabase = btFormat.Databases.GetDatabase("testCSV");
    //获取含 “Name” 和 “Value” 两个字段的表
    Table btTable = btDatabase.Tables.GetTable("Data");
    //获取表中“Name”字段对象
    Field nameField = btTable.Fields.GetField("Name");
    //获取表中“Value”字段对象
    Field valueField = btTable.Fields.GetField("Value");
    //往数据源中添加数据
    btTable.Records.Add();
    nameField.Value = "Value";
    valueField.Value = "123456789";
    btFormat.PrintSetup.PrinterName = printerName;
    //执行Bartender打印操作
    Messages messages;
    Result result = btFormat.PrintOut(false, false, PrintOutputConstants.sequential, 1, printerName, "Test");

    //判断是否打印成功
    if (result == Result.Success)
    {
        MessageBox.Show("打印成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else if (result == Result.Failure)
    {
        MessageBox.Show("打印失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //关闭Bartender引擎
    btFormat.Close(SaveOptions.DoNotSaveChanges);
    btEngine.Stop(SaveOptions.DoNotSaveChanges);
}

示例二:

场景:使用BT打印机打印一个材料标签

//打印一个材料标签
private void button2_Click(object sender, EventArgs e)
{
   //调用打印方法,传入BT打印机
   PrintLabel(labelFileName,BTPrinterName);
}

void PrintLabel(string labelfileName,string printerName)
{
    //实例化Bartender引擎
    Engine btEngine = new Engine();
    //返回 Bartender 程序界面
    btEngine.Visible = true;
    //打开标签模板文件
    LabelFormatDocument btFormat = btEngine.Documents.Open($"{labelfileName}");
    //获取当前打印机
    Printer printer = new Printer(printerName);
    //获取数据源并绑定数据
    Database btDatabase = btFormat.Databases.GetDatabase("testCSV");
    //获取含 “Name” 和 “Value” 两个字段的表
    Table btTable = btDatabase.Tables.GetTable("Data");
    //获取表中“Name”字段对象
    Field nameField = btTable.Fields.GetField("Name");
    //获取表中“Value”字段对象
    Field valueField = btTable.Fields.GetField("Value");
    //往数据源中添加数据
    btTable.Records.Add();
    nameField.Value = "Name";
    valueField.Value = "材料名称";
    //往数据源中添加数据
    btTable.Records.Add();
    nameField.Value = "Unit";
    valueField.Value = "单位";
    //往数据源中添加数据
    btTable.Records.Add();
    nameField.Value = "Weight";
    valueField.Value = "重量";
    //往数据源中添加数据
    btTable.Records.Add();
    nameField.Value = "Code";
    valueField.Value = "编码";
    //绑定数据源
    btFormat.PrintSetup.PrinterName = printerName;
    btFormat.PrintSetup.UseDatabasePallets = true;

    //执行Bartender打印操作
    Messages messages;
    Result result = btFormat.Print("test", out messages);
    //判断是否打印成功
    if (result == Result.Success)
    {
        MessageBox.Show("打印成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    else if (result == Result.Failure)
    {
        MessageBox.Show("打印失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    //关闭Bartender引擎
    btFormat.Close(SaveOptions.DoNotSaveChanges);
    btEngine.Stop(SaveOptions.DoNotSaveChanges);
}

以上就是 C#使用标签软件Bartender打印标签模板的完整攻略,希望对你有所帮助。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#使用标签软件Bartender打印标签模板 - Python技术站

(0)
上一篇 2023年6月7日
下一篇 2023年6月7日

相关文章

  • C#往线程里传递参数的方法小结

    针对“C#往线程里传递参数的方法小结”,我将分以下几步来进行详细讲解: 一、参数传递的基本方法 在C#中,向Thread线程传递参数有多种方法。其中最常用的一种是通过将参数封装到一个对象中,再将该对象传递给Thread.Start()方法。这个对象可以是任何一个类的实例,常用的方式是使用匿名类型或者是Tuple类型。 具体来说,可以按照如下方式编写代码: i…

    C# 2023年6月7日
    00
  • c#动态调用Webservice的两种方法实例

    下面是关于”c#动态调用Webservice的两种方法实例”的完整攻略。 一、使用Visual Studio生成Web服务引用 1. 根据WebService的URL生成引用 在Visual Studio中,右键选择项目 -> 添加服务引用(Add Service Reference),在弹出的窗口中输入WebService的URL地址,然后单击“Go…

    C# 2023年6月6日
    00
  • C# 从 UTF-8 流中读取字符串的正确方法及代码详解

    具体的攻略如下: 1. 首先了解 UTF-8 编码和流操作 在进行 UTF-8 流读取字符串之前,需要先了解 UTF-8 编码和流操作。UTF-8 是一种编码方式,用于将 Unicode 字符转换成字节序列。流是一种读取和写入数据的方式,是在内存中逐步读取和处理数据的过程。在 C# 中,可以使用 System.IO 命名空间中的类来实现流读取和写入。 2. …

    C# 2023年6月8日
    00
  • Sqlserver 2005使用XML一次更新多条记录的方法

    SQL Server 2005可以利用XML实现一次更新多条记录的操作。以下是步骤的详细说明和示例。 步骤一:创建存储过程 首先,需要创建一个存储过程来实现在SQL Server 2005中使用XML一次更新多条记录的操作。 CREATE PROCEDURE UpdateMultipleRecords @XMLData XML AS BEGIN –1.创建…

    C# 2023年6月8日
    00
  • C#利用win32 Api 修改本地系统时间、获取硬盘序列号

    修改本地系统时间 首先需要导入System.Runtime.InteropServices这个命名空间. using System.Runtime.InteropServices; 然后我们通过GetSystemTime方法获取系统时间,再通过SetSystemTime方法修改系统时间. [DllImport("Kernel32.dll"…

    C# 2023年6月1日
    00
  • 探讨jQuery的ajax使用场景(c#)

    探讨 jQuery 的 ajax 使用场景(c#) 什么是 ajax ajax 是 Asynchronous JavaScript and XML 的缩写,也就是异步的 JavaScript 和 XML。它是一种无需刷新整个页面就可以与服务器进行数据交互的技术。 jQuery 中的 ajax jQuery 提供了一些方便的方式来实现 ajax。通过 jQue…

    C# 2023年5月31日
    00
  • 解析C#中用Process类杀死进程,执行命令的深入分析

    解析C#中使用Process类杀死进程、执行命令的深入分析 在C#中,Process类提供了一个简单而有效的方法来管理系统进程。Process类可以启动一个新进程、关闭已有进程、管理系统资源等。本篇攻略将详细介绍使用Process类来杀死进程和执行命令的方法以及其中涉及的深入原理。 1. C#中使用Process类杀死进程 1.1 基本用法 在C#中,使用P…

    C# 2023年5月15日
    00
  • C#监控文件夹变化的方法

    下面我将为你详细讲解“C#监控文件夹变化的方法”的完整攻略。 1. 使用FileSystemWatcher类 在C#中,我们可以使用FileSystemWatcher类来监听特定文件夹的变化。使用方法如下: // 实例化一个FileSystemWatcher对象 FileSystemWatcher watcher = new FileSystemWatche…

    C# 2023年6月1日
    00
合作推广
合作推广
分享本页
返回顶部