打印提供无限页面

时间:2014-12-28 16:15:47

标签: c# .net loops printing

下面是一个打印测试字符串的方法。我无法弄清楚为什么这会给出具有相同内容的无限新页面。我需要在第30轮后打印一个新页面。

private void PrintSetup(Graphics g, System.Drawing.Printing.PrintPageEventArgs e) 
    {
        float linesPerPage = 0;
        int count = 0;
        float leftMargin = e.MarginBounds.Left;
        float topMargin = e.MarginBounds.Top;
        float rightMargin = e.MarginBounds.Right;
        float bottomMargin = e.MarginBounds.Bottom;
        float Height = e.MarginBounds.Height;
        float Width = e.MarginBounds.Width;
        float FontHeight = NormalFont.GetHeight();

        linesPerPage = Height / NormalFont.GetHeight(e.Graphics);

        while (count < linesPerPage)
        {

            g.DrawString("Test   " + count, NormalFont, BlackBrush, leftMargin, topMargin + Line(count));

            if (count > 30)
            {
                e.HasMorePages = true;
            }
            else 
            {
                e.HasMorePages = false;
            }

            count++;

        }
    }

2 个答案:

答案 0 :(得分:0)

一旦达到30,就永远不会重置count

答案 1 :(得分:0)

谢谢大家回答我...经过很长一段时间我创立了所发生的事情..以下代码工作正常。

如果计数超出范围,将创建一个新页面。

希望这对初学者有所帮助。感谢

_Line = 0; 

void printDocument1_PrintPage( object sender, System.Drawing.Printing.PrintPageEventArgs e )
                
            {

                float lineHeight = NormalFont.GetHeight(e.Graphics) + 4;
     
                    float yLineTop = e.MarginBounds.Top;
                    yLineTop = yLineTop + 100;

                    for ( ; _Line <= 100 ; _Line++ )
                    {

                        if ( yLineTop + lineHeight > e.MarginBounds.Bottom )
                        {
                            e.HasMorePages = true;
                            return;
                        }
     
                        e.Graphics.DrawString( "TEST: " + _Line, NormalFont, Brushes.Black, new PointF( e.MarginBounds.Left, yLineTop ) );

                        yLineTop += lineHeight;

                    }

                    e.HasMorePages = false;
              
            }