参照下面连接进行前置页面的搭建
c#:winform调用bartender实现打印(学习整理笔记)_c#怎么连接bartender-CSDN博客
接下来
1、添加控件获取本地打印机
可参照:c#:winform 获取本地打印机列表(下拉列表实现)-CSDN博客
①添加一个ComboBox控件,名为:cb_printer
②引入方法,使其能调用打印机相关功能
③写入方法,将获取的本地打印机添加到ComboBox控件
2、实现打印功能
3、完整代码
App.config
javascript
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
form1.cs
cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Seagull.BarTender.Print;
namespace test_bartender
{
//这里的Form1继承自Form
public partial class Form1 : Form
{
public Form1()
{
//来自分布类的方法-窗口展示
InitializeComponent();
LoadPrinters(); // 加载打印机列表
}
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;
}
// 设置打印机
if (cb_printer.SelectedIndex >= 0)
{
string selectedPrinter = cb_printer.SelectedItem.ToString();
format.PrintSetup.PrinterName = selectedPrinter; // 设置打印机名称
}
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("请选择模板");
}
}
private void cb_printer_SelectedIndexChanged(object sender, EventArgs e)
{
}
//引入打印机列表数据
private void LoadPrinters()
{
cb_printer.Items.Clear();
foreach (string printer in PrinterSettings.InstalledPrinters)
{
cb_printer.Items.Add(printer);
}
if (cb_printer.Items.Count > 0)
{
cb_printer.SelectedIndex = 0; // 默认选择第一个打印机
}
}
}
}
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();
this.cb_printer = new System.Windows.Forms.ComboBox();
((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);
//
// cb_printer
//
this.cb_printer.FormattingEnabled = true;
this.cb_printer.Location = new System.Drawing.Point(322, 58);
this.cb_printer.Name = "cb_printer";
this.cb_printer.Size = new System.Drawing.Size(249, 23);
this.cb_printer.TabIndex = 13;
this.cb_printer.SelectedIndexChanged += new System.EventHandler(this.cb_printer_SelectedIndexChanged);
//
// 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.cb_printer);
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;
private System.Windows.Forms.ComboBox cb_printer;
}
}