首先  using System.Drawing.Printing;
@@@@@@@@这个一定要注意 不然没法用的 要报错 如果觉得对你有帮助 给分吧
以下为参考

//**************************************************************
        //
        //                            打印相关(以下部分)
        //
        //**************************************************************

private  DataTable dtStudent; //存放需打印的学生ID列表
        private int intIndex = 0;     //当前打印序号

//打印所选班级
        private void btnPrintClassroom_Click(object sender, EventArgs e)
        {
            if (clbClassroom.Items.Count == 0) return;  //无班级列表则不能打印

//初始化打印学生数据表
            intIndex = 0;
            dtStudent = new DataTable();
            dtStudent.Columns.Add("Id", typeof(string));
           
            string strClassroomId;
            string strSql;
            OleDbConnection conn = new OleDbConnection(SqlHelper.connStr);
            conn.Open();
            OleDbCommand cmd;
            OleDbDataReader dr;

//若没有复选,则打印选择项
            if (clbClassroom.CheckedItems.Count == 0)
            {
                strSql = "select Id from Student where ClassroomId='" + clbClassroom.SelectedValue.ToString() + "'";
                cmd = new OleDbCommand(strSql, conn);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    DataRow drow = dtStudent.NewRow();
                    string strStudentId = dr["Id"].ToString();
                    StudentInfo si = new StudentInfo(strStudentId);
                    //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印
                    if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked)
                    {
                        drow["Id"] = strStudentId;
                        dtStudent.Rows.Add(drow);
                    }
                }
                dr.Close();
            }

foreach (System.Data.DataRowView item in clbClassroom.CheckedItems)
            {
                strClassroomId = item.Row["Id"].ToString();
                strSql = "select Id from Student where ClassroomId='" + strClassroomId + "'";
                cmd = new OleDbCommand(strSql, conn);
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    DataRow drow = dtStudent.NewRow();
                    string strStudentId = dr["Id"].ToString();
                    StudentInfo si = new StudentInfo(strStudentId);
                    //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印
                    if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked)
                    {
                        drow["Id"] = strStudentId;
                        dtStudent.Rows.Add(drow);
                    }
                }
                dr.Close();
            }
            conn.Close();

//初始化进度条
            pb.Minimum = 0;
            pb.Maximum = dtStudent.Rows.Count;
            pb.Value = 0;
            pb.Step = 1;
            if (dtStudent.Rows.Count > 0)
            {
                printDocument.Print();
            }
            else
            {
                MessageBox.Show("没有满足打印条件的数据!");
            }
        }

//打印所选学生
        private void btnPrintStudent_Click(object sender, EventArgs e)
        {
            if (clbStudent.Items.Count == 0) return;  //无学生列表则不能打印

//初始化打印学生数据表
            intIndex = 0;
            dtStudent = new DataTable();
            dtStudent.Columns.Add("Id", typeof(string));

//若没有复选,则打印选择学生项
            if (clbStudent.CheckedItems.Count == 0)
            {
                DataRow drow = dtStudent.NewRow();
                drow["Id"] = clbStudent.SelectedValue.ToString();
                dtStudent.Rows.Add(drow);
            }

foreach (System.Data.DataRowView item in clbStudent.CheckedItems)
            {
                DataRow drow = dtStudent.NewRow();
                string strStudentId = item["Id"].ToString();
                StudentInfo si = new StudentInfo(strStudentId);
                //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印
                if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked)
                {
                    drow["Id"] = strStudentId;
                    dtStudent.Rows.Add(drow);
                }
            }

//初始化进度条
            pb.Minimum = 0;
            pb.Maximum = dtStudent.Rows.Count;
            pb.Value = 0;
            pb.Step = 1;

if (dtStudent.Rows.Count > 0)
            {
                printDocument.Print();
            }
            else
            {
                MessageBox.Show("没有满足打印条件的数据!");
            }
        }

//打印预览
        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            if (clbStudent.Items.Count == 0) return;  //无学生列表则不能打印

//初始化打印学生数据表
            intIndex = 0;
            dtStudent = new DataTable();
            dtStudent.Columns.Add("Id", typeof(string));

//若没有复选,则打印预览选择学生项
            if (clbStudent.CheckedItems.Count == 0)
            {
                DataRow drow = dtStudent.NewRow();
                drow["Id"] = clbStudent.SelectedValue.ToString();
                dtStudent.Rows.Add(drow);
            }

foreach (System.Data.DataRowView item in clbStudent.CheckedItems)
            {
                DataRow drow = dtStudent.NewRow();
                string strStudentId = item["Id"].ToString();
                StudentInfo si = new StudentInfo(strStudentId);
                //若选择了只打带毕业证号并且有毕业证号 或者 没有选择只打带毕业证号的,则打印
                if (chbOnlyPrintDiplomaNo.Checked && si.DiplomaNo.Trim() != "" || !chbOnlyPrintDiplomaNo.Checked)
                {
                    drow["Id"] = strStudentId;
                    dtStudent.Rows.Add(drow);
                }
            }

if (dtStudent.Rows.Count > 0)
            {
                printPreviewDialog1.Document = printDocument;
                printPreviewDialog1.ShowDialog();
            }
            else
            {
                MessageBox.Show("没有满足打印条件的数据!");
            }

}

//打印设置
        private void btnPrintSetup_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument;
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                printDocument.Print();
            }

}

//设置打印学生成绩内容
        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            lblState.Text = "打印处理中,请等候......[" + intIndex.ToString() + "/" + dtStudent.Rows.Count.ToString() + "]";
            Application.DoEvents();

if (intIndex < dtStudent.Rows.Count)
            {
                string strStudentId = dtStudent.Rows[intIndex]["Id"].ToString();
                printContent(sender, e, strStudentId);
            }

pb.PerformStep();

intIndex++;

if (intIndex < dtStudent.Rows.Count)
            {
                e.HasMorePages = true;
            }
            else
            {
                lblState.Text = "打印完成";
                e.HasMorePages = false;
            }
        }

//打印内容设置函数
        private void printContent(object sender, PrintPageEventArgs e, string StudentId)
        {
            int x, y, w, h, l, t; //坐标,宽、高、左、上
            x = e.MarginBounds.X;
            y = e.MarginBounds.Y;
            w = e.MarginBounds.Width;
            h = e.MarginBounds.Height;
            l = e.MarginBounds.Left;
            t = e.MarginBounds.Top;

int rowSpan = Tools.IsNumeric( cbxRowSpan.Text ); //行间距

int xLeft = x + 20;      //左边距
            int xRight = 760 -20 ;   //右边距

int intRemark = 0; //该生有几科不正常成绩
            //获取学生基本信息
            StudentInfo si = new StudentInfo(StudentId);

//设置Graphics实例
            Graphics g = e.Graphics;
            //画笔普通
            Pen pen = new Pen(Color.Black);
            //画笔重
            Pen penStrong = new Pen(Color.Black,2);

//字体(表头,即大标题)
            Font fontHead = new Font(new FontFamily("宋体"), 17, FontStyle.Bold);
            //字体(小标题,即姓名、班级、落款等)
            Font fontCaption = new Font(new FontFamily("宋体"), 12);
            //字体(小表头,即序号,课程名等)
            Font fontBodyTitle = new Font(new FontFamily("黑体"), 10);
            //字体(正文)
            Font fontBody = new Font(new FontFamily("宋体"), 10);

//表头
            string strHead="XX学院" + si.Jie.ToString() + "届毕业生成绩单";
            g.DrawString(strHead, fontHead, Brushes.Black, xLeft + (xRight-xLeft)/2 -strHead.Length*fontHead.Height /2 + 20, y);

xLeft = xLeft - 30;
            y = y + Convert.ToInt32( fontHead.Height * 2.5);

//姓名,专业班级,制表日期
            g.DrawString("姓名:" + si.StudentName, fontCaption, Brushes.Black, xLeft, y);
            //是否打印毕业证书号 ,同时考虑专业班级的位置对称
            if (chbPrintDiplomaNo.Checked)
            {
                g.DrawString("专业班级:" + si.Classroom, fontCaption, Brushes.Black, xLeft + 140, y);
                if (si.Classroom.Length < 11)
                {
                    g.DrawString("毕业证号:" + si.DiplomaNo, fontCaption, Brushes.Black, xLeft + 390, y);
                }
                else
                {
                    y = y + Convert.ToInt32(fontCaption.Height * 1.1);
                    g.DrawString("毕业证号:" + si.DiplomaNo, fontCaption, Brushes.Black, xLeft+140, y);
                }
            }
            else
            {
                g.DrawString("专业班级:" + si.Classroom, fontCaption, Brushes.Black, xLeft + 330, y);
            }
            y = y + Convert.ToInt32(fontCaption.Height*1.5);

g.DrawLine(penStrong, xLeft, y, xRight, y); //加水平线
            int yStart = y;
            y = y + rowSpan;

g.FillRectangle(Brushes.Gray, xLeft, y-1, xRight-xLeft, fontBodyTitle.Height+2 );
            //左成绩表头
            g.DrawString("序号", fontBodyTitle, Brushes.Black, xLeft, y);
            g.DrawString("科目", fontBodyTitle, Brushes.Black, xLeft + 60, y);
            g.DrawString("成绩", fontBodyTitle, Brushes.Black, xLeft + 245, y);
            //右成绩表头
            g.DrawString("序号", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 , y);
            g.DrawString("科目", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 60, y);
            g.DrawString("成绩", fontBodyTitle, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 245, y);

y =y+ fontBodyTitle.Height +rowSpan   ;
            g.DrawLine(penStrong, xLeft, y, xRight, y);

y = y + rowSpan;

int intNo = 1; //课程序号
            for (int i = 0; i < si.TermCount; i++)
            {
                //g.FillRectangle(Brushes.LightGray, xLeft, y, xRight2, fontBody.Height);
                g.DrawString("第" + (i + 1).ToString() + "学期(" + si.arrTerm[i] + ")", fontBodyTitle, Brushes.Black, xLeft, y);

y = y + fontBodyTitle.Height + rowSpan;

g.DrawLine(pen, xLeft, y, xRight, y); //加水平线

y = y + rowSpan;

DataView dv = si.dtScore.DefaultView;
                dv.RowFilter = "Term='" + si.arrTerm[i] + "'";
                for (int j = 0; j < dv.Count; j++)
                {
                    //第一列成绩
                    if (dv[j]["Score"].ToString()=="" && dv[j]["Remark"].ToString() != "") //取非正常成绩
                    {
                        intRemark ++;
                    }
                    g.DrawString((intNo++).ToString() + "." , fontBody, Brushes.Black, xLeft+5, y);
                    g.DrawString( dv[j]["Course"].ToString() + ":", fontBody, Brushes.Black, xLeft+25, y);
                    g.DrawString(dv[j]["Score"].ToString(), fontBody, Brushes.Black, xLeft + 250, y);
                    //第二列成绩
                    if (++j < dv.Count)
                    {
                        if (dv[j]["Score"].ToString() == "" && dv[j]["Remark"].ToString() != "")
                        {
                            intRemark++;
                        }
                        g.DrawString((intNo++).ToString() + ".", fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2+10, y);
                        g.DrawString(dv[j]["Course"].ToString(), fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2 +10 + 25, y);
                        g.DrawString(dv[j]["Score"].ToString(), fontBody, Brushes.Black, xLeft + (xRight - xLeft) / 2 + 250, y);
                    }
                    y = y + fontBody.Height+rowSpan ;
                }
                if (i < si.TermCount-1)
                {
                    g.DrawLine(pen, xLeft, y, xRight, y); //加水平线
                }
                y = y + rowSpan;
            }
            g.DrawLine(penStrong, xLeft, y, xRight, y); //加水平线

g.DrawLine(penStrong, xLeft, yStart, xLeft, y); //加左竖线
            g.DrawLine(pen, xLeft + (xRight - xLeft) / 2, yStart, xLeft + (xRight - xLeft) / 2, y); //加中竖线
            g.DrawLine(penStrong,  xRight, yStart, xRight, y); //加右竖线

//选择合适的打印页脚的位置
            if (y  < (t + h)) //若剩余空行多,就在之间打印页脚
            {
                y=Convert.ToInt32( y+(t + h-y )*0.5);
                if (intRemark > 0)
                {
                    g.FillRectangle(Brushes.Gray, xLeft + 20, y, fontCaption.Height*2, fontCaption.Height);
                    g.DrawString(intRemark.ToString(), fontCaption, Brushes.Black, xLeft + 20, y);
                }
                g.DrawString("XX学院学生处", fontCaption, Brushes.Black, xLeft + 400, y);
                y = y + 20;
                g.DrawString("制表日期: " + System.DateTime.Now.ToShortDateString(), fontCaption, Brushes.Black, xLeft + 435, y);
            }
            else           //否则在页底打印页脚
            {
                if (intRemark > 0)
                {
                    g.FillRectangle(Brushes.Gray, xLeft + 20, y, fontCaption.Height * 2, fontCaption.Height);
                    g.DrawString(intRemark.ToString(), fontCaption, Brushes.Black, xLeft + 20, y);
                }
                g.DrawString("XX学院学生处", fontCaption, Brushes.Black, xLeft + 200, y + 5);
                g.DrawString("制表日期: " + System.DateTime.Now.ToShortDateString(), fontCaption, Brushes.Black, xLeft + 450, y + 5);
            }
        }

private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutBox aboutBox = new AboutBox();
            aboutBox.ShowDialog(this);
        }

转载于:https://www.cnblogs.com/zhwl/archive/2011/04/09/2010171.html

asp.net 调用打印机 (转)相关推荐

  1. C#实现调用打印机(打印字符串、打印绘图、打印图片),还差打印水晶报表

    C#实现调用打印机(打印字符串.打印绘图.打印图片),还差打印水晶报表 目的:调用打印机的使用 缺陷:打印的对象不全(还差打印水晶报表),不能实现在插件绘图板中画什么打印什么 改进:同缺陷,希望朋友们 ...

  2. Java调用打印机打印PDF文档的两种方法

    最近,由于项目需求,需要增加系统调用打印机打印PDF文档功能.以前在Asp.Net项目中做过套打,但是现在的需求直接文档打印,下面是实现代码调用打印机的两种方法. 1.Java Print Servi ...

  3. php使用NuSoap产生webservice结合WSDL让asp.net调用

    <?php require_once("nusoap-0.9.5/lib/nusoap.php"); //定义服务程序 function Add($a,$b) { retur ...

  4. asp.net调用opencv类库,实现图像处理显示

    asp.net调用opencv类库,实现图像处理显示 ​      原理上来说,通过dll的调用,无论是asp.net还是winform都可以调用opencv及其类库.但是在实现的过程还是有许多细节是 ...

  5. android 打印机打印html,Android下通过wifi调用打印机打印

    Android下通过wifi调用打印机打印 // Code in Activity try { Socket sock = new Socket("192.168.199.245" ...

  6. java打印word_Java jacob调用打印机打印word文档

    前面说了Java如何生成复杂的Word文档,今年记录下Java如何调用打印机打印word文档. 起初用的是自带的PrintJob,但是系统提供的打印机制并不成熟完整.网上的代码也是千篇一律,在我的打印 ...

  7. asp.net调用js方法小结

    asp.net调用js方法小结 key:asp.net js方法互调   asp.net 前台如何调用后台方法 1. javaScript函数中执行C#代码中的函数: 方法一:1.首先建立一个按钮,在 ...

  8. java 调用打印机 api_java 调用打印机API无法打印,但是直接打印可以,请问有人遇到过这样的问题吗?...

    java调用打印机的代码如下:publicstaticvoidmain(String[]args){PrintRequestAttributeSetpras=newHashPrintRequestAt ...

  9. asp.net 调用(引用 Native dll 的)C++/CLI dll 的问题及解决

    asp.net 调用引用 native dll 的 C++/CLI dll 会报错:说是dll或其依赖项找不到.具体原因在于 IIS 只会从系统路径下去查找 native dll,不会查找 Bin 目 ...

最新文章

  1. 编程能力差,90%输在了这点上!CTO:这样学编程 ,更容易成为高手
  2. JS魔法堂:彻底理解0.1 + 0.2 === 0.30000000000000004的背后
  3. 国资委:九月份常州光伏出口大增 新兴市场增幅亮眼
  4. nginx 反向代理跨域访问配置_nginx反向代理配置去除前缀
  5. html删除的标签属性,如何从html标签中删除属性?
  6. 如何让mysql索引更快一点
  7. python 数据结构转换层_python – 具有Maxpooling1D和channel_first的Keras模型
  8. TAOCP-1.2.10_求极大值
  9. 使用Mondrian Virtual OLAP Cube 实现星座模型并在saiku展现分析
  10. Automapper问题记录
  11. Guacamole之本地安装Guacamole(二)
  12. 因子分析在SPSS中的操作过程及结果解读
  13. Java语言的技术平台:JavaSE、JavaEE和JavaME
  14. JSON与XML的区别比较
  15. 舒尔特 Pro ,专业训练注意力专注力
  16. 《人生七年》-------殊途同归
  17. wx.scanCode(Object object)使用详解
  18. java 项目骨架,maven项目中骨架
  19. python爬取京东评论分析_Python爬取京东商品评价(动态网页的爬取)
  20. 训练ChatGPT的必备资源:语料、模型和代码库完全指南

热门文章

  1. multiprocessing python_Python多线程/进程(threading、multiprocessing)知识覆盖详解
  2. java 类成员访问权限_Java类成员访问权限控制知识总结
  3. 关于 Quartz 框架如何引入 Dubbo 服务
  4. java用if判断输入字符_JAVA中如何判断一个输入是数字还是字符串
  5. 免费的网络推广教大家新站点前期优化如何进行?
  6. 网络推广外包之下“真我”徐起表示今年中国区定下进军2000万销量目标
  7. 企业网络推广浅析外包企业网络推广如何有效布局关键词优化?
  8. 网站站外优化工作这四项比较“突出”
  9. map和foreach的区别和应用场景_浅析项目中常用的 VO、DTO、DO、PO的概念、区别和用处...
  10. php编写函数6,编写自己的PHP扩展函数