http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5263|回复: 0

[Winform] 一个打印的好例子

[复制链接]
发表于 2012-7-5 11:41:42 | 显示全部楼层 |阅读模式
先拉下面两个控件

然后看一下代码就明白了
每打印一页的时候要使用的事件
[C#] 纯文本查看 复制代码
代码 
 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics graphics = e.Graphics;
            e.Graphics.DrawString("社区便民 服务百姓", new Font("Arial", 13f, FontStyle.Bold), Brushes.Black, 10, 1);
            redstr();

            Font ziti = new Font(new FontFamily("宋体"), 10F, FontStyle.Bold);
            //行高
            float h = ziti.GetHeight(graphics);
            //每一页的行数
            float hangshu = (int)(e.MarginBounds.Height / h); ;
            //在边矩
            //float left = e.MarginBounds.Left;
            float left = 3;
            //float shang = e.MarginBounds.Top;
            float shang = 45;
            string str = "";
            for (int i = 0; i <= hangshu; i++)
            {
                str = sr.ReadLine();
                if (str == null)
                {
                    e.HasMorePages = false;
                    return;
                }
                float x = left;
                float y = shang + i * h;
                graphics.DrawString(str, ziti, new SolidBrush(Color.Black), x, y, new StringFormat());
            }
            e.HasMorePages = true;
        }


打印的内容
[C#] 纯文本查看 复制代码
代码 
 //打印
        private StringReader sr;

        //要打印的数据
        private void redstr()
        {
            StringBuilder builder1 = new StringBuilder();
            builder1.Append("......................");
            builder1.Append("\n");
            builder1.Append("名  称: 中国铁路");
            builder1.Append("\n");
            builder1.Append("车  次: " + txtCheCi.Text.Trim());
            builder1.Append("\n");
            builder1.Append("起始站: " + txtStart.Text.Trim());
            builder1.Append("\n");
            builder1.Append("到达站: " + txtEnd.Text.Trim());
            builder1.Append("\n");
            builder1.Append("席  别: " + cbbxibie.Text.Trim());
            builder1.Append("\n");
            builder1.Append("数  量: " + nudNumber.Value.ToString().Trim() + "张");
            builder1.Append("\n");
            builder1.Append("单  价: " + txtPrice.Text.Trim() + "元");
            builder1.Append("\n");
            string shoufei = user.tran1(objOfficeInfo.ofPara1, nudNumber.Value * Convert.ToDecimal(txtPrice.Text.Trim()));
            builder1.Append("手续费: " + shoufei + "元");
            builder1.Append("\n");
            builder1.Append("总  价: " + (Convert.ToDecimal(shoufei) + nudNumber.Value * Convert.ToDecimal(txtPrice.Text.Trim())).ToString().Trim() + "元");
            builder1.Append("\n");
            builder1.Append("订票时间:");
            builder1.Append("\n");
            builder1.Append(timeStart.Value.ToString().Trim() + "-\n" + timeEnd.Value.ToString().Trim());
            builder1.Append("\n");
            builder1.Append("......................");
            builder1.Append("\n");
            builder1.Append("注:铁路客运车次、车票变化");
            builder1.Append("\n");
            builder1.Append("以铁路客运官方信息为准,此");
            builder1.Append("\n");
            builder1.Append("单据仅作订票凭证。");
            builder1.Append("\n");
            builder1.Append("打印时间:\n" + DateTime.Now.ToString());
            builder1.Append("\n");
            builder1.Append("营业编号:" + objOfficeInfo.ofLogin);
            builder1.Append("\n");
            builder1.Append("营业电话:" + objOfficeInfo.ofPhone);
            builder1.Append("\n");

            string str = objOfficeInfo.ofAddress.ToString();
            string str1 = "";
            if (str.Length > 1)
            {
                str1 += str.Substring(0, 1);
                for (int i = 1; i < str.Length; i++)
                {
                    if (i % 7 != 0)
                    {
                        str1 += str.Substring(i, 1);
                    }
                    else
                    {
                        str1 += str.Substring(i, 1) + "\n";
                    }
                }
            }
            else
            {
                str1 = str;
            }

            builder1.Append("营业地址:" + str1);
            builder1.Append("\n");
            builder1.Append("......................");
            builder1.Append("\n");
            builder1.Append("全国加盟热线:15639808693");
            builder1.Append("\n");
          
            sr = new StringReader(builder1.ToString());
        }

开始打印
[C#] 纯文本查看 复制代码
代码 
                          try
                                {
                                    printDocument1.Print();
                                }
                                catch (Exception)
                                {
                                    MessageBox.Show("打印时出错");
                                }

相关帖子



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-12-19 04:50

© 2014-2021

快速回复 返回顶部 返回列表