C # e.HasMorePage is in Eternal Loop

1

I'm creating a system for printing labels as follows:

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    Image newImage;
    Point ulCorner;
    int x = 0;
    int xn = 0;
    int y = 0;
    int yn = 0;
    bool xativo = false;

    int n = 14;
    int n1 = 0;
    int yPage = 0;

    for (int i = 0; i < lstAdesivos.Items.Count; i++)
    {
        newImage = Image.FromFile(@path + "\Modelos\" + lstAdesivos.Items[i].Text + ".png");
        if (xativo == true)
        {
            x = 400;
            y = 0;
            xativo = false;
        }

        ulCorner = new Point(x, y);
        yPage += newImage.Size.Height;

        if (yPage > e.PageBounds.Height)
        {
            e.HasMorePages = true;
            yPage = 0;
        }

        e.Graphics.DrawImage(newImage, ulCorner);

        if (yn == nudCartelasColuna.Value - 1)
        {
            yn = 0;
            xativo = true;
        }
        else
        {
            y += 200;
            yn += 1;
        }
    }

    Font fonte = new Font("Verdana", 12);
    e.Graphics.DrawString(tbDescricao.Text, fonte, Brushes.Black, 400, 1050);
}

What happens is that instead of just creating a new page it creates many infinitely, would you like to know what I'm doing wrong?

    
asked by anonymous 13.08.2014 / 16:22

4 answers

1

It's a logical problem, I do not think you understand how the print process works with PrintDocument.

As you said, it goes back to PrintPage , and that's what you expect.

This event, PrintPage will be called once for each page, if when it finishes executing the value of e.HasMorePages is equal to true it will be called again.

What you need to know is what images have already been printed each time the event is called.

Basically you print what fits on the page, the time that it does not fit anymore, there are still more things to print you arrow e.HasMorePages = true and exits the function, then it will be called again and you continue printing what missing the new page from where it stopped, repeating the process if necessary.

How to do this there are several ways, you will probably need some value in the class scope to mark where you left off and know where to continue.

    
26.08.2014 / 13:39
0

My "kick" is that with each iteration of the block you are increasing the lstAdesivo.items.count, would it be possible? I did not see the definition of it in the code and I risk that if you are in an infinite loop, with each iteration the count goes up one unit. Can you test and report?

Looking over, your code does not seem wrong to me, so it's the only thing that comes to mind.

    
13.08.2014 / 19:32
0

So the for is working normally, but one thing I noticed is that after finishing it, it starts printDocument1_PrintPage again. Then go back in.

I'm calling it this way:

printPreviewDialog1.Document = this.printDocument1;

printPreviewDialog1.ShowDialog ();

    
13.08.2014 / 20:13
0

Try to

    if (yPage > e.PageBounds.Height)
    {
        e.HasMorePages = true;
        yPage = 0;
    }else{
        e.HasMorePages = true;

    }
    
26.08.2014 / 06:17