C#控制条码打印机 纸张大小,间距,绘制内容(所有条码打印机通用)

时间:2021-07-30 06:14:43
其他条码知识 请访问:http://www.ybtiaoma.com ,本文仅供参考,请勿转载,谢谢

using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows.Forms; /*2014-3-1
* 测试打印机 新北洋BTP-2100E Plus 西铁城CLP-S631
* 测试结果:页面大小,上间距,左间距都可以正常使用
*/ namespace PrintingExample
{
public partial class frnPrinterLabel : Form
{
public frnPrinterLabel()
{
InitializeComponent();
} private PrintDocument m_printDoc = null;//打印文档
private PrintPreviewDialog m_printPreview = null;//打印预览UI
private PrinterSettings psetting = new PrinterSettings();//实例打印设置对象 private float m_pageWidth = 70F;//纸张宽度 mm单位
private float m_pageHeight = 50F;//纸张高度 mm单位 private void Form1_Load(object sender, EventArgs e)
{
m_printDoc = new PrintDocument();//实例打印文档对象
m_printPreview = new PrintPreviewDialog(); //m_printPreview.PrintPreviewControl.AutoZoom = false;
m_printPreview.PrintPreviewControl.Zoom = ;
m_printPreview.Width = Screen.PrimaryScreen.Bounds.Width;
m_printPreview.Height = Screen.PrimaryScreen.Bounds.Height; //自定义纸张大小
m_printDoc.DefaultPageSettings.PaperSize = new PaperSize("newPage70X40"
, (int)(m_pageWidth / 25.4 * )
, (int)(m_pageHeight / 25.4 * ));
//自定义图片内容整体上间距/左间距
m_printDoc.OriginAtMargins = true;
m_printDoc.DefaultPageSettings.Margins.Top = (int)( / 25.4 * );
m_printDoc.DefaultPageSettings.Margins.Left = (int)( / 25.4 * );
//打印事件
m_printDoc.PrintPage += new PrintPageEventHandler(m_printDoc_PrintPage); } private void button1_Click(object sender, EventArgs e)
{
m_printDoc.Print();//打印
} private void btnView_Click_1(object sender, EventArgs e)
{
m_printPreview.Document = m_printDoc;//把打印文档显示到预览对话框中
m_printPreview.ShowDialog();
} /// <summary>
/// 绘制需要打印的内容
/// </summary>
void m_printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
       //创建文本信息
e.Graphics.DrawString("杭州允博条码科技有限公司", new Font("宋体", ), Brushes.Black, , ); e.Graphics.DrawString("地址:杭州市江干区九堡家苑三区\r\n" +
"电话:0571-87298557\r\n"+
"联系方式:15158883623" , new Font("宋体", ), Brushes.Black, , );
//创建二维码--需要引用DLL
       DotNetBarcode QRBarcode = new DotNetBarcode();//实例二维码

       QRBarcode.Type = DotNetBarcode.Types.QRCode;
            QRBarcode.QRSetTextType = DotNetBarcode.QRTextTypes.Automatic;

            QRBarcode.QRWriteBar("杭州允博条码科技有限公司", , , , e.Graphics);
} }
}