使用Bartender开发条码打印软件
1、Bartender软件可在百度搜索进行下载,安装步骤也可以在在安装Bartender软件是一定要注意勾选上SDK功能安装,如下图所示:

2、引用Seagull.BarTender.Print,路径为:C:\Program Files\Seagull\BarTender Suite\SDK\Assemblies。引用完成后定义初始化变量,如下图:


3、下图为页面初始化时需要获取的数据。

4、获取打印机列表的代码段如下图所示

5、选择需要打印条码的Bartender模板,通过模板获取设计标签的数据源名称。并填充都DataTable中。数据源对应的Value可通过手动修改赋值。



6、按照上面操作后,选择打印机点击打印按钮即可打印条码。

7、啥也不说。上源码。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Seagull.BarTender.Print;
namespace Bartender打印条码_.NET_._0
{
public partial class Form1 : Form
{
DataTable dt = new DataTable();
//项目名称
private const string appName = "Bartender打印条码_.NET_._0";
private Engine engine = null; // The BarTender Print Engine
private LabelFormatDocument format = null; // The currently open Format
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog of = new OpenFileDialog();
of.Title = "Select a folder that contains BarTender formats.";
DialogResult dr = of.ShowDialog();
if (dr == DialogResult.OK)
{
this.txtTemplateFile.Text = of.FileName;
GetTemplateDataSource();
}
}
private void GetTemplateDataSource()
{
try
{
if (format != null)
format.Close(SaveOptions.DoNotSaveChanges);
format = engine.Documents.Open(this.txtTemplateFile.Text.Trim());
engine.Start();
//模板中的数据源列
//为已有DataTable添加一新列
DataColumn dc1 = new DataColumn("DataSource", typeof(string));
dt.Columns.Add(dc1);
//为已有DataTable添加一新列
DataColumn dc2 = new DataColumn("DataValue", typeof(string));
dt.Columns.Add(dc2);
for (int i = 0; i < format.SubStrings.Count; i++)
{
DataRow dr = dt.NewRow();
dr[0] = format.SubStrings[i].Name;
dr[1] = format.SubStrings[i].Value;
dt.Rows.InsertAt(dr, i);
}
this.dataGrid.DataSource = dt;
}
catch (Exception ex)
{
//errorMessage = String.Format("Unable to open format: {0}\nReason: {1}", browsingFormats[index], comException.Message);
format = null;
}
}
private void btnPrint_Click(object sender, EventArgs e)
{
if (comPrinter.SelectedItem != null)
format.PrintSetup.PrinterName = comPrinter.SelectedItem.ToString();
format.PrintSetup.IdenticalCopiesOfLabel = 1;
Messages messages = null;
// 10 seconds
int waitForCompletionTimeout = 10000;
//向模板文件中赋值
//format.SubStrings["1"].Value = "123";
//format.SubStrings["2"].Value = "456";
//向模板数据源赋值
SetDataSourceValue();
Result result = format.Print(appName, waitForCompletionTimeout, out messages);
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
//耗时6秒
engine = new Engine(true);
}
catch (Exception ex)
{
// If the engine is unable to start, a PrintEngineException will be thrown.
MessageBox.Show(ex.Message);
this.Close(); // Close this app. We cannot run without connection to an engine.
return;
}
GetPrintList();
}
private void GetPrintList()
{
// Get the list of printers
Printers printers = new Printers();
foreach (Printer printer in printers)
{
comPrinter.Items.Add(printer.PrinterName);
}
if (printers.Count > 0)
{
// Automatically select the default printer.
comPrinter.SelectedItem = printers.Default.PrinterName;
}
}
//条码预览
private void btnPreview_Click(object sender, EventArgs e)
{
if (format != null)
format.Close(SaveOptions.DoNotSaveChanges);
try
{
format = engine.Documents.Open(this.txtTemplateFile.Text.ToString());
}
catch (System.Runtime.InteropServices.COMException comException)
{
MessageBox.Show(this, String.Format("Unable to open format: {0}\nReason: {1}", this.txtTemplateFile.Text.ToString(), comException.Message), appName);
format = null;
}
if (format != null)
{
//向模板数据源赋值
SetDataSourceValue();
// Select the printer in use by the format.
comPrinter.SelectedItem = format.PrintSetup.PrinterName;
FrmPreview preview = new FrmPreview(format);
preview.ShowDialog();
}
else
{
//// Clear any previous image.
//picThumbnail.ImageLocation = "";
}
}
private void SetDataSourceValue()
{
//获取DataGrid中最终数据
DataTable printData = (dataGrid.DataSource as DataTable);
//不要把数据源写死
for (int i = 0; i < printData.Rows.Count; i++)
{
format.SubStrings[printData.Rows[i][0].ToString()].Value = printData.Rows[i][1].ToString();
}
}
}
}