打印小票,使用的是BarcodeLib

打印

  private void Control_Click(object s,EventArgs e)
        {
            if (((Control)s).Name == "button1")
            {
                if(textBox1.Text!="")
                {
                    XmlDocument xldoc = new XmlDocument();
                    xldoc.Load("D:\\Learn\\C#训练\\LikeIFace\\LikeIFace\\IPconfig.xml");
                    xmld.SelectNodes("ZJY").Item(0).SelectNodes("IP").Item(0).InnerText = textBox1.Text;
                    xmld.Save("D:\\Learn\\C#训练\\LikeIFace\\LikeIFace\\IPconfig.xml");
                    textBox2.Text = textBox1.Text;

                }
                ipendpoint = new IPEndPoint(IPAddress.Parse(xmld.SelectNodes("ZJY").Item(0).SelectNodes("IP").Item(0).InnerText), int.Parse(xmld.SelectNodes("ZJY").Item(0).SelectNodes("port").Item(0).InnerText));
                textBox2.Text = ipendpoint.Address.ToString();
                label4.Text = ipendpoint.Port.ToString();
                PrintCommand p = new PrintCommand();
                p.print();



            }

            PrintServer print = new PrintServer();
           
            print.GetPrintQueue("Fax");



        }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Printing;

using Ipconfig;
using System.Windows.Forms;
using System.Data;
namespace LikeIFace
{
    public class PrintCommand
    {
        IpconfigHelper ip = new IpconfigHelper();
        /// <summary>
        /// 打印取号票
        /// </summary>
        /// <param name="send"></param>
        /// <param name="e"></param>
        public void print()
        {


            PrintDocument pd = new PrintDocument();//new能够被打印机使用的对线
            PrintPreviewDialog pa = new PrintPreviewDialog();//打印对话框
            pd.PrinterSettings.PrinterName =ip.PrinterName;//打印机名称
            pd.PrintController=new StandardPrintController();//获取打印机进程的控制器
            pd.PrintPage += DrawPage;
            pd.DefaultPageSettings.PaperSize = new PaperSize("票据",300,300);//设置纸张的大小
            pa.Document = pd;
            pa.ShowDialog();//预览
            if (pd.PrinterSettings.IsValid)
            {
                //pd.Print();
                MessageBox.Show("打印成功");
            }
            else
            {
                MessageBox.Show("打印机连接错误");
            }

        }
        /// <summary>
        /// 画取号票
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void DrawPage(object sender, PrintPageEventArgs e)
        {

            float x,y;
            float leftMargin=0,topMargin=0;
            
            //画标题
            Font font=new Font("黑体",20);
            x=leftMargin+ip.TitleNameX;
            y=topMargin;
            e.Graphics.DrawString(ip.TitleName,new Font("黑体",20),Brushes.Black,x,y,new StringFormat ());
            //画第二行 副标题
            x = leftMargin + 70;
            y += font.GetHeight(e.Graphics);//返回此字体的行距
            font = new Font("黑体", 15);
            e.Graphics.DrawString("抽血排号凭证", font, Brushes.Black, x, y, new StringFormat());

            //画排队号
            x = leftMargin;
            y += font.GetHeight(e.Graphics)+15;
            string str = "777";
            e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat());

            //画线
            Pen pen = new Pen(Color.Black);
            pen.Width = 2;
            e.Graphics.DrawLine(pen, 280, 65, 10, 65);

            x = leftMargin + 10;
            y = y + font.GetHeight(e.Graphics)+20;
            font = new Font("黑体", 20);
            str = "简简";
            e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat());


            //画你所想
            x = leftMargin + 115;
            y = y + font.GetHeight(e.Graphics) - 25;
            font = new Font("宋体", 12);
            str = "";
            e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat());




            x = leftMargin + 110;
            y = y + font.GetHeight(e.Graphics) - 5+20;
            font = new Font("宋体", 12);
            str = "17";
            e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat());


            //画二维码
            Image image;
            BarcodeLib.Barcode b=new BarcodeLib.Barcode();
            b.BackColor=System.Drawing.Color.White;//设置图片背景
            b.ForeColor=System.Drawing.Color.Black;//设置字体颜色
            b.ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
            font = new Font("宋体", 1);
            b.LabelFont = font;
            b.Height=50;
            b.Width = 131;
            image=b.Encode(BarcodeLib.TYPE.CODE128,"20180720");
            e.Graphics.DrawImage(image, 155, 70, 111, 65);

        }
                   

    }
}


}
}

猜你喜欢

转载自www.cnblogs.com/Giayong/p/9342343.html