e. Hasmorepages stays in Infinite Loop

Asked

Viewed 24 times

-1

I am trying to create a medium of printing meats, however the e.Hasmorepages keeps adding pages and does not stop according to the amounts of parcels in the meat.

    public static string Impressora;
    private static void BuscarImpressora()
    {
        var configImpressora = new PrinterSettings();
        Impressora = configImpressora.PrinterName.ToString();       
    }

    public static void Imprimir()
    {
        BuscarImpressora();
        using (var pd = new PrintDocument())
        {
            pd.PrinterSettings.PrinterName = Impressora;
            pd.DocumentName = "Impressão Carner";
            pd.PrintPage += Pd_PrintPage;
            pd.Print();
        }

    }
    private static void Pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        var printDocument = sender as System.Drawing.Printing.PrintDocument;

        if (printDocument != null)
        {
            ///CANETAS
            Pen blackPen = new Pen(Color.Black, 1);
            Pen RedPen = new Pen(Color.Red, 1);
            RedPen.DashStyle = DashStyle.Dash;
            ///USING
            using (var Titulo = new Font("Arial ", 14))
            using (var brush = new SolidBrush(Color.Black))
            {
                ///CONTAGEM
                int n = 10+1;
                ///EIXO
                int x1 = 0;
                int y1 = 39;
                int x2 = 708;
                int y2 = 39;
                ///TAMANHO
                int w = 708;
                int h = 196;
                int hPage = 39;
                while (n > 0)
                {
                    ///RETANGULOS
                    //Rectangle Margen = new Rectangle(x1+20, y1, w-39, h-39);
                    ///ÁREA DE IMPRESSÃO
                    //e.Graphics.DrawRectangle(blackPen, Margen);
                    e.Graphics.DrawLine(RedPen, x1, y1, x2, y2);
                    ///CONTAGEM -1
                    n--;
                    ///SOMAR TAMANHO
                    y1 = y1 + 196;
                    y2 = y2 + 196;
                    hPage = hPage + 196;
                    if (hPage > e.PageBounds.Height)
                    {
                        e.HasMorePages = true;
                        hPage = 0;
                    }
                    else
                        e.HasMorePages = true;
                }
            }
        }
    }

1 answer

0

You assign true to it in both conditions... if (hPage > e.PageBounds.Height) as in the else.

Have a look at your code.

if (hPage > e.PageBounds.Height)
{
    e.HasMorePages = true;
    hPage = 0;
}
else
    e.HasMorePages = false;
  • When I put it this way, it doesn’t count adding 1 more page if hPage is larger than e.PageBound.Heigth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.