最近做了一個項目,使用不干膠標簽貼在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("黑體", 11, System.Drawing.FontStyle.Bold);//標題字體 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("標題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("資產編號:", fntTxt, brush, new System.Drawing.Point(20, 31)); e.Graphics.DrawString("123456789123465", fntTxt, brush, new System.Drawing.Point(80, 31)); e.Graphics.DrawString("資產序號:", 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("此處為二維碼數據"); 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/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 = 40; // 必須制定高度、寬度 options.Width = 120; // 2.生成條形碼圖片並保存 BarcodeWriter writer = new BarcodeWriter(); writer.Options = options; writer.Format = BarcodeFormat.CODE_128; //二維碼編碼 return writer.Write(asset); // 生成圖片 }
