文档章节

C# winform小票打印

深圳大道
 深圳大道
发布于 2016/12/29 15:32
字数 1402
阅读 11
收藏 0
(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);

            }
        }

本文转载自:http://blog.csdn.net/smartsmile2012/article/details/22290601

深圳大道
粉丝 3
博文 877
码字总数 0
作品 0
深圳
架构师
私信 提问
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(14)---Winform开发的常用类库(终结篇,CHM文档放送)

俗话说,一个好汉十个帮,众人拾柴火焰高等都说明一个道理,有更多的资源,更丰富的积累,都是助你走向成功,走向顶峰的推动力。 本篇的公用类库的介绍主题是程序开发中常用到的一些辅助类,...

walb呀
2017/12/04
0
0
网页WEB打印控件制作

在WEB系统中,打印的确是比较烦人的问题,如果我们能制作一个属于自己的自定义的打印插件,那么我们在后续自定义打印的时候能随心所欲的控制打印,这样的效果对于程序员来说是非常开心的一件...

CandyNext
2016/06/13
2.8K
6
用CEF3封包浏览器客户端

前提C#开发,其他语言需要沟通 1、 可以换url、icon、名称, 2、 支持打印条码(Lodop)、小票,支持插件(CEF3支持) 3、 支持文件下载,下载弹窗选择保存文件夹 4、 支持弹窗 5、有关闭、最小...

snamper
2016/08/15
11
0
c# winform打印表格中的html富文本格式如何实现

c# winform打印表格中的html富文本格式如何实现,寻求帮助

lfllfl1
2016/09/29
603
0
『水晶报表』使用 水晶报表 实现打印

在我们开发各种工具软件的时候,我们不可避免的会遇到打印的问题。而使用.NET开发打印功能,水晶报表或许会是一个十分不象错的选择! 一般步骤: 准备好想要打印的数据源; 制作用于规定打印...

灰大羊
2016/07/05
223
0

没有更多内容

加载失败,请刷新页面

加载更多

kibana汉化

kibana5 / 6 需要下载补丁包,https://github.com/anbai-inc/Kibana_Hanization 其中 v6 版本原生支持国际化,只需要添加资源文件并且配置即可 kibana7 v7版本官方内置汉化资源,在配置文件 ...

细肉云吞
7分钟前
0
0
spring boot 自定义日志 log4j2

使用默认的日志在实际开发中会存在很多问题,比如备份文件名称无法自动重命名、各个等级的日志被放在一个文件中等,所以实际开发中为了更好满足我们的需求,我们一般都会自定义采用配置的方式...

雷开你的门
11分钟前
1
0
PCB设计-Allegro软件入门系列-设计参数配置(上)

前言 经历了导入网表,和放置器件后,我们就要画板子了,但是必要的设计参数也要先准备好,磨刀不误砍柴工。 《一》显示参数 这里主要设置DRC报错标志大小和飞线显示类型 (1)DRC标志可以适当...

demyar
12分钟前
2
0
js实现微博、微信分享

html <!-- 分享 --><div class="share-box"> <b style="vertical-align: middle;">分享到:</b> <a title="分享到新浪微博" class="shareSina"><span class="share-icon"></span><......

张兴华ZHero
28分钟前
2
0
创龙TMS320DM8168浮点DSP C674x + ARM Cortex-A8的CPU、NAND FLASH、NOR FLASH

TL6678-EasyEVM是广州创龙基于SOM-TL6678核心板而研发的一款多核高性能DSP开发板。开发板采用核心板+底板方式,底板采用沉金无铅工艺的四层板设计,尺寸为200mm*106.65mm,它为用户提供了SOM...

Tronlong创龙
31分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部