打印小票,使用的是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().SelectNodes("IP").Item().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().SelectNodes("IP").Item().InnerText), int.Parse(xmld.SelectNodes("ZJY").Item().SelectNodes("port").Item().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("票据",,);//设置纸张的大小
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=,topMargin=; //画标题
Font font=new Font("黑体",);
x=leftMargin+ip.TitleNameX;
y=topMargin;
e.Graphics.DrawString(ip.TitleName,new Font("黑体",),Brushes.Black,x,y,new StringFormat ());
//画第二行 副标题
x = leftMargin + ;
y += font.GetHeight(e.Graphics);//返回此字体的行距
font = new Font("黑体", );
e.Graphics.DrawString("抽血排号凭证", font, Brushes.Black, x, y, new StringFormat()); //画排队号
x = leftMargin;
y += font.GetHeight(e.Graphics)+;
string str = "";
e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat()); //画线
Pen pen = new Pen(Color.Black);
pen.Width = ;
e.Graphics.DrawLine(pen, , , , ); x = leftMargin + ;
y = y + font.GetHeight(e.Graphics)+;
font = new Font("黑体", );
str = "简简";
e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat()); //画你所想
x = leftMargin + ;
y = y + font.GetHeight(e.Graphics) - ;
font = new Font("宋体", );
str = "女";
e.Graphics.DrawString(str, font, Brushes.Black, x, y, new StringFormat()); x = leftMargin + ;
y = y + font.GetHeight(e.Graphics) - +;
font = new Font("宋体", );
str = "";
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("宋体", );
b.LabelFont = font;
b.Height=;
b.Width = ;
image=b.Encode(BarcodeLib.TYPE.CODE128,"");
e.Graphics.DrawImage(image, , , , ); } }
}

}
}

上一篇:javase基础复习攻略《七》


下一篇:洗礼灵魂,修炼python(44)--巩固篇—反射之重新认识hasattr,gettattr,setattr,delattr