1: private bool PrintMore = false;
2: private int pagecounter=2;
3: private int PrintUntilIndex = 0;
4: float pageheight = 0;
5: private void PrintSales(List<Sale> Sales)
6: {
7: PrintDocument recordDoc;
8: // Create the document and name it
9: recordDoc = new PrintDocument();
10: recordDoc.DocumentName = "Receipt";
11: // Set Printer By Name
12: recordDoc.PrinterSettings.PrinterName = "Epson 220 POS";
13: recordDoc.PrintPage += new PrintPageEventHandler(this.PrintReceiptPage);
14: recordDoc.Print();
15:
16: recordDoc.PrintPage -= new PrintPageEventHandler(this.PrintReceiptPage);
17:
18: // Dispose of document when done printing
19: recordDoc.Dispose();
20:
21: while (PrintMore == true)
22: {
23: // Create the document and name it
24: recordDoc = new PrintDocument();
25: recordDoc.DocumentName = "Receipt" + pagecounter;
26: pagecounter++;
27: recordDoc.PrintPage += new PrintPageEventHandler(this.PrintReceiptPage);
28: // Preview document
29: //PrintPreviewDialog dlgPreview = new PrintPreviewDialog();
30: //dlgPreview.Document = recordDoc;
31: //dlgPreview.ShowDialog();
32:
33: recordDoc.Print();
34:
35: recordDoc.PrintPage -= new PrintPageEventHandler(this.PrintReceiptPage);
36:
37: // Dispose of document when done printing
38: recordDoc.Dispose();
39: }
40: //reset value
41: pagecounter = 1;
42: PrintUntilIndex = 0;
43: }
44:
45: private void PrintReceiptPage(object sender, PrintPageEventArgs ev){
46: //Get Paper Height
47: pageheight = ev.PageSettings.PaperSize.Height;
48:
49: float linesPerPage = 0;
50: float yPosition = 0;
51: int RowMargin = 15;
52: int marginRight = 220;
53: int RightAfterProductName = 80;
54: float leftMargin = 0;
55: float topMargin = ev.MarginBounds.Top;
56: string line = null;
57: Font printFont = new Font("Arial",
58: 10, System.Drawing.GraphicsUnit.Point);
59:
60: Font GrandTotalFont = new Font("Arial",
61: 12, System.Drawing.GraphicsUnit.Point);
62: int RightMostMargin = 120;
63: SolidBrush myBrush = new SolidBrush(Color.Black);
64: // Work out the number of lines per page, using the MarginBounds.
65: linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
66:
67: //Header
68: if (PrintMore == false)
69: {
70: //Print your First Page Header Title
71: ev.Graphics.DrawString("Header", printFont, myBrush, leftMargin, yPosition, new StringFormat());
72: yPosition += RowMargin;
73: }
74: //Loop Through List
75: foreach (var item in Sales.Skip(PrintUntilIndex).Take(Sales.Count - PrintUntilIndex))
76: {
77: if (pageheight < yPosition)
78: {
79: break;
80: }
81: //More Printing
82: if (pageheight < yPosition)
83: {
84: break;
85: }
86:
87: PrintUntilIndex++;
88: }
89: if (pageheight < yPosition)
90: {
91:
92: PrintMore = true;
93: }
94: else
95: PrintMore = false;
96: }
97:
98: }