效果
学习路径
C# winform调用Bartender进行自定义打印、批量打印、检索文件夹中的模板_哔哩哔哩_bilibili
一、初始环境搭建见:
二、创建bartender文件
1、创建两个文件test.btw,test2.btw,效果图
2、创建流程
文本->单行
①建立文本:姓名(普通文本)
直接输入标题便可
②建立文本:李四
点击名称,输入数据源
输入数据
其余建立文本方式与这个一致,目的是便于vs可操控bartender数据
③建立二维码内容
选择样式之后,左击鼠标展示到页面
双击二维码进行属性编辑
可见性设置为**"无"**:为了隐藏二维码下的文本
右击数据源->删除原有数据源123456
右击数据源->新建
选择已有或新建数据源
按照上述信息将姓名、性别、班级添加入二维码
3、将两个btw存入项目文件夹中
三、编写vs文件
1、修改App.config,避免不兼容问题
2、Form1.cs
cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Seagull.BarTender.Print;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace test_bartender
{
//这里的Form1继承自Form
public partial class Form1 : Form
{
public Form1()
{
//来自分布类的方法-窗口展示
InitializeComponent();
}
public Engine engine = new Engine();//定义一个打印机引擎
public LabelFormatDocument format = null;//获取模板内容
public static string path = Application.StartupPath + @"\Model";//模板路径(Application.StartupPath获取当前执行路径)
public static DirectoryInfo direct = new DirectoryInfo(path);//实例化指定文件路径的目录
public FileInfo[] fileInfo = direct.GetFiles();//获取指定路径上的所有文件
public List<String> fileList = new List<string>();//所有模板文件数据(定义一个空列表)
//加载模板文件名称至listBox控件
public void loadList_Model() {
//循环文件列表
foreach(var item in fileInfo)
{
//筛选出指定格式的文件(如果文件后缀为.BTW)
if (item.Extension.ToUpper() == ".BTW")
{
fileList.Add(item.Name);//将文件的Name存入列表fileList
}
}
//数据绑定,这里绑定了数据源到fileList之后就不能在对这个进行绑定,否则造成重复,所以后面模糊查询需要创建新的数据源
listb_models.DataSource = fileList;
}
//执行打印
//printmodel:模板名
//printnum:数量
//Sname:姓名
//Sex:性别
//Sclass:班级
public void Pint_model(string printmodel, int printnum, string Sname, string Sex, string Sclass)
{
for (int i = 0; i < printnum; i++)
{
engine.Start();//开始打印
btn_print.Enabled = false;//防止按钮重复点击,开始打印九设置打印为进制
format = engine.Documents.Open(path + $"\\{printmodel}");//从路径path中的模板printmodel去打开文件
if (Sname != "")
{
format.SubStrings["姓名"].Value = tb_name.Text;
format.SubStrings["性别"].Value = tb_sex.Text;
format.SubStrings["班级"].Value = tb_class.Text;
}
Result rel = format.Print();//获取打印状态
if (rel == Result.Success)
{
MessageBox.Show("打印成功!");
}
else
{
MessageBox.Show("打印失败!");
}
btn_print.Enabled = true;//启加载完成,启用按钮
engine.Stop();//打印完成
}
}
//窗口加载时使用
private void Form1_Load(object sender, EventArgs e)
{
loadList_Model();//启用加载模板
}
private void tb_model_TextChanged(object sender, EventArgs e)
{
string searchTxt = tb_model.Text;//获取文本框输入的要查询的数据
List<string> searchModel = new List<string>();//声明一个列表存放 查询出的文件名
if (tb_model.Text.Trim().Length == 0)//如果查询框中的数据为空
{
//创建新的数据源:BindingSource
BindingSource bs = new BindingSource();
bs.DataSource = fileList;//将查询的全部模板数据存入这个新的数据源
listb_models.DataSource = bs;//将重新查到的全部模板数据又重新绑定给listb_models
}
else//有输入的数值,即需要模糊查询
{
for (int i = 0; i < fileList.Count; i++)//对全部模板数据进行循环
{
//查询我的全部模板数据的单项,查询这一项的是否包含searchTxt(输入的值)
searchModel = fileList.Where(m => m.Contains(searchTxt)).ToList();//查询是否子项是否包含输入的值
}
BindingSource bs = new BindingSource();//新建数据源
bs.DataSource = searchModel;//数据源 绑定含有输入的数据的项
listb_models.DataSource = bs;//ListBox控件重新绑定数据
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
//按钮点击事件
private void btn_print_Click(object sender, EventArgs e)
{
int num = (int) nup_num .Value;//获取打印数量
string modelname;
if (listb_models.SelectedIndex>=0) {
modelname = listb_models.SelectedItem.ToString();//获取ListBox选中的模板
Pint_model(modelname,num,tb_name.Text,tb_sex.Text,tb_class.Text);
}
else
{
MessageBox.Show("请选择模板");
}
}
}
继承举个例子
现在有个Human类
//public partial class Human
//{
// public string Name { get; set; }
// public int Age { get; set; }
//}
现在有学生类Student
public partial class Student
{
//学生类也存在Name和Age属性
public string Name { get; set; }
public int Age { get; set; }
//还有自己独有的一些属性
public int score { get; set; }
//由于Name和Age可以直接来自Hum,就可以替换成 public partial class Student:Human如下
}
//public partial class Student :Human
//{
// //还有自己独有的一些属性
// public int score { get; set; }
// //通过继承Student除了自己独有的属性score,还含有Huam的全部属性
//}
}
3、Form1.cs设计页面
4、Form1.Designer.cs
cs
namespace test_bartender
{
//分部类,相同的命名空间下,会将相同的类合并,在Form1.cs可以看到也有一个类Form1,运行的时候,这里的类会合并到Form1.cs文件中
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listb_models = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.tb_model = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.tb_name = new System.Windows.Forms.TextBox();
this.tb_class = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.tb_sex = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.nup_num = new System.Windows.Forms.NumericUpDown();
this.btn_print = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.nup_num)).BeginInit();
this.SuspendLayout();
//
// listb_models:模板内容
//
this.listb_models.Cursor = System.Windows.Forms.Cursors.Hand;
this.listb_models.FormattingEnabled = true;
this.listb_models.ItemHeight = 15;
this.listb_models.Location = new System.Drawing.Point(21, 111);
this.listb_models.Name = "listb_models";
this.listb_models.Size = new System.Drawing.Size(280, 244);
this.listb_models.TabIndex = 3;
//
// label1-大标题
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.ForeColor = System.Drawing.SystemColors.ControlText;
this.label1.Location = new System.Drawing.Point(183, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(269, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Winform调用Bartender打印";
//
// label2-模板名标题
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.Location = new System.Drawing.Point(18, 80);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(71, 15);
this.label2.TabIndex = 1;
this.label2.Text = "模板名:";
//
// tb_model:输入的模板名
//
this.tb_model.Location = new System.Drawing.Point(84, 77);
this.tb_model.Name = "tb_model";
this.tb_model.Size = new System.Drawing.Size(217, 25);
this.tb_model.TabIndex = 2;
this.tb_model.TextChanged += new System.EventHandler(this.tb_model_TextChanged);
//
// label3-姓名标题
//
this.label3.AutoSize = true;
this.label3.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label3.Location = new System.Drawing.Point(372, 111);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(55, 15);
this.label3.TabIndex = 4;
this.label3.Text = "姓名:";
//
// tb_name-姓名文本框
//
this.tb_name.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.tb_name.Location = new System.Drawing.Point(420, 108);
this.tb_name.Name = "tb_name";
this.tb_name.Size = new System.Drawing.Size(100, 18);
this.tb_name.TabIndex = 5;
this.tb_name.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// tb_class-班级文本框
//
this.tb_class.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.tb_class.Location = new System.Drawing.Point(420, 148);
this.tb_class.Name = "tb_class";
this.tb_class.Size = new System.Drawing.Size(100, 18);
this.tb_class.TabIndex = 7;
//
// label4-班级标题
//
this.label4.AutoSize = true;
this.label4.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label4.Location = new System.Drawing.Point(372, 151);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(55, 15);
this.label4.TabIndex = 6;
this.label4.Text = "班级:";
this.label4.Click += new System.EventHandler(this.label4_Click);
//
// tb_sex-性别标题
//
this.tb_sex.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.tb_sex.Location = new System.Drawing.Point(420, 188);
this.tb_sex.Name = "tb_sex";
this.tb_sex.Size = new System.Drawing.Size(100, 18);
this.tb_sex.TabIndex = 9;
//
// label5-性别标题
//
this.label5.AutoSize = true;
this.label5.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label5.Location = new System.Drawing.Point(372, 191);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(55, 15);
this.label5.TabIndex = 8;
this.label5.Text = "性别:";
//
// label6-数量标题
//
this.label6.AutoSize = true;
this.label6.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label6.Location = new System.Drawing.Point(372, 260);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(55, 15);
this.label6.TabIndex = 10;
this.label6.Text = "数量:";
//
// nup_num-数量文本框
//
this.nup_num.Location = new System.Drawing.Point(420, 250);
this.nup_num.Name = "nup_num";
this.nup_num.Size = new System.Drawing.Size(100, 25);
this.nup_num.TabIndex = 11;
this.nup_num.ValueChanged += new System.EventHandler(this.numericUpDown1_ValueChanged);
//
// btn_print-打印按钮
//
this.btn_print.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.btn_print.Font = new System.Drawing.Font("宋体", 10F);
this.btn_print.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.btn_print.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.btn_print.Location = new System.Drawing.Point(375, 316);
this.btn_print.Name = "btn_print";
this.btn_print.Size = new System.Drawing.Size(150, 30);
this.btn_print.TabIndex = 12;
this.btn_print.Text = "打印";
this.btn_print.UseVisualStyleBackColor = false;
this.btn_print.Click += new System.EventHandler(this.btn_print_Click);
//
// Form1-窗口
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(635, 387);
this.Controls.Add(this.btn_print);
this.Controls.Add(this.nup_num);
this.Controls.Add(this.label6);
this.Controls.Add(this.tb_sex);
this.Controls.Add(this.label5);
this.Controls.Add(this.tb_class);
this.Controls.Add(this.label4);
this.Controls.Add(this.tb_name);
this.Controls.Add(this.label3);
this.Controls.Add(this.listb_models);
this.Controls.Add(this.tb_model);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "窗口";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.nup_num)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox tb_model;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox tb_name;
private System.Windows.Forms.TextBox tb_class;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox tb_sex;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ListBox listb_models;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
private System.Windows.Forms.NumericUpDown nup_num;
private System.Windows.Forms.Button btn_print;
}
}