首页 > 编程语言 >C#调用打印机实现打印

C#调用打印机实现打印

时间:2023-08-04 16:33:12浏览次数:45  
标签:打印机 调用 string C# 打印 AppendLine sb new 模板

一、引用BarcodeStandard.dll

#region BarcodeStandard.dll
/*
*
* 使用说明
需要通过NuGet进行安装BarcodeLib.dll,必不可少
*/

string inputString;

/// <summary>
/// 获取所以打印机驱动名称
/// </summary>
private void getPrintDocumentlist()
{
PrintDocument print = new PrintDocument();
string sDefault = print.PrinterSettings.PrinterName;//默认打印机名
comboBox_drive.Items.Add(sDefault);

comboBox_drive.Text = sDefault;//显示默认驱动名称
foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
{
if (sPrint != sDefault)
{
comboBox_drive.Items.Add(sPrint);
}
}

}
/// <summary>
/// 打印绘制
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Font titleFont = new Font("宋体", 9, FontStyle.Bold);//标题字体

Font fntTxt = new Font("宋体", 9, FontStyle.Regular);//正文文字

Brush brush = new SolidBrush(Color.Black);//画刷

Pen pen = new Pen(Color.Black); //线条颜色

Point po = new Point(10, 10);
try
{
//画String
e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);//打印内容


//画横线
//Point[] point = { new Point(20, 50), new Point(200, 50) };//纵坐标不变
//e.Graphics.DrawLines(pen, point);
//画竖线
//Point[] points1 = { new Point(60, 70), new Point(60, 70 + 40) };//横坐标不变
//e.Graphics.DrawLines(pen, points1);
//画矩形
//e.Graphics.DrawRectangle(pen, 20, 70, 90, 90);
}

catch (Exception ex)
{
MessageBox.Show(this, "打印出错!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}
/// <summary>
/// 获取打印内容
/// </summary>
/// <returns></returns>
public StringBuilder GetPrintSW()
{
StringBuilder sb = new StringBuilder();

string tou = "XXXXXX科技有限公司";

string address = "安徽省合肥市瑶海区";

string saleID = "100010000001"; //单号

string item = "项目";

decimal price = 25.00M;

int count = 5;

decimal total = 0.00M;

decimal fukuan = 500.00M;

sb.AppendLine(" " + tou + " \n");

sb.AppendLine("-----------------------------------------");

sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID);

sb.AppendLine("-----------------------------------------");

sb.AppendLine("项目" + " " + "数量" + " " + "单价" + " " + "小计");

for (int i = 0; i < count; i++)
{
decimal xiaoji = (i + 1) * price;

sb.AppendLine(item + (i + 1) + " " + (i + 1) + " " + price + " " + xiaoji);

total += xiaoji;

}

sb.AppendLine("-----------------------------------------");

sb.AppendLine("数量:" + count + " 合计: " + total);

sb.AppendLine("付款:" + fukuan);

sb.AppendLine("现金找零:" + (fukuan - total));

sb.AppendLine("-----------------------------------------");

sb.AppendLine("地址:" + address + "");

sb.AppendLine("电话:130000000000");

sb.AppendLine("谢谢惠顾欢迎下次光临!");

sb.AppendLine("-----------------------------------------");

return sb;

}


/// <summary>
/// 生成条形码
/// </summary>
/// <param name="content">内容</param>
/// <returns></returns>
public static Image GenerateBarCodeBitmap(string content)
{
using (var barcode = new Barcode()
{
IncludeLabel = true,
Alignment = AlignmentPositions.CENTER,
Width = 250,
Height = 100,
RotateFlipType = RotateFlipType.RotateNoneFlipNone,
BackColor = Color.White,
ForeColor = Color.Black,
})
{
return barcode.Encode(TYPE.CODE128B, content);
}
}
#endregion

二、引用Seagull.BarTender.Print.dll

#region Seagull.BarTender.Print.dll
/// <summary>
/// 打印测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void printbt_Click(object sender, EventArgs e)
{
string qd = comboBox_drive.Text;//下拉列表选择的驱动名称
var printDocument = new PrintDocument();
//指定打印机
printDocument.PrinterSettings.PrinterName = qd;//驱动名称

printDocument.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
try
{

//打印预览
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//ppd.Document = printDocument;
//ppd.ShowDialog();

//打印
printDocument.Print();
}
catch (InvalidPrinterException)
{

}
finally
{
printDocument.Dispose();
}
}
/// <summary>
/// BarTender打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BarTender_Click(object sender, EventArgs e)
{
try

{
//程序中写入引用 using Seagull.BarTender.Print.dll,必不可少;
//安装Bartender后,在安装的根目录或者system32下课可找到对应的dll
#region
Engine btEngine = new Engine();
btEngine.Start();
string lj = AppDomain.CurrentDomain.BaseDirectory + "test.btw"; //test.btw是BT的模板
LabelFormatDocument btFormat = btEngine.Documents.Open(lj);

//对BTW模版相应字段进行赋值
btFormat.SubStrings["name"].Value ="Liming";
btFormat.SubStrings["code"].Value = "1234567890";

//指定打印机名
btFormat.PrintSetup.PrinterName = "WPS 虚拟打印机";

//改变标签打印数份连载
btFormat.PrintSetup.NumberOfSerializedLabels = 1;

//打印份数
btFormat.PrintSetup.IdenticalCopiesOfLabel = 1;
Messages messages;

int waitout = 10000; // 10秒 超时
Result nResult1 = btFormat.Print("标签打印软件", waitout, out messages);
btFormat.PrintSetup.Cache.FlushInterval = CacheFlushInterval.PerSession;

//不保存对打开模板的修改
btFormat.Close(Seagull.BarTender.Print.SaveOptions.DoNotSaveChanges);

//结束打印引擎
btEngine.Stop();
#endregion


}
catch (Exception ex)
{
MessageBox.Show("错误信息: " + ex.Message);
return;
}
}

#endregion

三、引用 Interop.LabelManager2.dll

#region Interop.LabelManager2.dll
/// <summary>
/// 打印功能 CodeSoft
/// </summary>
/// <param name="PrintParam1">打印模板参数值1</param>
/// <param name="PrintParam2">打印模板参数值2</param>
/// <param name="PrintParam3">打印模板参数值3</param>
/// <param name="PrintParam4">打印模板参数值4</param>
/// <returns></returns>
public bool SoftCodePrint(string PrintParam1 = "", string PrintParam2 = "", string PrintParam3 = "", string PrintParam4 = "")
{
bool result = false;
int printNum = 2;//打印份数
try
{
string text = string.Empty;
ApplicationClass labApp = null;
Document doc = null;
string labFileName = AppDomain.CurrentDomain.BaseDirectory + "Template\\" + "Test.Lab";//模板地址
if (!File.Exists(labFileName))
{
throw new Exception("沒有找到标签模版");
}

for (int i = 0; i < printNum; i++)
{
labApp = new ApplicationClass();
labApp.Documents.Open(labFileName, false);// 调用设计好的label文件
doc = labApp.ActiveDocument;

//可通过配置档进行配置打印信息
doc.Variables.FreeVariables.Item("模板变量名称1").Value = PrintParam1;
doc.Variables.FreeVariables.Item("模板变量名称2").Value = PrintParam2;
doc.Variables.FreeVariables.Item("模板变量名称3").Value = PrintParam3;
doc.Variables.FreeVariables.Item("模板变量名称4").Value = PrintParam4;
doc.PrintDocument(1);
}

labApp.Quit();
result = true;
}
catch (Exception ex)
{

}
return result;

}
#endregion

标签:打印机,调用,string,C#,打印,AppendLine,sb,new,模板
From: https://www.cnblogs.com/skynight/p/17606357.html

相关文章

  • k8s部署DataEase1.16.0cluster模式
    1.下载官方helm  chart包下载地址:https://github.com/mfanoffice/dataease-helm/releases,当前最新为1.16.0#下载并解压helmchart包wgethttps://github.com/mfanoffice/dataease-helm/releases/download/1.16.0/dataease-1.16.0.tgztarxfdataease-1.16.0.tgzcddataease......
  • [Javascript] event target and currentTarget
    <Parent><child><button/></child></Parent>functiononClick(event){console.log('target:',event.target)//buttonconsole.log('currentTarget',event.currentTarget)//parent}pa......
  • 使用Locust进行接口性能测试:安装、命令参数解析与示例解读(一)
    “Locust是一款开源的Python性能测试工具,它可以模拟大量并发用户对网站或者其他接口进行压力测试”一、Locust简介与安装1.使用pip安装Locust:pip3installlocust2.通过GitHub克隆项目并安装(推荐Python3):gitclonehttps://github.com/locustio/locustcdlocustpython......
  • Mitsubishi 三菱FXPLC学习之数据处理指令(下)
    本来打算花一篇文章的篇幅来写数据处理指令的,但写着写着发现,一篇文章根本写不完QAQ。上篇文章结束得有点突兀,那这里也再不啰嗦,我们直奔主题吧。01、字交换指令XCH字交换指令,顾名思义,就是将两个字软元件的数据相互交换。从编程手册的截图可以看到,XCH指令可以用于16位和32位......
  • Js中的Function和function
    Js中的Function和function起因最近收到一份渗透测试报告,里面指出了一个xss漏洞。在看报告的过程中,对于payload的生效有一些疑问。于是查询了一些js语法的相关内容,总结一下关于Funtion和funtion的相关知识。最后也列举一下目前常用的xss绕过技巧。生效载核:Function(atob`YWxlcn......
  • C语言学习笔记(七)初识结构体
    初识结构体结构体的声明结构体的基础知识结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量。结构的声明struct标签{ 值; 值; ……}变量列表;例://定义一个结构体类型structStu//struct-结构体关键字Stu-结构体标签structStu-结......
  • 使用Locust进行接口性能测试:Locust and TaskSet类详细分析(二)
    “Locust是一款开源的Python性能测试工具,它可以模拟大量并发用户对网站或者其他接口进行压力测试”一、Locust类详细说明在Locust中,Locust类是整个负载测试工具的核心。它用于创建并发用户场景,模拟用户行为。示例:fromlocustimportLocust,TaskSet,task#每一个Locust类,......
  • 咨询 consulting
    提供一些PPT等资料参考:客户和合作伙伴成功案例|MicrosoftAzure ......
  • win11 xshell 应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。
    安装 最新支持的VisualC++可再发行程序包下载|MicrosoftDocs下载x86版本下载x86版本下载x86版本正常打开。......
  • docker compose 安装Prometheus+granfa
    cd/home/xxxxmkdir-pprometheuschmod777prometheuscdprometheusmkdir-pgrafana_dataprometheus_datachmod777grafana_dataprometheus_datadocker-compose.ymlversion:"3.7"services:node-exporter:image:prom/node-exporter:lat......