Click here to Skip to main content
15,899,124 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem No 1=>
I'm printing customer receipt but when i add more then 20 product then it generate new page and the header of 1st page also show in new page but i want it show only product details in new page header is show only first page please tell me how can i do this.

problem No 2=>

when more then 1 page are added then i take print using Thermal printer the problem is that it print only first page and skip the other pages .but i want it print all the pages that is more then 1 page in only one page that is printed by thermal printer.

Problem No =>
when i added more the 20 product then it continuously generate new page and not stop to generating new page

What I have tried:

Its my code that is use to print customer Receipt
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //this method get data from database and fill in list
            FillListforprintingItems();

            e.Graphics.DrawString("Date :" +  DateTime.Now, new Font("Arial", 12, FontStyle.Bold),
            Brushes.Black, 200,50);
            e.Graphics.DrawString("------------------------------------------------------------------------------------",
            new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(9, 85));
            e.Graphics.DrawString("Product", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(10,100));
            //e.Graphics.DrawString("Qty", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(340, 100));
            e.Graphics.DrawString("Amount", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(180, 100));
            e.Graphics.DrawString
            ("------------------------------------------------------------------------------------"
            ,new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(9,  115));
            //int ypos = 250;
            int startX = 10;
            int startY = 140;
            int offset = 40;
            for (int i = NumberOfItemPrintedSofar; i < _List.Count; i++)
            {
                NumberofExpensperPage++;
                if (NumberofExpensperPage <= 20)
                {
                    NumberOfItemPrintedSofar++;
                    if (NumberOfItemPrintedSofar <= _List.Count)
                    {
                        e.Graphics.DrawString(_List[i].Qty+" X " +_List[i].Name, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY);
                        e.Graphics.DrawString(Convert.ToString("Vat % :"+_List[i].vateparecent), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY + 20);
                        string colorsize = _List[i].sizcolor;
                        if (colorsize != "")
                        {
                            e.Graphics.DrawString(Convert.ToString(colorsize), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY + 40);
                        }
                        e.Graphics.DrawString(_List[i].amountwithvat.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX + 170, startY);                    
                        // e.Graphics.DrawString(Convert.ToString( _List[i].Qty), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX + 330, startY);
                        if (colorsize != "")
                        {
                            e.Graphics.DrawString
                          ("----------------------------------------------------------------------------------",                     
                         new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY = startY + 55));
                        }
                        if (colorsize == "")
                        {
                            e.Graphics.DrawString
                       ("----------------------------------------------------------------------------------",
                      new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY = startY + 35));

                        }
                        startY += 15;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }
                else
                {
                    NumberofExpensperPage = 0;
                    e.HasMorePages = true;
                    return;
                }
            } 
            //e.Graphics.DrawString("----------------------------------------------------------------------------------",
            //new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY));
            e.Graphics.DrawString("Total Amount :" + TotalAmounttextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX + 10 , startY + 30));
            //rest thhe veriable
            NumberofExpensperPage = 0;
            NumberOfItemPrintedSofar = 0;
            _List.Clear();           
        }
Posted
Updated 30-Jan-19 8:07am
v2

1 solution

So check your page number (which I assume is based on NumberOfItemPrintedSofar) and only print the header on the first page.

And do yourself a favour: don't create Font objects willy nilly - they each use Handles which are scarce resources that are only released when the Font object is Disposed. And just creating new instances all the time will run you out of Handles long, long before your run out of memory and the GC kicks in to automatically dispose them.

Create one sample of each font you use - and from what I can see you only need two - and keep them in class based private variables - your code will be faster, and won't crash with "out of memory" errors when the Handles are exhausted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900