ESC/P 列印指令使用,3種票據列印方法(轉)
原文地址:http://blog.csdn.net/pfworld/archive/2008/02/05/2084666.aspx
具體內容大家自己看!如有好的解決方案大家共同研究!
(1)自定義紙張設定
控制皮膚->印表機和傳真->右鍵->伺服器屬性->建立新的格式
(2)自定義紙張使用
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight);
NewPrint:制定一紙張名稱。 iWidth:紙張使用寬度。 iHeight:紙張使用高度。
iWidth,iHeight 可以在使用過程中調整。
例如:iWidth=923,iHeight=480
(3)ESC/P指令使用
using System;
using System.Runtime.InteropServices;
using System.Data;
using System.IO;
using System.Windows.Forms;
namespace PrintDome
{
class ClsPrintLPT
{
private IntPtr iHandle;
private FileStream fs;
private StreamWriter sw;
private string prnPort = "LPT1"; //印表機埠
public ClsPrintLPT()
{
}
private const uint GENERIC_READ = 0x80000000;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
///
/// 開啟一個vxd(裝置)
///
[DllImport("kernel32.dll", EntryPoint = "CreateFile", CharSet = CharSet.Auto)]
private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);
///
/// 開始連線印表機
///
private bool PrintOpen()
{
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
MessageBox.Show("沒有連線印表機或者印表機埠不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return false;
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
sw = new StreamWriter(fs, System.Text.Encoding.Default); //寫資料
return true;
}
}
///
/// 列印字串
///
/// 要列印的字串
private void PrintLine(string str)
{
sw.WriteLine(str); ;
}
///
/// 關閉列印連線
///
private void PrintEnd()
{
sw.Close();
fs.Close();
}
///
/// 列印票據
///
/// tb_Temp 全部欄位資料集合
/// true:列印成功 false:列印失敗
public bool PrintDataSet(DataSet dsPrint)
{
try
{
if (PrintOpen())
{
PrintLine(" ");
PrintLine("[XXXXXXXXXXXXXXXXXX超市]");
PrintLine("NO : " + dsPrint.Tables[0].Rows[0][1].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][2].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][3].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][4].ToString());
PrintLine("XXXXXX: " + dsPrint.Tables[0].Rows[0][5].ToString());
PrintLine("操 作 員: " + dsPrint.Tables[0].Rows[0][6].ToString() + " " + dsPrint.Tables[0].Rows[0][7].ToString());
PrintLine("-------------------------------------------");
}
PrintEnd();
return true;
}
catch
{
return false;
}
}
///
/// ESC/P 指令
///
/// 0:退紙命令 1:進紙命令 2:換行命令
public void PrintESC(int iSelect)
{
string send;
iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (iHandle.ToInt32() == -1)
{
MessageBox.Show("沒有連線印表機或者印表機埠不是LPT1!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
fs = new FileStream(iHandle, FileAccess.ReadWrite);
}
byte[] buf = new byte[80];
switch (iSelect)
{
case 0:
send = "" + (char)(27) + (char)(64) + (char)(27) + 'j' + (char)(255); //退紙1 255 為半張紙長
send = send + (char)(27) + 'j' + (char)(125); //退紙2
break;
case 1:
send = "" + (char)(27) + (char)(64) + (char)(27) + 'J' + (char)(255); //進紙
break;
case 2:
send = "" + (char)(27) + (char)(64) + (char)(12); //換行
break;
default:
send = "" + (char)(27) + (char)(64) + (char)(12); //換行
break;
}
for (int i = 0; i < send.Length; i++)
{
buf[i] = (byte)send[i];
}
fs.Write(buf, 0, buf.Length);
fs.Close();
}
}
}
---------------------------------------------------------------------------------------------------------------
使用例1(LPT列印):
printLPT.PrintESC(0); //列印前退紙
printLPT.PrintDataSet(dsPrint);
printLPT.PrintESC(1); //列印後進紙
使用例2(水晶報表列印):
this.reportDocument1.Load(Application.StartupPath + "//Temp.rpt");
PageMargins pMaargins;
pMaargins = reportDocument1.PrintOptions.PageMargins;
pMaargins.topMargin = 5;
pMaargins.bottomMargin = 0;
pMaargins.leftMargin = 5;
pMaargins.rightMargin = 0;
reportDocument1.PrintOptions.ApplyPageMargins(pMaargins);
reportDocument1.Refresh();
reportDocument1.SetDataSource(dsPrint);
//reportDocument1.PrintOptions.PrinterName = "Microsoft Office Document Image Writer";
printLPT.PrintESC(0); //列印前退紙
reportDocument1.PrintToPrinter(1, false, 0, 0);
timer1.Enabled = true;
使用例3(printDocument 列印):
this.printDocument1.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight);
printLPT.PrintESC(0); //列印前退紙
this.printDocument1.Print();
-----------------------------------PrintPage()----------------------------------------------------------
int iX;
int iY;
int iTopMargin = 35; //頂邊距
int iLeftMargin = 70;//左邊距
int iButtomMargin = 40;//底邊距
int iMarginX = 25; //文字間距
int iMarginY = 25; //文字行距
int icellTopMargin = 12; //單元格頂邊距
int icellLeftMargin = 12; //單元格左邊距
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Font titleFont = new Font("宋體", 16, FontStyle.Bold);//標題字型
Font fntTxt = new Font("宋體", 11, FontStyle.Regular); //正文文字
Brush brush = new SolidBrush(Color.Black);//畫刷
Pen pen = new Pen(Color.Black); //線條顏色
try
{
string sTitle = "<>";
//string sDataTitle = "No: " + dsPrint.Tables[0].Rows[0][1].ToString() + " " +
// "XXXXX: " + dsPrint.Tables[0].Rows[0][17].ToString() + " " +
// "XXXXX: " + dsPrint.Tables[0].Rows[0][19].ToString() + " " + dsPrint.Tables[0].Rows[0][18].ToString();
string sDataTitle = "No: " + dsPrint.Tables[0].Rows[0][1].ToString() + " " +
"XXXXX: " + dsPrint.Tables[0].Rows[0][17].ToString() + " " +
"XXXXX: " + dsPrint.Tables[0].Rows[0][19].ToString();
int width = e.PageBounds.Width;
int height = e.PageBounds.Height;
//int xoffset = (int)((width - e.Graphics.MeasureString(sTitle, titleFont).Width) / 2);
//int xoffset2 = (int)((width - e.Graphics.MeasureString(sDataTitle, fntTxt).Width) / 2);
//e.Graphics.DrawString(sTitle, titleFont, brush, xoffset, iTopMargin); //標題
//e.Graphics.DrawString(sDataTitle, fntTxt, brush, xoffset2, iTopMargin += iTopMargin + 5); //副標題資料
e.Graphics.DrawString(sTitle, titleFont, brush, iLeftMargin + 140, iTopMargin); //標題
e.Graphics.DrawString(sDataTitle, fntTxt, brush, iLeftMargin + 60, iTopMargin += 35); //副標題資料
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 25 + icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 25;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX, iY + 110)); //最左邊的豎線
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最上邊的豎線
string sCell = "XXXXX: ";
int iCellWidth = (int)((e.Graphics.MeasureString(sCell, fntTxt).Width));
int iCellHeight = (int)((e.Graphics.MeasureString(sCell, fntTxt).Height));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //1
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][2].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //2
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //3
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][3].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //4
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + iCellWidth, iY), new Point(iX, iY + 110)); //5
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][4].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawLine(pen, new Point(iX += 2 * icellLeftMargin + 100, iY), new Point(iX, iY + 110)); //6
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 20 + iCellHeight + 3 * icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 2 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下邊的豎線
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][13].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][14].ToString() + " KG", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][15].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
iMarginX = iLeftMargin + icellLeftMargin;
iMarginY = iTopMargin + 20 + iCellHeight + 6 * icellTopMargin;
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 5 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下邊的豎線
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][8].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][10].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
e.Graphics.DrawString("XXXXX: ", fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + 100, iMarginY));
e.Graphics.DrawString(dsPrint.Tables[0].Rows[0][12].ToString(), fntTxt, brush, new Point(iMarginX += 2 * icellLeftMargin + iCellWidth, iMarginY));
iX = iLeftMargin;
iY = iTopMargin + 20 + iCellHeight + 8 * icellTopMargin;
e.Graphics.DrawLine(pen, new Point(iX, iY), new Point(iX + 670, iY)); //最下邊的豎線
e.Graphics.DrawLine(pen, new Point(0, iY += iButtomMargin), new Point(e.PageBounds.Width, iY)); //最下邊的豎線
}
catch
{
MessageBox.Show(this, "資料庫連線錯誤,列印失敗!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
----------------------------------PrintPage()--------------------------------------
附1:
程式碼 功能 程式碼 功能
LF 換行 ESC m 區域性切割
CR 回車 ESC o 印章
ESC SP 設定右邊界 ESC q 釋放紙
ESC ! 設定列印方式 ESC r 選擇列印顏色
ESC * 設定位對映方式 ESC z 設定或取消兩頁並行列印
ESC @ 初始化印表機 ESC BEL 蜂鳴器ON/OFF
ESC R 選擇國際字元子集 ESC c5 禁止/使能皮膚開關
ESC d 列印及N行進紙 ESC c6 禁止/使能ON-LINE開關
ESC t 選擇字元碼錶 ESC p 產生指定脈衝
ESC l 選擇或取消倒過來的字元ESC V 傳送印表機狀態
ESC c0 選擇列印頁 ESC ~ LED ON/OFF
FF 列印送出單頁 HT 水平TAB
RS 流水TAB ESC % 選擇或取消使用者自定義字符集
ESC 2 選擇行間距為1/6英寸 ESC & 定義使用者自定義字符集
ESC 3 設定行進為最小間距 ESC D 設定TAB位置
ESC < 返回行首 ESC i 全切割
ESC C 設定單頁長度 ESC f 設單頁等待時間
ESC F 選擇或取消單頁退紙區 ESC e 列印病退回N行
ESC J 以最小間距進行列印和進紙 ESC c4 選擇列印紙及檢測器(終止列印)
ESC K 以最小間距進行列印和退紙 ESC c3 選擇紙結束訊號輸出
ESC U 選擇或取消單向列印 ESC c1 選擇行間距
中文模式下的命令
程式碼 功能 程式碼 功能
FS & 選擇中文字元模式 FS – n 設定中文字元下劃線模式開關
FS . 取消中文模式 FS ! n 選擇中文字型
附2:
相關文章
- 門診收費票據列印程式碼,望大家指正
- 使用指令碼列印楊輝三角指令碼
- 列印交易票功能操作手冊
- 列印電子發票調整格式
- 分享 vxe-table 實現列印出貨單、自定義列印單據
- web列印方法Web
- 怎麼做能列印發票的軟體?
- 3D列印(1)3D
- 考研黨列印資料怎麼使用雲列印服務?
- 找出非列印字元方法字元
- ESC快捷鍵使用方法分享
- 簡單票據檢測方法
- Python 6種列印99乘法表的方法詳解!Python
- Python中4種方法實現列印整個Pandas DataFramePython
- 轉換流與列印流
- 從命令列中列印(轉)命令列
- OLO列印盒:把手機放進這個小盒就能3D列印3D
- win7系統下列印ppt時去除列印時間的方法Win7
- 透過裝飾器列印最大值與根據傳入引數進行列印次數
- 使用儲存過程實現分頁列印 (轉)儲存過程
- 在C#裡實現DATAGRID的列印預覽和列印 (轉)C#
- 用FDM列印會遇到的3D列印常見問題詳解3D
- 印表機怎麼取消列印任務 取消印表機列印任務的方法
- Laravel5.6 如何列印 SQL?insert/update/select 列印方法總結LaravelSQL
- 用printer物件列印表格 (轉)物件
- VC列印實踐淺談 (轉)
- vb基礎(列印問題) (轉)
- Web 列印Web
- Java列印Java
- 列印流
- 印萌自助列印系統,如何提升傳統列印店的列印效率?
- ThinkPHP 列印 sql 語句的幾種方式PHPSQL
- 3D列印筆 畫出模型3D模型
- 雲列印軟體免費版在哪?雲列印服務怎麼使用?
- 漫談程式設計師系列:3D列印能列印出程式猿嗎程式設計師3D
- 一學生將海洋入侵物種變成3D列印材料3D
- 在ASP程式中列印Excel報表的新方法 (轉)Excel
- 3D列印技術之切片引擎(3)3D