最近公司规划了个新项目,要求自动打印贴纸,以达到提高生产效率及防呆防错的目的,经过研究琢磨终于实现;遂将代码与步骤整理出来,分享与大家
1.1 先使用BarTender创建btw档案,然后设置btw档案的内容与txt文件关联,这样修改txt文件的内容btw档案的内容就跟着变化
文章有新增内容,请各位小伙伴移步到这里:https://blog.csdn.net/caofuzhou/article/details/125943991
1.2 创建txt档案,档案内容为abc123,与btw档案同处于一个文件夹
1.3 btw档案内容关联txt档案内容
跳过简单、繁琐的步骤,最后选择好栏位,点击“确定”完成关联设置
2.1 接下来是程式代码, 这里采用Win Form项目的方式实现自动打印
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = Application.StartupPath;
string txt = path + "\\param.txt";//txt档案路径
StreamWriter sw=new StreamWriter(txt,false);
sw.WriteLine(textBox2.Text);
string btw= path + "\\demo.btw";//btw档案路径
Process p = null;
//寻找名字为bartend的进程
Process[] Po = Process.GetProcessesByName("bartend");
if (Po.Length > 0)
{
p = Po[0];
p.StartInfo.FileName = "bartend.exe";
//列印btw档案并最小化程序
p.StartInfo.Arguments = $@"/AF={btw} /P /min=SystemTray";
p.EnableRaisingEvents = true;
}
else
{
p = new Process();
p.StartInfo.FileName = "bartend.exe";
p.StartInfo.Arguments = $@"/AF={btw} /P /min=SystemTray";
p.EnableRaisingEvents = true;
}
//列印份数
int pageCount = Convert.ToInt32(textBox2.Text);
for (int i = 0; i < pageCount; i++)
{
p.Start();
}
}
}
}
2.2 程式的界面,点击列印自动打印贴纸
3.1 最后是打印机自动出贴纸