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 </span>= <span style="color:#800000;">"</span><span style="color:#800000;">ZDesigner GX430t</span><span style="color:#800000;">"</span>;       <span style="color:#008000;">//</span><span style="color:#008000;">打印机名称
</span><span style="color:#008000;">//</span><span style="color:#008000;">pd.DefaultPageSettings.Landscape = true;  </span><span style="color:#008000;">//</span><span style="color:#008000;">设置横向打印,不设置默认是纵向的</span>
pd.PrintController = <span style="color:#0000ff;">new</span><span style="color:#000000;"> 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);           //线条颜色         
<span style="color:#0000ff;">try</span><span style="color:#000000;">
{ 
    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">北京汽车技师学院</span><span style="color:#800000;">"</span>, titleFont, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">20</span>, <span style="color:#800080;">10</span><span style="color:#000000;">));

    Point[] points111 </span>= { <span style="color:#0000ff;">new</span> Point(<span style="color:#800080;">20</span>, <span style="color:#800080;">28</span>), <span style="color:#0000ff;">new</span> Point(<span style="color:#800080;">230</span>,<span style="color:#800080;">28</span><span style="color:#000000;">) };
    e.Graphics.DrawLines(pen, points111); 

    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">资产编号:</span><span style="color:#800000;">"</span>, fntTxt, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">20</span>, <span style="color:#800080;">31</span><span style="color:#000000;">));
    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">123456789123465</span><span style="color:#800000;">"</span>, fntTxt, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">80</span>, <span style="color:#800080;">31</span><span style="color:#000000;">));
    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">资产序号:</span><span style="color:#800000;">"</span>, fntTxt, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">20</span>, <span style="color:#800080;">46</span><span style="color:#000000;">));
    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">123456789131321</span><span style="color:#800000;">"</span>, fntTxt, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">80</span>, <span style="color:#800080;">46</span><span style="color:#000000;">));

    e.Graphics.DrawString(</span><span style="color:#800000;">"</span><span style="color:#800000;">北京汽车技师学院</span><span style="color:#800000;">"</span>, fntTxt1, brush, <span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">100</span>, <span style="color:#800080;">62</span><span style="color:#000000;">));

    Bitmap bitmap </span>= CreateQRCode(<span style="color:#800000;">"</span><span style="color:#800000;">此处为二维码数据</span><span style="color:#800000;">"</span><span style="color:#000000;">);
    e.Graphics.DrawImage(bitmap, </span><span style="color:#0000ff;">new</span> System.Drawing.Point(<span style="color:#800080;">240</span>, <span style="color:#800080;">10</span><span style="color:#000000;">));  

}
</span><span style="color:#0000ff;">catch</span><span style="color:#000000;"> (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/1ebIILBJuhI2n02C_l-_gUQ

  密码: w1jc

附另一demo https://gitee.com/daolizhe/WinFormPrinter

猜你喜欢

转载自blog.csdn.net/s_156/article/details/112961164