Using PrintDocument to Print Multiple Pages

I am trying to print an invoice. Invoices should be printed on several pages, but where the problem arises. I can perfectly print the invoice on one page, but as soon as the invoice does not fit on one page, the press just shuts down the first page.

Here is the code I'm using. "artikelen" is a list of articles (List). I read several similar examples, and I'm sure something is missing here.

(Edited: some someecceary code deleted)

public void PrintA4Factuur()
    {
        p = new PrintDocument();
        p.PrintPage +=
            new PrintPageEventHandler(printPage);
        printPreviewDialog.Document = p;
        printPreviewDialog.ShowDialog();
    }

void printPage(object sender1, PrintPageEventArgs e1)
    {
Graphics g = e1.Graphics;
int yPos = 320;
float pageHeight = e1.MarginBounds.Height;
int artikelPosition = 0;
while (yPos + 100 < pageHeight
            && artikelPosition < this.artikelen.Count)
        {
            // Do stuff with articles (printing details in different rectangles

            artikelPosition += 1;
            yPos += 20;
        }

        if (artikelPosition < this.artikelen.Count)
        {
            e1.HasMorePages = true;
            return;
        }
        else
        {
            e1.HasMorePages = false;
        }
}
+3
source share
2 answers

I found that your code does the opposite: if it prints more than one page, it continues to print to infinity.

PrintPage, :

int artikelPosition = 0;

Reset :

public void PrintA4Factuur()
{
  artikelPosition = 0

  p = new PrintDocument();
  p.PrintPage += printPage;
  printPreviewDialog.Document = p;
  printPreviewDialog.ShowDialog();
}

PrintPage:

void printPage(object sender1, PrintPageEventArgs e1)
{
  Graphics g = e1.Graphics;
  int yPos = 320;
  float pageHeight = e1.MarginBounds.Height;

  // int artikelPosition = 0;

  // continue with code
}
+3

, artikelPosition , .

e1.MarginBounds , , p.DefaultPageSettings .

, GetHeight(yourDeviceGraphPort), .

float , int s.

- , Dispose , . ; PrintDocument.Print() .

SolidBrush, System.Drawing.

+4

All Articles