C#打印分页中的HasMorePages用法

对于HasMorePages,从字面上理解就是多页打印,也就是实现C#打印分页。在很多时候,由于参数设定错误,很容易出现new PointF(10.0, 30.0)部分报错的现象。这里需要谨慎对待。

HasMorePages的作用很容易产生误解,但也很好理解:当PrintPage函数执行完后,如果HasMorePages==true,则重新执行一遍PrintPage这个函数。只要明白了这一点,打印分页就很简单了。

在打印时,可以把打印位置保存下来,以便于第二次执行PrintPage时知道从哪开始打印。例如,下面这段程序用来打印一个DataTable中有所有数据,每页打印一条:

C#打印分页代码

 
 
  1. class Print  
  2. {  
  3.     public partial class NoticePrinter : Form  
  4.     {  
  5.         PrintDialog _printDialog;  
  6.         PrintDocument _printDocument;  
  7.         DataTable _table;  
  8.         int _curRow;  
  9.    
  10.         //在执行这个函数之前,先要把_curRow初始化为0;  
  11.         //当HasMorePages==true时,这个函数会重复执行,直到HasMorePages==fasle为止;  
  12.         void _printDocument_PrintPage(object sender, PrintPageEventArgs e)  
  13.         {  
  14.             DataRow row;  
  15.             string name;  
  16.             string sex;  
  17.    
  18.             row = _table.Rows[_curRow];  
  19.             name = row["xm"].ToString().Trim();  
  20.             sex = row["xb"].ToString().Trim();  
  21.    
  22.             Font font = new Font("宋休", 12);  
  23.    
  24.             e.Graphics.DrawString(name, font, Brushes.Black, new PointF(10.0, 10.0));  
  25.             e.Graphics.DrawString(sex, font, Brushes.Black, new PointF(10.0, 30.0));  
  26.    
  27.             _curRow++;  
  28.             if (_curPage == _table.Rows.Count)  
  29.             {  
  30.                 e.HasMorePages = false;  
  31.             }  
  32.             else 
  33.             {  
  34.                 e.HasMorePages = true;  
  35.             }  
  36.         }  
  37.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
打印方法小议,页面设置对话框、打印预览对话框、打印对话框等功能,C#源代码 //字符串流对 象,一行一行读取文本 private StringReader MyReader; private void button1_Click(object sender, EventArgs e) { //显示页面设置对话框 PageSetupDialog MyDlg = new PageSetupDialog(); MyDlg.Document = this.printDocument1; MyDlg.ShowDialog(); } private void button2_Click(object sender, EventArgs e) { //显示打印预览对话框 PrintPreviewDialog MyDlg = new PrintPreviewDialog(); MyDlg.Document = this.printDocument1; this.MyReader = new StringReader(this.richTextBox1.Text); MyDlg.ShowDialog(); } private void button3_Click(object sender, EventArgs e) { //显示打印对话框 PrintDialog MyDlg = new PrintDialog(); MyDlg.Document = this.printDocument1; MyDlg.ShowDialog(); } private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { Graphics g = e.Graphics; //每一页的行数 float MyLines = e.MarginBounds.Height / this.richTextBox1.Font.GetHeight (g); //打印时的行计数器 int MyLineNumber = 0; //打印时的纵坐标 float MyYPosition = 0; float MyMarginLeft = e.MarginBounds.Left; float MyMarginTop = e.MarginBounds.Top; //每一行要打印的文本 string MyLine = ""; while ((MyLineNumber < MyLines) && ((MyLine = MyReader.ReadLine()) != null)) { MyYPosition = MyMarginTop + MyLineNumber * this.richTextBox1.Font.GetHeight(g); g.DrawString(MyLine, this.richTextBox1.Font, new SolidBrush (Color.Black), MyMarginLeft, MyYPosition, new StringFormat()); MyLineNumber++; } if (MyLine != null) { //发出下一次PrintPage事件 e.HasMorePages = true; }
要在WinForms实现分页打印,您可以使用PrintDocument类的PrintPage事件。PrintPage事件在打印打印每一页时触发,您可以在该事件处理程序绘制当前页的内容,并告诉打印机是否还有更多的页需要打印。 以下是一个示例代码,它会在打印文档的PrintPage事件处理程序绘制一个文本框的内容,并将其分成多页打印。 ```csharp public partial class Form1 : Form { private PrintDocument printDoc = new PrintDocument(); private int currentPage = 1; public Form1() { InitializeComponent(); // 设置打印文档的事件处理程序 printDoc.PrintPage += new PrintPageEventHandler(PrintDoc_PrintPage); } private void btnPrint_Click(object sender, EventArgs e) { // 显示打印对话框并打印文档 PrintDialog printDlg = new PrintDialog(); printDlg.Document = printDoc; if (printDlg.ShowDialog() == DialogResult.OK) { currentPage = 1; printDoc.Print(); } } private void PrintDoc_PrintPage(object sender, PrintPageEventArgs e) { // 在打印文档的事件处理程序绘制当前页的内容 Graphics g = e.Graphics; // 绘制文本框的内容 Font font = new Font("Arial", 12); string text = textBox1.Text; float lineHeight = font.GetHeight(g); float x = e.MarginBounds.Left; float y = e.MarginBounds.Top + (currentPage - 1) * e.MarginBounds.Height; RectangleF rect = new RectangleF(x, y, e.MarginBounds.Width, e.MarginBounds.Height); StringFormat format = new StringFormat(); format.LineAlignment = StringAlignment.Near; format.Alignment = StringAlignment.Near; int chars, lines; g.MeasureString(text, font, rect.Size, format, out chars, out lines); g.DrawString(text.Substring(0, chars), font, Brushes.Black, rect, format); // 告诉打印机是否还有更多的页需要打印 if (chars < text.Length) { e.HasMorePages = true; currentPage++; } else { e.HasMorePages = false; currentPage = 1; } } } ``` 在此示例,我们使用了一个文本框控件(textBox1)来输入要打印的内容。在打印文档的PrintPage事件处理程序,我们使用Graphics对象绘制当前页的内容,并使用MeasureString方法计算出当前页能够容纳的文本量。如果文本量超过了当前页的容量,则将HasMorePages属性设置为true,并增加currentPage的值,以便在下一页上绘制剩余的文本。如果所有的文本都已经打印完毕,则将HasMorePages属性设置为false,并将currentPage的值重置为1。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值