C#中进行文本打印的功能

来源:互联网 发布:淘宝购物最便宜的软件 编辑:程序博客网 时间:2024/05/22 09:27
问题描述:      
 
一个记事本程序,要求能按标准打印其中的文档,包括在每行文字数目上进行控制等、
 
解决方法:
一、搞清楚打印的过程:     
  1、定义PrintDocument类,并且声明其PrintPage事件。
  1. private void PrintDocument()
  2.         {
  3.             printDocument = new PrintDocument();
  4.             printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
  5.         }
2、实现PrintPage事件(关键):
  1. /**//// <summary>
  2.         /// 用来处理打印过程的核心事件。
  3.         /// </summary>
  4.         /// <param name=&quot;sender&quot;></param>
  5.         /// <param name=&quot;e&quot;></param>
  6.         /// <remarks></remarks>
  7.         void printDocument_PrintPage(object sender, PrintPageEventArgs e)
  8.         {
  9.             //StringReader lineReader = new StringReader(textBox.Text);
  10.             Graphics graphic = e.Graphics;//获取绘图对象
  11.             float linesPerPage = 0;//页面行号
  12.             float yPosition = 0;//绘制字符串的纵向位置
  13.             float leftMargin = e.MarginBounds.Left;//左边距
  14.             float topMargin = e.MarginBounds.Top;//上边距
  15.             string line = string.Empty;//读取的行字符串
  16.             int currentPageLine=0;//当前页读取的行数
  17.             Font charFont = richTextBox1.Font;//获取打印字体
  18.             SolidBrush brush = new SolidBrush(Color.Black);//刷子
  19.             linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
  20.             //countNum记录全局行数,currentPageLine记录当前打印页行数。
  21.             while (countNum < richTextBox1.Lines.Length)
  22.                 {
  23.                     if (currentPageLine < linesPerPage)
  24.                     {
  25.                         line = richTextBox1.Lines[countNum].ToString();
  26.                         if (line.Length <= 50)
  27.                         {
  28.                             yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
  29.                             //绘制当前行
  30.                             graphic.DrawString(line, charFont, brush, leftMargin, yPosition, new StringFormat());
  31.                             countNum++;
  32.                             currentPageLine++;
  33.                         }
  34.                         else
  35.                         {
  36.                             //拆分后的行数
  37.                             int moreLine = line.Length / 50 + 1;
  38.                             string tempLine;
  39.                             for (int i = 0; i < moreLine; i++)
  40.                             {
  41.                                 //获得当前行的子串
  42.                                 if ((line.Length - i * 50) >= 50)
  43.                                 {
  44.                                      tempLine = line.Substring(i * 50, 50);
  45.                                 }
  46.                                 else
  47.                                 {
  48.                                     tempLine = line.Substring(i * 50, line.Length - i * 50);
  49.                                 }
  50.                                 yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
  51.                                 //绘制当前行
  52.                                 graphic.DrawString(tempLine, charFont, brush, leftMargin, yPosition, new StringFormat());
  53.                                 //当前打印页行数加1
  54.                                 currentPageLine++;
  55.                             }
  56.                             //总行数加1
  57.                             countNum++;
  58.                         }
  59.                     }
  60.                     else
  61.                     {
  62.                         line = null;
  63.                         break;
  64.                     }
  65.                 }
  66.             //一页显示不完时自动重新调用此方法
  67.             if (line == null)
  68.             {
  69.                 e.HasMorePages = true;
  70.             }
  71.             else
  72.             {
  73.                 e.HasMorePages = false;
  74.             }
  75.             //每次打印完后countNum清0;
  76.             if (countNum >= richTextBox1.Lines.Length)
  77.             {
  78.                 countNum = 0;
  79.             }
  80.         }

 

3、使用 printDocument.Print()进行打印
  1. private void 打印PToolStripButton_Click(object sender, EventArgs e)
  2.         {
  3.            // tsmSave_Click(sender, e);
  4.             //初始化PrintDocument()
  5.             this.PrintDocument();
  6.             PrintDialog print = new PrintDialog();
  7.             //将初始化后的printDocument赋给print.Document
  8.             print.Document = printDocument;
  9.             try
  10.             {
  11.                 if (print.ShowDialog() == DialogResult.OK)
  12.                 {
  13.                     //打印时直接调用PrintDocument的Print方法
  14.                     printDocument.Print();
  15.                 }
  16.             }
  17.             catch (Exception ex)
  18.             {
  19.                 MessageBox.Show(&quot;打印出错:&quot; + ex.ToString());
  20.                 //出错则结束打印过程
  21.                 printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
  22.             }
  23.         }
 4、定义打印预览(可选) \、、、、、、、
        private void 打印预览VToolStripMenuItem_Click(object sender, EventArgs e)        
        {            
            PrintPreviewDialog printPreview = new PrintPreviewDialog();            
            //初始化PrintDocument            
            this.PrintDocument();           
            //将初始化后的printDocument赋给print.Document            
            printPreview.Document = printDocument;            
            try            
            {               
                printPreview.ShowDialog();         
            }            
            catch (Exception ex)            
            {               
                MessageBox.Show(ex.ToString());        
            }        
        }
 
原创粉丝点击