C#?使用PrintDocument類打印標(biāo)簽的方法
最近做了一個(gè)項(xiàng)目,使用不干膠標(biāo)簽貼在RFID抗金屬標(biāo)簽上,那么就會(huì)出現(xiàn)標(biāo)簽打印的問題,該如何打印呢?后來經(jīng)過網(wǎng)上沖浪發(fā)現(xiàn),其實(shí)打印標(biāo)簽和打印A4紙的方法一樣,只不過就是布局、設(shè)置紙張大小的問題。
本文介紹打印機(jī)初步配置,以及實(shí)現(xiàn)方法。標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼。
首先設(shè)置打印機(jī)紙張大小,紙張高寬度以實(shí)際標(biāo)簽為準(zhǔn),設(shè)置好后可打印測(cè)試頁測(cè)試一下,以ZDesigner GX430t打印機(jī)為例。

創(chuàng)建PrintDocument實(shí)例,以及配置打印機(jī)名稱:
/// <summary>
/// 打印
/// </summary>
private void Myprinter()
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(printDocument_PrintA4Page);
pd.DefaultPageSettings.PrinterSettings.PrinterName = "ZDesigner GX430t"; //打印機(jī)名稱
//pd.DefaultPageSettings.Landscape = true; //設(shè)置橫向打印,不設(shè)置默認(rèn)是縱向的
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.Print();
}
設(shè)置頁面布局,根據(jù)實(shí)際需求進(jìn)行排版
private void printDocument_PrintA4Page(object sender, PrintPageEventArgs e)
{
Font titleFont = new Font("黑體", 11, System.Drawing.FontStyle.Bold);//標(biāo)題字體
Font fntTxt = new Font("宋體", 10, System.Drawing.FontStyle.Regular);//正文文字
Font fntTxt1 = new Font("宋體", 8, 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("標(biāo)題name", titleFont, brush, new System.Drawing.Point(20, 10));
Point[] points111 = { new Point(20, 28), new Point(230,28) };
e.Graphics.DrawLines(pen, points111);
e.Graphics.DrawString("資產(chǎn)編號(hào):", fntTxt, brush, new System.Drawing.Point(20, 31));
e.Graphics.DrawString("123456789123465", fntTxt, brush, new System.Drawing.Point(80, 31));
e.Graphics.DrawString("資產(chǎn)序號(hào):", fntTxt, brush, new System.Drawing.Point(20, 46));
e.Graphics.DrawString("123456789131321", fntTxt, brush, new System.Drawing.Point(80, 46));
e.Graphics.DrawString("底部name", fntTxt1, brush, new System.Drawing.Point(100, 62));
Bitmap bitmap = CreateQRCode("此處為二維碼數(shù)據(jù)");
e.Graphics.DrawImage(bitmap, new System.Drawing.Point(240, 10));
}
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 = 80, //寬度
Height = 80 //高度
};
BarcodeWriter writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
return writer.Write(asset);
}效果圖:

最后附上源碼,里面有zxing.dll
鏈接: https://pan.baidu.com/s/1mWdjSPt282tKVz-B1VJoTA
提取碼: 517j
2019.09.04 補(bǔ)充:
增加一維碼打印
/// <summary>
/// 創(chuàng)建條碼方法
/// </summary>
/// <param name="asset"></param>
/// <returns></returns>
public static Bitmap CreateCode(string asset)
{
// 1.設(shè)置條形碼規(guī)格
EncodingOptions options = new EncodingOptions();
options.Height = 40; // 必須制定高度、寬度
options.Width = 120;
// 2.生成條形碼圖片并保存
BarcodeWriter writer = new BarcodeWriter();
writer.Options = options;
writer.Format = BarcodeFormat.CODE_128; //二維碼編碼
return writer.Write(asset); // 生成圖片
}到此這篇關(guān)于C# 使用PrintDocument類打印標(biāo)簽的文章就介紹到這了,更多相關(guān)C# 打印標(biāo)簽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能
這篇文章主要為大家介紹了使用快捷鍵在Unity中快速鎖定和解鎖Inspector右上角的鎖功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
VS2019下安裝和破解?DevExpress?19.2?插件的詳細(xì)教程
這篇文章主要介紹了VS2019?安裝并破解?DevExpress?19.2?插件的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
C#實(shí)現(xiàn)對(duì)Json字符串處理實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)Json字符串處理,通過一個(gè)json實(shí)例分析了C#進(jìn)行JSON操作的方法,需要的朋友可以參考下2014-09-09

