其他條碼知識 請訪問: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 = 1;
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 * 100)
, (int)(m_pageHeight / 25.4 * 100));
//自定義圖片內容整體上間距/左間距
m_printDoc.OriginAtMargins = true;
m_printDoc.DefaultPageSettings.Margins.Top = (int)(2 / 25.4 * 100);
m_printDoc.DefaultPageSettings.Margins.Left = (int)(2 / 25.4 * 100);
//打印事件
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("宋體", 15), Brushes.Black, 2, 2);
e.Graphics.DrawString("地址:杭州市江干區九堡家苑三區\r\n" +
"電話:0571-87298557\r\n"+
"聯系方式:15158883623"
, new Font("宋體", 10), Brushes.Black, 2, 30);
//創建二維碼--需要引用DLL
DotNetBarcode QRBarcode = new DotNetBarcode();//實例二維碼
QRBarcode.Type = DotNetBarcode.Types.QRCode;
QRBarcode.QRSetTextType = DotNetBarcode.QRTextTypes.Automatic;
QRBarcode.QRWriteBar("杭州允博條碼科技有限公司", 2, 100, 2, e.Graphics);
}
}
}