zoukankan      html  css  js  c++  java
  • 一段简单的打印代码(c#)

      

    代码
     1 //数据流   
     2 System.IO.StringReader sr;
     3       //要打印的字符串
     4         private void redstr()
     5         {
     6             StringBuilder ojbbulider = new StringBuilder();
     7             ojbbulider.Append("\n");
     8             ojbbulider.Append ("测试1");
     9             ojbbulider.Append("测试2");
    10             ojbbulider.Append("------------------------------");
    11             ojbbulider.Append("测试3");
    12             ojbbulider.Append("\n");
    13             sr = new System.IO.StringReader(ojbbulider.ToString());
    14         }
    15         //打印方法
    16         private void button1_Click(object sender, EventArgs e)
    17         {
    18             try
    19             {
    20                 if (printDialog1.ShowDialog() == DialogResult.OK)
    21                 {
    22                     printDocument1.Print();
    23                 }
    24             }
    25             catch (Exception ex)
    26             {
    27                 MessageBox.Show("打印时出错" + ex.Message.ToString().Trim());
    28             }
    29         }
    30 
    31         //打印预览
    32         private void button2_Click(object sender, EventArgs e)
    33         {
    34             try
    35             {
    36                
    37                 printPreviewDialog1.ShowDialog();
    38             }
    39             catch (Exception ex)
    40             {
    41                 MessageBox.Show("预览时出错" + ex.Message.ToString().Trim());
    42             }
    43         }
    44 
    45         //打印设置
    46         private void button3_Click(object sender, EventArgs e)
    47         {
    48             try
    49             {
    50                 pageSetupDialog1.ShowDialog();
    51             }
    52             catch (Exception ex)
    53             {
    54                 MessageBox.Show("设置时出错" + ex.Message.ToString().Trim());
    55             }
    56         }
    57         
    58         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    59         {
    60             Graphics objgraphics = e.Graphics;
    61             e.Graphics.DrawString("测试"new Font(new FontFamily("宋体"), 10F), Brushes.Black, 102);
    62             redstr();
    63             Font ziti = new Font(new FontFamily("宋体"), 10F);
    64             //行高
    65             float h = ziti.GetHeight(objgraphics);
    66             //每一面的行数
    67             float hangshu = (int)(e.MarginBounds.Height/h);
    68             float left = e.MarginBounds.Left;
    69             float top = e.MarginBounds.Top;
    70 
    71             string str = "";
    72             for (int i = 0; i < hangshu; i++)
    73             {
    74                 str = sr.ReadLine();
    75                 if (str == null)
    76                 {
    77                     //没有下一页
    78                     e.HasMorePages = false;
    79                     return;
    80                 }
    81                 float x = left;
    82                 float y = top + h*i;
    83                 objgraphics.DrawString(str, ziti, new SolidBrush(Color.Black), x, y,new StringFormat());
    84             }
    85             //有下一页
    86             e.HasMorePages = true;
    87         }
    88     }

            

  • 相关阅读:
    web自动化测试-D2-学习笔记之二(HTML基础之JS)
    web自动化测试-D2-学习笔记之一(HTML基础之DOM操作)
    web自动化测试-D1-学习笔记之一(HTML 和 CSS基础)
    Selenium-常问面试题
    python自动化测试-D11-学习笔记之一(yaml文件,ddt)
    python习题:封装一个日志模块
    python自动化测试-D10-学习笔记之二(Python–logging模块)
    python习题:unittest参数化-数据从文件或excel中读取
    python自动化测试-D10-学习笔记之一(unittest参数化)
    python自动化测试-D9-学习笔记之二(异常处理)
  • 原文地址:https://www.cnblogs.com/angleSJW/p/1627383.html
Copyright © 2011-2022 走看看