• C#中进行文本打印



    问题描述:
          做了个记事本程序,要求能按标准打印其中的文档,包括在每行文字数目上进行控制等。
    解决方法:
     一、搞清楚打印的过程:
          1、定义PrintDocument类,并且声明其PrintPage事件。
       
    private void PrintDocument()
            
    {
                printDocument 
    = new PrintDocument();
                printDocument.PrintPage 
    += new PrintPageEventHandler(printDocument_PrintPage);
            }
         2、实现PrintPage事件(关键):
        
        /// <summary>
            
    /// 用来处理打印过程的核心事件。
            
    /// </summary>
            
    /// <param name="sender"></param>
            
    /// <param name="e"></param>
            
    /// <remarks></remarks>

            void printDocument_PrintPage(object sender, PrintPageEventArgs e)
            
    {
                
    //StringReader lineReader = new StringReader(textBox.Text);
                Graphics graphic = e.Graphics;//获取绘图对象
                float linesPerPage = 0;//页面行号
                float yPosition = 0;//绘制字符串的纵向位置
                float leftMargin = e.MarginBounds.Left;//左边距
                float topMargin = e.MarginBounds.Top;//上边距
                string line = string.Empty;//读取的行字符串
                int currentPageLine=0;//当前页读取的行数
                Font charFont = richTextBox1.Font;//获取打印字体
                SolidBrush brush = new SolidBrush(Color.Black);//刷子
                linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
                
    //countNum记录全局行数,currentPageLine记录当前打印页行数。
                while (countNum < richTextBox1.Lines.Length)
                    
    {
                        
    if (currentPageLine < linesPerPage)
                        
    {
                            line 
    = richTextBox1.Lines[countNum].ToString();
                            
    if (line.Length <= 50)
                            
    {
                                yPosition 
    = topMargin + (currentPageLine * charFont.GetHeight(graphic));
                                
    //绘制当前行
                                graphic.DrawString(line, charFont, brush, leftMargin, yPosition, new StringFormat());
                                countNum
    ++;
                                currentPageLine
    ++;
                            }

                            
    else
                            
    {
                                
    //拆分后的行数
                                int moreLine = line.Length / 50 + 1;
                                
    string tempLine;
                                
    for (int i = 0; i < moreLine; i++)
                                
    {
                                    
    //获得当前行的子串
                                    if ((line.Length - i * 50>= 50)
                                    
    {
                                         tempLine 
    = line.Substring(i * 5050);
                                    }

                                    
    else
                                    
    {
                                        tempLine 
    = line.Substring(i * 50, line.Length - i * 50);
                                    }

                                    yPosition 
    = topMargin + (currentPageLine * charFont.GetHeight(graphic));
                                    
    //绘制当前行
                                    graphic.DrawString(tempLine, charFont, brush, leftMargin, yPosition, new StringFormat());
                                    
    //当前打印页行数加1
                                    currentPageLine++;
                                }

                                
    //总行数加1
                                countNum++;
                            }

                        }

                        
    else
                        
    {
                            line 
    = null;
                            
    break;
                        }

                    }

                
    //一页显示不完时自动重新调用此方法
                if (line == null)
                
    {
                    e.HasMorePages 
    = true;
                }

                
    else
                
    {
                    e.HasMorePages 
    = false;
                }

                
    //每次打印完后countNum清0;
                if (countNum >= richTextBox1.Lines.Length)
                
    {
                    countNum 
    = 0;
                }

            }
         3、使用 printDocument.Print()进行打印
     private void 打印PToolStripButton_Click(object sender, EventArgs e)
            
    {
               
    // tsmSave_Click(sender, e);
                
    //初始化PrintDocument()
                this.PrintDocument();
                PrintDialog print 
    = new PrintDialog();
                
    //将初始化后的printDocument赋给print.Document
                print.Document = printDocument;
                
    try
                
    {
                    
    if (print.ShowDialog() == DialogResult.OK)
                    
    {
                        
    //打印时直接调用PrintDocument的Print方法
                        printDocument.Print();
                    }

                }

                
    catch (Exception ex)
                
    {
                    MessageBox.Show(
    "打印出错:" + ex.ToString());
                    
    //出错则结束打印过程
                    printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
                }

            }
        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());
                }

            }
    完整代码下载/Files/seek/NotePad.rar
    SIGNATRUE-----------------------------------
    龟看上去很慢很慢,而且还有些憨,虽然没有兔子跑的快,但是只要有坚持不懈的毅力,就一定会到达成功的比彼岸.如果自己是龟,就不要试图把自己变成兔子,我就是那只憨龟。
  • 相关阅读:
    python发送邮件
    常用的排序算法
    关于前端ajax请求url为何添加一个随机数
    RabbitMQ消息队列
    shell编程基本语法和变量
    第70课 展望:未来的学习之路(完结)
    第69课 技巧:自定义内存管理
    第68课 拾遗:让人迷惑的写法
    第67课 经典问题解析五
    第66课 C++中的类型识别
  • 原文地址:https://www.cnblogs.com/seek/p/1192421.html
Copyright © 2020-2023  润新知