winform打印和预览

  • 调用代码
  • 源码
    • 此文章转载自上善卍若水[winform打印和预览](https://blog.csdn.net/mixiu888/article/details/80916130)

在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .Net Framework的打印功能都以组件的方式提供,为程序员提供了很大的方便。由于工作中常用到印功功能,个人写了一个专门打印DataGridView对象一个类,可以实现预览和打印功能,而且自动缩放字段、添加颜色;在预览时可以根据不同的比例查看效果,可以查看第几页数据和直接打印第几页的 数据。请看效果图。

调用代码

if (MessageBox.Show("提交完成,是否打印", "【温馨提示】", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK){Print(this.dgvData, applyName, "");}

源码

        static DataGridView dgv;static string titleName = ""; //标题名称       static string titleName2 = ""; //第二标题名称     static int rowIndex = 0;   //当前行       static int page = 1; //当前页      static int rowsPerPage = 0;  //每页显示多少行public static void Print(DataGridView dataGridView, string title, string title2){try{if (dataGridView == null) { return; }titleName = title;titleName2 = title2;dgv = dataGridView;PrintPreviewDialog ppvw = new PrintPreviewDialog();ppvw.PrintPreviewControl.Zoom = 1.0; //显示比例为100%PrintDocument printDoc = new PrintDocument();PrintDialog MyDlg = new PrintDialog();MyDlg.Document = printDoc;printDoc.DefaultPageSettings.PaperSize = new PaperSize("A4", 850, 1000);printDoc.DefaultPageSettings.Margins = new Margins(60, 60, 60, 60); //设置边距             ppvw.Document = printDoc;   //设置要打印的文档               ((Form)ppvw).WindowState = FormWindowState.Maximized; //最大化               rowIndex = 0; //当前行              page = 1;  //当前页                             printDoc.PrintPage += new PrintPageEventHandler(printDoc_PrintPage); //打印事件 printDoc.EndPrint += new PrintEventHandler(printDoc_EndPrint);ppvw.Document.DefaultPageSettings.Landscape = true;    // 设置打印为横向               ppvw.ShowDialog(); //打开预览}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}private static void printDoc_PrintPage(object sender, PrintPageEventArgs e){//标题字体Font titleFont = new Font("微软雅黑", 16, FontStyle.Bold);//标题尺寸SizeF titleSize = e.Graphics.MeasureString(titleName, titleFont, e.MarginBounds.Width);//x坐标int x = e.MarginBounds.Left;//y坐标int y = Convert.ToInt32(e.MarginBounds.Top - titleSize.Height);//边距以内纸张宽度int pagerWidth = e.MarginBounds.Width;//画标题e.Graphics.DrawString(titleName, titleFont, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2, y);y += (int)titleSize.Height;if (titleName2 != null && titleName2 != ""){//画第二标题e.Graphics.DrawString(titleName2, dgv.Font, Brushes.Black, x + (pagerWidth - titleSize.Width) / 2 + 200, y);//第二标题尺寸SizeF titleSize2 = e.Graphics.MeasureString(titleName2, dgv.Font, e.MarginBounds.Width);y += (int)titleSize2.Height;}//表头高度int headerHeight = 0;//纵轴上 内容与线的距离int padding = 6;//所有显示列的宽度int columnsWidth = 0;//计算所有显示列的宽度foreach (DataGridViewColumn column in dgv.Columns){//隐藏列返回if (!column.Visible) continue;//所有显示列的宽度columnsWidth += column.Width;}//计算表头高度foreach (DataGridViewColumn column in dgv.Columns){//列宽int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));//表头高度int temp = (int)e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height + 2 * padding;if (temp > headerHeight) headerHeight = temp;}//画表头foreach (DataGridViewColumn column in dgv.Columns){//隐藏列返回if (!column.Visible) continue;//列宽int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));//内容居中要加的宽度float cenderWidth = (columnWidth - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Width) / 2;if (cenderWidth < 0) cenderWidth = 0;//内容居中要加的高度float cenderHeight = (headerHeight + padding - e.Graphics.MeasureString(column.HeaderText, column.InheritedStyle.Font, columnWidth).Height) / 2;if (cenderHeight < 0) cenderHeight = 0;//画背景e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), new Rectangle(x, y, columnWidth, headerHeight));//画边框e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x, y, columnWidth, headerHeight));画上边线//e.Graphics.DrawLine(Pens.Black, x, y, x + columnWidth, y);画下边线//e.Graphics.DrawLine(Pens.Black, x, y + headerHeight, x + columnWidth, y + headerHeight);画右边线//e.Graphics.DrawLine(Pens.Black, x + columnWidth, y, x + columnWidth, y + headerHeight);//if (x == e.MarginBounds.Left)//{//    //画左边线//    e.Graphics.DrawLine(Pens.Black, x, y, x, y + headerHeight);//}//画内容e.Graphics.DrawString(column.HeaderText, column.InheritedStyle.Font, new SolidBrush(column.InheritedStyle.ForeColor), new RectangleF(x + cenderWidth, y + cenderHeight, columnWidth, headerHeight));x += columnWidth;}x = e.MarginBounds.Left;y += headerHeight;while (rowIndex < dgv.Rows.Count){DataGridViewRow row = dgv.Rows[rowIndex];if (row.Visible){int rowHeight = 0;foreach (DataGridViewCell cell in row.Cells){DataGridViewColumn column = dgv.Columns[cell.ColumnIndex];if (!column.Visible || cell.Value == null) continue;int tmpWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));int temp = (int)e.Graphics.MeasureString(cell.Value.ToString(), column.InheritedStyle.Font, tmpWidth).Height + 2 * padding;if (temp > rowHeight) rowHeight = temp;}foreach (DataGridViewCell cell in row.Cells){DataGridViewColumn column = dgv.Columns[cell.ColumnIndex];if (!column.Visible) continue;int columnWidth = (int)(Math.Floor((double)column.Width / (double)columnsWidth * (double)pagerWidth));e.Graphics.DrawRectangle(Pens.Black, new Rectangle(x, y, columnWidth, rowHeight));if (cell.Value != null){//内容居中要加的宽度float cenderWidth = (columnWidth - e.Graphics.MeasureString(cell.Value.ToString(), cell.InheritedStyle.Font, columnWidth).Width) / 2;if (cenderWidth < 0) cenderWidth = 0;//内容居中要加的高度float cenderHeight = (rowHeight + padding - e.Graphics.MeasureString(cell.Value.ToString(), cell.InheritedStyle.Font, columnWidth).Height) / 2;if (cenderHeight < 0) cenderHeight = 0;画下边线//e.Graphics.DrawLine(Pens.Black, x, y + rowHeight, x + columnWidth, y + rowHeight);画右边线//e.Graphics.DrawLine(Pens.Black, x + columnWidth, y, x + columnWidth, y + rowHeight);//if (x == e.MarginBounds.Left)//{//    //画左边线//    e.Graphics.DrawLine(Pens.Black, x, y, x, y + rowHeight);//}//画内容e.Graphics.DrawString(cell.Value.ToString(), column.InheritedStyle.Font, new SolidBrush(cell.InheritedStyle.ForeColor), new RectangleF(x + cenderWidth, y + cenderHeight, columnWidth, rowHeight));}x += columnWidth;}x = e.MarginBounds.Left;y += rowHeight;if (page == 1) rowsPerPage++;//打印下一页if (y + rowHeight > e.MarginBounds.Bottom){e.HasMorePages = true;break;}}rowIndex++;}//页脚string footer = " 第 " + page + " 页,共 " + Math.Ceiling(((double)dgv.Rows.Count / rowsPerPage)).ToString() + " 页";//画页脚e.Graphics.DrawString(footer, dgv.Font, Brushes.Black, x + (pagerWidth - e.Graphics.MeasureString(footer, dgv.Font).Width) / 2, e.MarginBounds.Bottom);page++;}static void printDoc_EndPrint(object sender, PrintEventArgs e){rowIndex = 0; //当前行          page = 1;  //当前页            rowsPerPage = 0;//每页显示多少行}

此文章转载自上善卍若水winform打印和预览

winform打印和预览相关推荐

  1. ABAP 如何判断调用smartforms时是进行打印还是预览

    原文地址:ABAP 如何判断调用smartforms时是进行打印还是预览作者:Lemon SAP在调用smartforms 时,在打印时有直接打印和预览两个选择,有时候需要区别用户的操作是打印还是预览 ...

  2. PrintDocument打印、预览、打印机设置和打印属性的方法(较完整)

    C# 中打印.预览.打印机设置和打印属性的方法http://www.veryhuo.com 2011-10-08 烈火学院 投递稿件 我有话说private void Form1_Load(objec ...

  3. delphi 获取打印机默认纸张_在DELPHI中实现打印的预览

    在DELPHI中实现打印的预览 PCPOP.COM 2005年10月18日 类型:转载 作者:<电脑报> 编辑:王琛 ----------------------------------- ...

  4. JavaScript 页面打印,预览,设置,分页

    在HTML页中加载打印对象 <object id="WebBrowser" width="0" height="0" classid= ...

  5. winform界面嵌入dwg图纸_WPF中使用WinForm控件预览DWG文件(学习笔记)

    操作环境:XP,C# ,.Net4.0和VS2010中测试 WinForm中使用DWGThumbnail不用这么麻烦,下面讨论的是在WPF中使用,WPF中无法直接引用DWGThumbnail.ocx来 ...

  6. JavaScript打印和预览等

    JavaScript打印和预览等 1.JavaScript局部打印 <input id="btnPrint" type="button" value=&q ...

  7. 在web html页面中,打印、预览当前页面

    最近的项目需要简单的实现一下打印当前页面的数据,有分页打印的功能.下面的一段代码可以帮助我们实现简单的打印功能,要想实现复杂的打印那需要利用其他的打印控件.这里就不提了! <html> & ...

  8. 打印图片预览时图片显示不出来_办公小技巧:深入挖掘实用的Excel打印秘诀

    我们在打印Excel表格的时候,经常会遇到一些意想不到的问题,比如表格一页多一点,想打印在一页纸上,这就需要调整.还有就是一个大型数据表,只需打印其中的一部分,此时就需要将这些数据进行分离等等--其实 ...

  9. 谷歌浏览器直接启动打印不预览解决方案

    1. 创建一个谷歌浏览器的快捷方式 2. 右键快捷方式,点击[属性],点击[起始位置],在[目标]尾部位置 添加 " --kiosk-printing" 注意空格!!! 重点来了 ...

最新文章

  1. 查看linux文件的权限:ls -l 文件名称
  2. 《软件加密与解密》第三版学习日志二
  3. 三级计算机系统是什么情况,三级PC技术: 计算机的组成和分类
  4. 使用java命令运行class文件提示“错误:找不到或无法加载主类“的问题分析
  5. (七) DockerUI与Shipyard以及InfluxDB+cAdvisor+Grafana配置监控...
  6. Reddit程序员的酒后真言
  7. 如何实现一平台多系统_自动化设备数据采集系统如何实现
  8. 树莓派VI命令大全(附vim使用异常,卸载重新安装步骤)
  9. 是什么意思网络语_互联网推广是什么意思?新手网络推广怎么干?
  10. c语言单片机避障小车应用,51单片机控制寻迹避障小车各种源程序(功能很多)
  11. 中国大学MOOC(慕课) 一个不错的学习网站
  12. QtSQL的使用心得
  13. MATLAB 读取和显示 bin 文件数据
  14. Codeforeces——69A Young Physicist
  15. 【Linux】Linux 磁盘与文件系统管理命令
  16. 霹雳灯双灯c语言程序,单片机霹雳游侠灯源程序
  17. 仿微信联系人索引列表ListView
  18. Java实现自动输入账号密码登陆软件
  19. 545day(jquery-ajax-event.html)
  20. 牛客OI周赛7:小睿睿的询问【ST表】

热门文章

  1. 服务器配置怎么选择硬件配置
  2. 对计算机学校的意见和建议,对学校管理的意见和建议范文
  3. 按计算机病毒工作原理可将其分为,计算机基础知识练习题3.doc
  4. 有什么做GIF的软件?这3款APP超酷炫
  5. Python 将英语单词列表,转换为听写使用的MP3格式(每个单词朗读两遍)
  6. 中标麒麟安装(学习)
  7. IDEA常用快捷键和谷歌浏览器快捷键Mac版
  8. 鱼c笔记——Python 的 GUI 编程(一):接触 Tkinter
  9. 完美恢复被Ghost覆盖的硬盘数据
  10. 在Excel中画条形图和折线图的二组合图!