Question

I want to print on to the NextPageusing printdocument in C#. The code I am using keeps on incrementing the number of pages to the infinite loop.

enter image description here

The code I am using is as follows:

PaperSize pze = new PaperSize("Custom Paper size", 800, 150);

public void Print()
{
     printDocument1.DefaultPageSettings.PaperSize = pze;
     printDocument1.Print();
}
int YHeight = 0;
private void printDocument1_PrintPage(object sender,     System.Drawing.Printing.PrintPageEventArgs e)
{
        panel1_Paint(sender, new PaintEventArgs(e.Graphics, this.ClientRectangle));
        int heightOfPage = 150;
        float pageHeight = e.MarginBounds.Height;
        while (heightOfPage + 100 < pageHeight && YHeight < this.SaveBeforePrint.Count)
        {

            YHeight += 1;
            heightOfPage += 20;
        }

        if (YHeight < this.SaveBeforePrint.Count)
        {
            e.HasMorePages = true;
            return;
        }
        else
        {
            e.HasMorePages = false;
        }
}
Was it helpful?

Solution

Hi change your PrintPage method as below ..

here countInRow is the count of barcode in a row on each page to display ..

int countInRow = 3;  // number of barcode in a row on every page.

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{        
   panel1_Paint(sender, new PaintEventArgs(e.Graphics, this.ClientRectangle));

   int locCount = SaveBeforePrint.Count;
   for (int i = 0; i < countInRow && i < locCount; i++)
   {
      SaveBeforePrint.RemoveAt(0);        //remove the top element always
   }
   e.HasMorePages = (SaveBeforePrint.Count > 0);
}

you have to make one more change in "panel1_Paint" method also, change the for loop condition

private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
  .
  .
  .
    for (int serial = 0; serial < SaveBeforePrint.Count && serial < countInRow; serial++)
    {
     // you code goes here.. 
    }
  .
  .
  .
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top