实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace A4打印
{
public partial class Form1 : Form
{
DB db = new DB();
string successMsg = "";
string errMsg = "";
string sql = "";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<String> list = LocalPrinter.GetLocalPrinters(); //获得系统中的打印机列表
foreach (String s in list)
{
comboBox1.Items.Add(s); //将打印机名称添加到下拉框中
}
comboBox1.SelectedIndex = 0;
this.Width = 1250;
this.Height = 750;
}
private void button1_Click(object sender, EventArgs e)
{
string fontName = "隶书";
Bitmap bmp = new Bitmap(1200, 700);
Graphics g = Graphics.FromImage((Image)bmp);
g.Clear(Color.White);
int h = 50;
int w = 1100;
int s = 150;
string[] dres = {
" 宇宙人酒楼预订菜单",
" ",
" 预订餐台:牡丹,桂花,玫瑰,月季",
" 预订日期:二〇二〇年九月三十日 午餐",
" ",
"凉菜",
" 凉拌土鸡|壹贰份",
" 凉拌牛肉|壹份",
" 凉拌耳片|壹份",
"热菜" ,
" 宫保鸡丁|壹份",
" 鱼香肉丝|壹份",
" 木瓜官燕|壹份",
" 红烧带鱼|壹份",
" 清蒸甲鱼|壹份",
" 水煮牛肉|壹份",
" 蒜蓉大虾|壹份",
" 干锅排骨|壹份",
" 爆炒猪肝|壹份",
" 鸿运当头|壹份",
" 蚂蚁上树|壹份",
"汤品",
" 青菜圆子汤|壹份",
" 鱼头豆腐汤|拾贰例",
"小吃",
" 南瓜饼|壹份",
" 绿豆糕|壹份",
"酒水",
" 五粮液|壹瓶",
" 王老吉|拾贰瓶",
" ",
" 合计金额:伍佰伍拾元整"
};
for (int j = 0; j < 80; j )
{
for (int i = 0; i <= dres.Length - 1; i )
{
w -= 30;
string str = dres[i];
string[] arr = str.Split('|');
//上部
if (arr[0].Length > j)
{
str = arr[0][j].ToString();
}
else
{
str = " ";
};
if (dres[i].Length > j)
{
g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
}
else
{
g.DrawString(" ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
};
//下部
if (dres[i].Contains('|'))
{
if (arr[1].Length > j)
{
str = arr[1][j].ToString();
}
else
{
str = " ";
};
if (dres[i].Length > j)
{
g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, 750 - s);
}
else
{
g.DrawString(" ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, 750 - s);
};
};
};
s -= 24;
h = 24;
w = 1100;
};
pictureBox1.Image = bmp;
}
private void button2_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage = new PrintPageEventHandler(printDocument_Print);
pd.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //打印机名称
pd.DefaultPageSettings.Landscape = true; //设置横向打印,不设置默认是纵向的
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.Print();
}
int newRow_s = 0;
int newRow_e = 36;
DataTable dt = null;
private void printDocument_Print(object sender, PrintPageEventArgs e)
{
string fontName = "隶书";
PrintDocument tmp = (PrintDocument)sender;
Graphics g = e.Graphics;
g.Clear(Color.White);
string[] dres = {
" 宇宙人酒楼预订菜单",
" ",
" 预订餐台:牡丹,桂花,玫瑰,月季",
" 预订日期:二〇二〇年九月三十日|午餐",
" ",
"凉菜",
" 凉拌土鸡|壹贰份",
" 凉拌牛肉|壹份",
" 凉拌耳片|壹份",
"热菜" ,
" 宫保鸡丁|壹份",
" 鱼香肉丝|壹份",
" 木瓜官燕|壹份",
" 红烧带鱼|壹份",
" 清蒸甲鱼|壹份",
" 水煮牛肉|壹份",
" 蒜蓉大虾|壹份",
" 干锅排骨|壹份",
" 爆炒猪肝|壹份",
" 鸿运当头|壹份",
" 蚂蚁上树|壹份",
" 干锅排骨|壹份",
" 爆炒猪肝|壹份",
" 鸿运当头|壹份",
" 蚂蚁上树|壹份",
" 干锅排骨|壹份",
" 爆炒猪肝|壹份",
" 鸿运当头|壹份",
" 蚂蚁上树|壹份",
" 干锅排骨|壹份",
" 爆炒猪肝|壹份",
" 鸿运当头|壹份",
" 蚂蚁上树|壹份",
"汤品",
" 青菜圆子汤|壹份",
" 鱼头豆腐汤|拾贰例",
"小吃",
" 南瓜饼|壹份",
" 绿豆糕|壹份",
"酒水",
" 五粮液|壹瓶",
" 王老吉|拾贰瓶",
" ",
" 合计金额:伍佰伍拾元整"
};
int len = dres.Length;
int sLen = newRow_s;
int eLen = newRow_e;
if (len < 36)
{
sLen = 0;
eLen = len;
};
int h = 50;
int w = 1100;
int s = 150;
int zh = 700;
for (int j = 0; j < 30; j )
{
for (int i = sLen; i <= eLen - 1; i )
{
w -= 30;
string str = dres[i];
string[] arr = str.Split('|');
//上部
if (arr[0].Length > j)
{
str = arr[0][j].ToString();
}
else
{
str = " ";
};
if (dres[i].Length > j)
{
g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
}
else
{
g.DrawString(" ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, h);
};
//下部
if (dres[i].Contains('|'))
{
if (arr[1].Length > j)
{
str = arr[1][j].ToString();
}
else
{
str = " ";
};
if (dres[i].Length > j)
{
g.DrawString(str, new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, zh - s);
}
else
{
g.DrawString(" ", new Font(fontName, 18, FontStyle.Bold), Brushes.Black, w, zh - s);
};
};
};
s -= 24;
h = 24;
w = 1100;
};
if (newRow_e< len)
{
e.HasMorePages = true;
newRow_s = 36;
if (newRow_e 36 <= len)
{
newRow_e = 36;
}
else
{
newRow_e = len;
};
}
else
{
e.HasMorePages = false;
newRow_s = 0;
newRow_e = 36;
};
}
private void button3_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage = new PrintPageEventHandler(printDocument_Print1);
pd.DefaultPageSettings.PrinterSettings.PrinterName = comboBox1.Text; //打印机名称
pd.DefaultPageSettings.Landscape = true; //设置横向打印,不设置默认是纵向的
pd.PrintController = new System.Drawing.Printing.StandardPrintController();
pd.Print();
}
private void printDocument_Print1(object sender, PrintPageEventArgs e)
{
string fontName = "隶书";
PrintDocument tmp = (PrintDocument)sender;
Graphics g = e.Graphics;
g.Clear(Color.White);
Pen myPen = new Pen(Color.FromArgb(255, Color.Black), 1.0F);
//原点坐标
int o_w = 0;
int o_h = 0;
//g.DrawLine(myPen, new Point(0, 0), new Point(10, 0));
//g.DrawLine(myPen, new Point(10, 0), new Point(10, 10));
//g.DrawLine(myPen, new Point(0, 0), new Point(0, 10));
//g.DrawLine(myPen, new Point(0, 10), new Point(10, 10));
//标题
string title = "宇宙人酒楼" "宴席单";
//预订桌台
string ths = "预订桌台:" "201(国色天香),202(难忘今宵),203(梅兰竹菊),204(满天星),205(锦水湖畔),206(小鸟依人),207(王者至尊)";
int len = GetByteLength(ths);
int blen = ths.Length;
if (len > 86) {
int xlen = blen * 86 / len;
ths = ths.Substring(0, xlen) "…";
};
//预定人姓名
string ydName = "预订人姓名:" "张三";
string ydZs = "预订桌数:" "2桌";
string ydDate = "预订日期:" "2020年12月01日";
string ydBc = "预订餐次:" "晚餐";
g.DrawString(title, new Font(fontName, 24, FontStyle.Bold), Brushes.Black, 340, o_h);
g.DrawString(ths, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, o_w, 50);
g.DrawString(ydName, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, o_w, 80);
g.DrawString(ydZs, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 240, 80);
g.DrawString(ydDate, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 420, 80);
g.DrawString(ydBc, new Font(fontName, 14, FontStyle.Bold), Brushes.Black, 720, 80);
//边框
g.DrawLine(myPen, new Point(o_w, 110), new Point(900, 110));
g.DrawLine(myPen, new Point(900, 110), new Point(900, 650));
g.DrawLine(myPen, new Point(o_w, 110), new Point(o_w, 650));
g.DrawLine(myPen, new Point(o_w, 650), new Point(900, 650));
g.DrawLine(myPen, new Point(o_w, 560), new Point(900, 560));
//g.DrawLine(myPen, new Point(890, 640), new Point(900, 640));
//g.DrawLine(myPen, new Point(900, 640), new Point(900, 650));
//g.DrawLine(myPen, new Point(890, 650), new Point(900, 650));
//g.DrawLine(myPen, new Point(890, 640), new Point(890, 650));
}
/// <summary>
/// 获取字符所占半角宽度个数
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public int GetByteLength(string str)
{
int j = 0;
for (int i = 0; i < str.Length; i )
{
if ((int)str[i] > 127)
{
j ; j ;
}
else
{
j ;
}
}
return j;
}
private void button4_Click(object sender, EventArgs e)
{
int s1 = GetByteLength("(长)");
int s2 = GetByteLength("(长)");
MessageBox.Show(s1.ToString());
MessageBox.Show(s2.ToString());
}
private void button5_Click(object sender, EventArgs e)
{
string fontName = "隶书";
Bitmap bmp = new Bitmap(1200, 700);
Graphics g = Graphics.FromImage((Image)bmp);
g.Clear(Color.White);
Pen myPen = new Pen(Color.FromArgb(255, Color.Black), 1.0F);
//原点坐标
int o_w = 0;
int o_h = 0;
//标题
string title = "玉金香酒楼" "宴席单";
//预订桌台
string ths = "预订桌台:" "201(国色天香),202(难忘今宵),203(梅兰竹菊),204(满天星),205(锦水湖畔),206(小鸟依人),207(王者至尊)";
int len = GetByteLength(ths);
int blen = ths.Length;
if (len > 86)
{
int xlen = blen * 86 / len;
ths = ths.Substring(0, xlen) "…";
};
//预定人姓名
string ydName = "预订人姓名:" "张三";
//预订桌数
string ydZs = "预订桌数:" "2桌";
//预订日期
string ydDate = "预订日期:" "2020年12月01日";
//预订餐次
string ydBc = "预订餐次:" "晚餐";
//预订菜品
DataTable cp_dt = null;
errMsg = "";
dt = null;
sql = ""
"SELECT "
"rtrim(c.cpmc) cpmc, "
"y.sl, "
"rtrim(c.unit) unit, "
"y.price, "
"y.je, "
"isnull(y.remark,'') cpsm, "
"isnull(x.mc,'未知') cplx,c.no cpbm "
"FROM "
"yy_dzcd y "
"left outer join cpdb c on y.cpbh=c.no "
"left outer join cpxsl x on x.no=c.cpxsl "
"WHERE "
"y.no=" "217" " "
"ORDER BY x.mc ASC; "
"";
if (db.getDataTable(sql, out dt, out errMsg))
{
cp_dt = dt;
}
else
{
MessageBox.Show("错误,获取预订菜品失败!");
return;
};
//获取台面备注
string o_tmbz = "";
successMsg = "";
errMsg = "";
dt = null;
sql = ""
"SELECT "
"rtrim(remark) result "
"FROM "
"yydc "
"WHERE "
"no=" "217" " "
"";
if (db.getTheData(sql, out successMsg, out errMsg))
{
o_tmbz = successMsg;
}
else
{
MessageBox.Show("错误,获取台面备注失败!");
return;
};
int h = 0;
int h_h = 0;
int w_w = 0;
//打印头部
g.DrawString(title, new Font(fontName, 24, FontStyle.Bold), Brushes.Black, 340, o_h h);
h = 50;
g.DrawString(ths, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, o_w, h);
h = 30;
g.DrawString(ydName, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, o_w, h);
g.DrawString(ydZs, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 240, h);
g.DrawString(ydDate, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 420, h);
g.DrawString(ydBc, new Font(fontName, 14, FontStyle.Regular), Brushes.Black, 720, h);
//打印边框
g.DrawLine(myPen, new Point(o_w, 110), new Point(900, 110));
g.DrawLine(myPen, new Point(900, 110), new Point(900, 650));
g.DrawLine(myPen, new Point(o_w, 110), new Point(o_w, 650));
g.DrawLine(myPen, new Point(o_w, 650), new Point(900, 650));
g.DrawLine(myPen, new Point(o_w, 560), new Point(900, 560));
g.DrawLine(myPen, new Point(290, 110), new Point(290, 560));
g.DrawLine(myPen, new Point(580, 110), new Point(580, 560));
//打印菜品
h = 45;
h_h = h;
if (cp_dt.Rows.Count > 0)
{
string old_cplx = "";
foreach (DataRow dr in cp_dt.Rows)
{
string o_cplx = Convert.ToString(dr["cplx"]);
string o_cpmc = Convert.ToString(dr["cpmc"]);
string o_sl = Convert.ToString(dr["sl"]);
string o_unit = Convert.ToString(dr["unit"]);
if (h > 510)
{
h = h_h;
w_w = 290;
};
if (old_cplx != o_cplx)
{
old_cplx = o_cplx;
g.DrawString(old_cplx, new Font(fontName, 15, FontStyle.Bold), Brushes.Black, o_w w_w 15, h);
h = 20;
g.DrawString(" " o_cpmc, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w w_w 15, h);
g.DrawString(o_sl o_unit, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w w_w 230, h);
h = 20;
}
else
{
g.DrawString(" " o_cpmc, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w w_w 15, h);
g.DrawString(o_sl o_unit, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w w_w 230, h);
h = 20;
};
};
};
//打印备注
g.DrawString("备注:", new Font(fontName, 15, FontStyle.Bold), Brushes.Black, o_w 15, 575);
g.DrawString(" " o_tmbz, new Font(fontName, 13, FontStyle.Regular), Brushes.Black, o_w 15, 600);
pictureBox1.Image = bmp;
}
}
}
标签: 打印
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论