ESC/P 打印指令使用,3种票据打印方法。

news/2024/11/30 1:28:16/

具体内容大家自己看!如有好的解决方案大家共同研究!

(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;

        /// <summary>
        /// 打开一个vxd(设备)
        /// </summary>
        [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);

        /// <summary>
        /// 开始连接打印机
        /// </summary>
        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;
            }
        }

        /// <summary>
        /// 打印字符串
        /// </summary>
        /// <param name="str">要打印的字符串</param>
        private void PrintLine(string str)
        {
            sw.WriteLine(str); ;
        }

        /// <summary>
        /// 关闭打印连接
        /// </summary>
        private void PrintEnd()
        {
            sw.Close();
            fs.Close();
        }

        /// <summary>
        /// 打印票据
        /// </summary>
        /// <param name="ds">tb_Temp 全部字段数据集合</param>
        /// <returns>true:打印成功 false:打印失败</returns>
        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;
            }
        }

        /// <summary>
        /// ESC/P 指令
        /// </summary>
        /// <param name="iSelect">0:退纸命令 1:进纸命令 2:换行命令</param>
        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 = "<<XXXXXXXXXXXXXXXXXXXXXXXXX>>";

                //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:

 


http://www.ppmy.cn/news/382113.html

相关文章

永和打印发票

目录: day01:业务需求开发环境 day02:HTML网页制作CSS样式表二维码 day03:动态技术J2ee ServletJSP day04:mysql数据库sqlyog的使用 day05: PowerDesigner的使用Jdbc day06:日期格式的转换和统一 day07:项目开发设计思路 Database:永和大王数据 FAQ:没有在WEB-INF\cla…

数电票是否打印,看这一篇就够了~

近日&#xff0c;财政部会计司发布了《关于公布电子凭证会计数据标准&#xff08;试行版&#xff09;的通知》&#xff0c;为做好电子凭证会计数据标准深化试点工作&#xff0c;研究制定了9类电子凭证的会计数据标准。在通知的《电子凭证会计数据标准——全面数字化的电子发票&…

[票据打印]打印走纸控制(1)

本方法适用于几乎所有打印机.而且不需要编程&#xff0e;从控制面板中打开&#xff02;传真和打印机&#xff02;的文件夹&#xff0c;不要点选任何打印机&#xff0e;1&#xff0e;在这个界面上的菜单栏上&#xff0c;点选&#xff02;文件&#xff02;,新建一个纸型&#xff…

关于报表打印

1 分页策略 分页与打印时密切相关的&#xff0c;皕杰报表提供了四种分页策略&#xff0c;即按纸张大小分页、按数据行数分页、按数据列数分页、用户自定义分页和不分页。分页由2个因素来控制&#xff0c;一个每个页面的大小&#xff0c;另外一个是分页顺序&#xff08;打印顺序…

iOS小技能:蓝牙打印商品价签和交易小票的模版,实现自动连接最近使用的打印机 (针对佳博GP-2120TU型号为例子进行展开) 【包含完整demo源码】

文章目录 前言I 、获取标签打印命令1.1 解决人民币符号乱码的问题II、获取票据打印命令2.1 门店票据:使用字符串格式化进行排版2.2 避免乱码问题,推荐使用%n@ 进行格式化,而非%ns2.3 自动实现%ns 自动补齐空格的功能III、 实现自动连接最近使用的打印机IV、常见问题4.1 iOS蓝…

关于电子发票打印报销最优美的姿势——发票大师网页版

目录 关于电子发票打印报销最优美的姿势关于电子发票发票大师网页版发票合并功能发票抽取功能页面整理 简单好用&#xff0c;就是发票大师 关于电子发票打印报销最优美的姿势 关于电子发票 支付电子化、办公无纸化&#xff0c;这些都成为电子发票普及的基础。从一开始&#x…

票据业务-银票、商票

一、商业汇票简介 商业汇票简介 商业汇票有时简称票据或汇票&#xff0c;但实际上&#xff0c;票据是指各种有价证券和凭证包括汇票、 本票和支票&#xff08; 注&#xff1a;本课件说到的票据就特指商业汇票 &#xff09; 本票属于银行自付证券&#xff0c;而支票、汇票属于…

票据打印机打印位图

[b] 使用POS命令中的位图打印命令打印位图。有使用8点列印和24点列印模式。 接口&#xff1a; [/b] public static byte[] imagePixelToPosByte_8(String filePath, int m)throws Exception;public static byte[] imagePixelToPosByte_8(File bmpFile, int m)throws Excepti…