mohamed_69 0 Newbie Poster

Hello, guys i am having a problem with the Printing some tickets i need to print each ticket in a different page beacause the printer i'm using can only print the size you customize it to print and my program can have different variations knwint that i import an Excel file and dependinf on its content the tickets are generated.
* I already tried to print using an offset that increments but i still have the same problem [Capture 1] . (offsetTicket +=300;)
* Knowing that i only have a printer that can only print according to the size we define i think it will be more appropriate to put each ticket in its own page
* So i've put the (offsetTicket +=0;) to 0, [Capture 2]
NB: i already tried the ev.HasMorePages Property but i don't know where and how should i use it bacause as you can see in the code there is 2 loops
The code and captures of the result in question follows:

 void pd_PrintPage(object sender, PrintPageEventArgs e)
        {

              //********************************************************************************************\\
             //************************************print function********************************************\\
            //************************************************************************************************\\
            int offsetTicket = 0;
            currentpage = 0;
            nbr_pages = 0;
            // starting position of week and date
            int startXd = 200;
            int startYd = 200;
            int offsetX = 60;
            int offsetYpic = 0;

            // starting position of Drawstring and barcodes
            int startX = 150; // horizontal
            int startY = 10; // vertical


                for (i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    nbr_rep = 1; // count of repetitions
                for (j = 0; j < nbr_rep; j++)
                    {
                        int offsetY = 0;
                        Graphics graphic = e.Graphics;
                        Font font = new Font("Courier New", 32);
                        float frontHeight = font.GetHeight();

                        // line 1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        graphic.DrawString("PN :", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + offsetY + offsetTicket);
                        string cellval = dataGridView1.Rows[i].Cells[5].Value.ToString();
                        graphic.DrawString(cellval, new Font("Courier New", 14), new SolidBrush(Color.Black), startX + offsetX, startY + offsetY + offsetTicket);
                        offsetY = offsetY + (int)FontHeight + 10;
                        // line 1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                        // line 2 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        graphic.DrawString("Qty:", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + offsetY + offsetTicket);
                        //database selection
                        string constring = "datasource=localhost;port=3306;username=root;password=eciadmin123";
                        MySqlConnection conDataBase = new MySqlConnection(constring);
                        MySqlCommand cmdDataBase = new MySqlCommand(" select PU from visual_fifo.refs_pu where ref='" + cellval + "' ; ", conDataBase);
                        MySqlDataReader myReader;
                        try
                        {
                            conDataBase.Open();
                            myReader = cmdDataBase.ExecuteReader();
                            while (myReader.Read())
                            {
                                string qty_val = myReader.GetString(0);
                                textBox1.Text = qty_val;
                            }
                            conDataBase.Close();
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        graphic.DrawString(textBox1.Text, new Font("Courier New", 14), new SolidBrush(Color.Black), startX + offsetX, startY + offsetY + offsetTicket);
                        offsetY = offsetY + (int)FontHeight + 10;
                        // line 2 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                        // line 3 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                        graphic.DrawString("Lot:", new Font("Courier New", 14), new SolidBrush(Color.Black), startX, startY + offsetY + offsetTicket);
                        string cellval3 = dataGridView1.Rows[i].Cells[13].Value.ToString();
                        graphic.DrawString(cellval3, new Font("Courier New", 14), new SolidBrush(Color.Black), startX + offsetX, startY + offsetY + offsetTicket);
                        offsetY = offsetY + (int)FontHeight + 5;
                        // line 3 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                        //count of repetitions
                        string cellvalQty_in_Un_of_Entry = dataGridView1.Rows[i].Cells[14].Value.ToString();
                        int num = int.Parse(cellvalQty_in_Un_of_Entry);
                        int pu = int.Parse(textBox1.Text);
                        nbr_rep = num / pu;

                        // barecode PN---------------------------------------------------------------------------------------------------------------------------
                        // -------------------------------------------------------------------------------------------------------------------------------------
                        //barecode alignment
                        b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                        //barecode type 128
                        BarcodeLib.TYPE type = BarcodeLib.TYPE.CODE128;
                        try
                        {
                            if (type != BarcodeLib.TYPE.UNSPECIFIED)
                            {
                                //add label
                                b.IncludeLabel = true;
                                //rotation = rotateonflipnone
                                b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), "rotatenoneflipnone", true);
                                // ***** Encoding performed here *****
                                pictureBox2.BackgroundImage = b.Encode(type, cellval.Trim(), Color.Black, Color.White, 145, 75);
                            }
                            //reposition the barecode to the center
                            pictureBox2.Location = new Point((this.pictureBox2.Location.X + this.pictureBox2.Width / 2) - pictureBox2.Width / 2, (this.pictureBox2.Location.Y + this.pictureBox2.Height / 2) - pictureBox2.Height / 2);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }

                        Bitmap bitmap = new Bitmap(pictureBox2.Width, pictureBox2.Height);
                        pictureBox2.DrawToBitmap(bitmap, new Rectangle(0, 0, pictureBox2.Width, pictureBox2.Height));
                        Point loc_bc_pn = new Point(0, 10 + offsetYpic + offsetTicket);
                        e.Graphics.DrawImage(bitmap, loc_bc_pn);
                    // -------------------------------------------------------------------------------------------------------------------------------------\\

                      // barecode QTY-------------------------------------------------------------------------------------------------------------------------\\
                     // ---------------------------------------------------------------------------------------------------------------------------------------\\
                    //barecode alignment
                    b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                        //barecode type 128
                        BarcodeLib.TYPE type2 = BarcodeLib.TYPE.CODE128;
                        try
                        {
                            if (type2 != BarcodeLib.TYPE.UNSPECIFIED)
                            {
                                //add label
                                b.IncludeLabel = true;
                                //rotation = rotateonflipnone
                                b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), "rotatenoneflipnone", true);
                                // ***** Encoding performed here *****
                                pictureBox3.BackgroundImage = b.Encode(type, textBox1.Text.Trim(), Color.Black, Color.White, 145, 75);
                            }
                            //reposition the barecode to the center
                            pictureBox3.Location = new Point((this.pictureBox3.Location.X + this.pictureBox3.Width / 2) - pictureBox3.Width / 2, (this.pictureBox3.Location.Y + this.pictureBox3.Height / 2) - pictureBox3.Height / 2);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        Bitmap bitmap2 = new Bitmap(pictureBox3.Width, pictureBox3.Height);
                        pictureBox3.DrawToBitmap(bitmap2, new Rectangle(0, 0, pictureBox3.Width, pictureBox3.Height));
                        Point loc_bc_qty = new Point(0, 90 + offsetYpic + offsetTicket);
                        e.Graphics.DrawImage(bitmap2, loc_bc_qty);
                     // -------------------------------------------------------------------------------------------------------------------------------------\\                                      

                      // barecode Lot-------------------------------------------------------------------------------------------------------------------------\\
                     // ---------------------------------------------------------------------------------------------------------------------------------------\\
                    //barecode alignment
                    b.Alignment = BarcodeLib.AlignmentPositions.CENTER;
                        //barecode type 128
                        BarcodeLib.TYPE type3 = BarcodeLib.TYPE.CODE128;

                        try
                        {
                            if (type3 != BarcodeLib.TYPE.UNSPECIFIED)
                            {
                                //add label
                                b.IncludeLabel = true;
                                //rotation = rotateonflipnone
                                b.RotateFlipType = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), "rotatenoneflipnone", true);
                                // ***** Encoding performed here *****
                                pictureBox4.BackgroundImage = b.Encode(type, cellval3.Trim(), Color.Black, Color.White, 145, 75);
                            }
                            //reposition the barecode to the center
                            pictureBox4.Location = new Point((this.pictureBox4.Location.X + this.pictureBox4.Width / 2) - pictureBox4.Width / 2, (this.pictureBox4.Location.Y + this.pictureBox4.Height / 2) - pictureBox4.Height / 2);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        Bitmap bitmap3 = new Bitmap(pictureBox4.Width, pictureBox4.Height);
                        pictureBox4.DrawToBitmap(bitmap2, new Rectangle(0, 0, pictureBox4.Width, pictureBox4.Height));
                        Point loc_bc_lot = new Point(0, 190 + offsetYpic + offsetTicket);
                        e.Graphics.DrawImage(bitmap2, loc_bc_lot);
                    // -------------------------------------------------------------------------------------------------------------------------------------\\

                    // line date and weeks
                    //graphic.DrawString("Date:", new Font("Courier New", 14), new SolidBrush(Color.Black), startXd, startYd + offsetY);

                    graphic.DrawString("Week:", new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startXd, startYd + offsetYpic + offsetTicket);
                        string cellval4 = dataGridView1.Rows[i].Cells[19].Value.ToString();
                        offsetY = offsetY + (int)FontHeight + 5;
                        DateTime datetime = Convert.ToDateTime(cellval4);
                        int week_nub = GetIso8601WeekOfYear(datetime);
                        string week_num_str = week_nub.ToString();
                        graphic.DrawString(cellval4, new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startXd + offsetX - 100, startYd + offsetYpic + 30 + offsetTicket);
                        graphic.DrawString(week_num_str, new Font("Courier New", 12, FontStyle.Bold), new SolidBrush(Color.Black), startXd + offsetX - 10, startYd + offsetYpic + offsetTicket);
                        currentpage = currentpage + 1;
                        offsetTicket +=0;
                }
                    nbr_pages += nbr_rep;
                    nbr_page_txt.Text = nbr_pages.ToString();
                    current_txt.Text = currentpage.ToString();
                }
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.