C# 使用PrintDocument类打印标签
最近做了一个项目,使用不干胶标签贴在RFID抗金属标签上,那么就会出现标签打印的问题,该如何打印呢?后来经过网上冲浪发现,其实打印标签和打印A4纸的方法一样,只不过就是布局、设置纸张大小的问题。
本文介绍打印机初步配置,以及实现方法。标签主要展示资产基本信息以及二维码。
首先设置打印机纸张大小,纸张高宽度以实际标签为准,设置好后可打印测试页测试一下,以ZDesigner GX430t打印机为例。

创建PrintDocument实例,以及配置打印机名称:
/// <summary>
/// 打印
/// </summary>
private void Myprinter()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page); pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t"; //打印机名称
//pd.DefaultPageSettings.Landscape = true; //设置横向打印,不设置默认是纵向的
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.Print();
}
设置页面布局,根据实际需求进行排版
private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
{
Font titleFont = new Font("黑体", , System.Drawing.FontStyle.Bold);//标题字体
Font fntTxt = new Font("宋体", , System.Drawing.FontStyle.Regular);//正文文字
Font fntTxt1 = new Font("宋体", , System.Drawing.FontStyle.Regular);//正文文字
System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.Black);//画刷
System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black); //线条颜色 try
{
e.Graphics.DrawString("标题name", titleFont, brush, new System.Drawing.Point(, )); Point[] points111 = { new Point(, ), new Point(,) };
e.Graphics.DrawLines(pen, points111); e.Graphics.DrawString("资产编号:", fntTxt, brush, new System.Drawing.Point(, ));
e.Graphics.DrawString("", fntTxt, brush, new System.Drawing.Point(, ));
e.Graphics.DrawString("资产序号:", fntTxt, brush, new System.Drawing.Point(, ));
e.Graphics.DrawString("", fntTxt, brush, new System.Drawing.Point(, )); e.Graphics.DrawString("底部name", fntTxt1, brush, new System.Drawing.Point(, )); Bitmap bitmap = CreateQRCode("此处为二维码数据");
e.Graphics.DrawImage(bitmap, new System.Drawing.Point(, )); }
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
二维码生成方法,我这里使用zxing
/// <summary>
/// 二维码方法
/// </summary>
/// <param name="asset"></param>
/// <returns></returns>
public static Bitmap CreateQRCode(string asset)
{
EncodingOptions options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8", //编码
Width = , //宽度
Height = //高度
};
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
return writer.Write(asset);
}
效果图:

最后附上源码,里面有zxing.dll
链接: https://pan.baidu.com/s/1F2joXgj0gmPrwf4yALC-vQ
提取码: yg4x
2019.09.04 补充:
增加一维码打印
/// <summary>
/// 创建条码方法
/// </summary>
/// <param name="asset"></param>
/// <returns></returns>
public static Bitmap CreateCode(string asset)
{
// 1.设置条形码规格
EncodingOptions options = new EncodingOptions();
options.Height = ; // 必须制定高度、宽度
options.Width = ; // 2.生成条形码图片并保存
BarcodeWriter writer = new BarcodeWriter();
writer.Options = options;
writer.Format = BarcodeFormat.CODE_128; //二维码编码
return writer.Write(asset); // 生成图片
}
C# 使用PrintDocument类打印标签的更多相关文章
- 【转】C#中PrintDocument类详解
		PrintDocument组件是用于完成打印的类,其常用属性.方法和事件如下: 属性DocumentName:字符串类型,记录打印文档时显示的文档名(例如,在打印状态对话框或打印机队列中显示). 方法 ... 
- C# 使用PrintDocument 绘制表格 完成 打印预览
		C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable 经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代 ... 
- C# 使用PrintDocument 绘制表格 完成 打印预览 DataTable
		经过不断的Google与baidu,最终整理出来的打印类 主要是根据两个参考的类组合而成,稍微修改了一下,参考代码及来源见最后(其中一份是VB语言的) 其中遇到的一些问题也已经得到了解决(分页,打印预 ... 
- (转)打印相关_C#(PrintDocument、PrintDialog、PageSetupDialog、PrintPreviewDialog)
		原文地址:http://www.cnblogs.com/smallsoftfox/archive/2012/06/25/2562718.html 参考文章:http://www.cnblogs.com ... 
- Java类的继承与多态特性-入门笔记
		相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ... 
- 吉特仓库管理系统- 斑马打印机 ZPL语言的腐朽和神奇
		上一篇文章说到了.NET中的打印机,在PrintDocument类也暴露一些本质上上的问题,前面也提到过了,虽然使用PrintDcoument打印很方便.对应条码打印机比如斑马等切刀指令,不依赖打印机 ... 
- 基于Win服务的标签打印(模板套打)
		最近做了几个项目,都有在产品贴标的需求 基本就是有个证卡类打印机,然后把产品的信息打印在标签上. 然后通过机器人把标签贴到产品上面 标签信息包括文本,二维码,条形码之类的,要根据对应的数据生成二维码, ... 
- 根据第三方库spire.pdf使用指定打印机打印pdf文件
		private void button1_Click(object sender, EventArgs e) { PdfDocument doc = new PdfDocument(); string ... 
- 在C#程序中实现插件架构
		阅读提示:这篇文章将讲述如何利用C#奇妙的特性,实现插件架构,用插件(plug-ins)机制建立可扩展的解决方案. 在.NET框架下的C#语言,和其他.NET语言一样提供了很多强大的特性和机制.其中一 ... 
随机推荐
- 364. Nested List Weight Sum II 大小反向的括号加权求和
			[抄题]: Given a nested list of integers, return the sum of all integers in the list weighted by their ... 
- [leetcode]34.Find First and Last Position of Element in Sorted Array找区间
			Given an array of integers nums sorted in ascending order, find the starting and ending position of ... 
- tiny4412 --uboot移植(1)
			开发环境:win10 64位 + VMware12 + Ubuntu14.04 32位 工具链:linaro提供的gcc-linaro-6.1.1-2016.08-x86_64_arm-linux-g ... 
- maven 监理web war 项目
- js 原型链与继承
			var A = function(){ this.name="xiaoming"; } A.prototype.age=9; var a = new A(); console.lo ... 
- python基础之Day9
			一.文件操作 1.r+t:可读可写 2.w+t:可写可读 3.a+t:可追加写.可读 4.f.seek(offset,whence) offset代表文件的指针的偏移量,单位是字节byteswhenc ... 
- Python(四) 列表元组
- java28
			1.使用多态的优点 把要创建的多个子类缩减为一个父类接着传入参数,用参数调用子类的方法, 输出时直接调用父类的方法,这时传参传创建的对象 2.多态方法的调用 调用的方法前有static时,会默认调用父 ... 
- springboot 使用maven 打包 报 (请使用 -source 7 或更高版本以启用 diamond 运算符) 错误解决办法
			在使用springboot maven 打包时 报如下错误 (请使用 -source 7 或更高版本以启用 diamond 运算符) pom.xml编译插件 配置如下: <plugin> ... 
- Paper | 帧间相关性 + 压缩视频质量增强(MFQE)
			目录 1. ABSTRACT 2. INTRODUCTION 3. RELATED WORKS 3.1. Quality Enhancement 3.2. Multi-frame Super-reso ... 
