Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
>How can I print on two different printers but with different conditions, for example, when a single copy is printed it is printed on printer "X" and if two copies are printed it is printed on printer "Y" in c#.

C#
using (PrintDocument pd = new PrintDocument())
          {
              pd.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
              pd.PrinterSettings.PrinterName = "Datamax";
              pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
              pd.DefaultPageSettings.PaperSize = new PaperSize("Custom", 403, 244);
              pd.Print();
          }
          try
          {
              Conexion.Open();
              System.Diagnostics.Debug.WriteLine("ComboBox" + guna2ComboBox1.SelectedItem);
              SqlCommand cmd1 = new SqlCommand("XUltra", Conexion);
              cmd1.CommandType = CommandType.StoredProcedure;
              cmd1.Parameters.Add("@Maquina", SqlDbType.NVarChar).Value = guna2ComboBox1.SelectedValue;

              ;
              DataTable dtq = new DataTable();
              dtq.Load(cmd1.ExecuteReader());
              dataGridView3.DataSource = dtq;

          }
          catch (Exception ex)
          {
              throw new Exception("", ex);
          }
          Conexion.Close();

C#
<pre> private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            copies--;
            pageNum++;

            // Don't dispose of the e.Graphics object.
            // You didn't create it then its not your call.
            var g = e.Graphics;

            using (var fnt = new Font("Courier New", 14, FontStyle.Bold))
            using (var f = new Font("Courier New", 8, FontStyle.Bold))
                using (var fa = new Font("Courier New",11 , FontStyle.Bold))
            {
                g.DrawString("ATLTZAYANCA", fnt, Brushes.Black, 135, 10);
                g.DrawString(guna2TextBox3.Text, fnt, Brushes.Black, 137, 65);
                //g.DrawImage(picktureBox.Image, 174, 171, 60, 60);
                g.DrawString(guna2ComboBox1.Text,fnt,Brushes.Black, 178, 171);
                g.DrawString($"Etiqueta {pageNum}", f, Brushes.Black, 215, 230);
                g.DrawString($"Cantidad {label9.Text}", f, Brushes.Black, 165, 108);
                g.DrawString($"Tensión {guna2TextBox4.Text}", f, Brushes.Black, 165, 125);
                g.DrawString(guna2TextBox1.Text, fa, Brushes.Black, 135, 40);
                g.DrawString(guna2TextBox2.Text, f, Brushes.Black, 135, 90);

                e.HasMorePages = copies > 0;
            }

        }




This is my code, here I only use one printer and it does not have the condition that I would like it to have so that if two or more copies are printed, they are sent to another printer.

What I have tried:

I already investigated but nothing has come out that helps me with what I need.
Posted
Updated 30-Jan-24 6:53am
v2

1 solution

You can't do it in the PrintPage event handler - that has already processed the print options, and generated a graphics context for a specific printer so it's too late to try and change that.

What you need to do is open a PrintDialog Class (System.Windows.Forms) | Microsoft Learn[^] when you handle the user request to print (i.e. the print button Click event or similar), check the copies count, and then select the right printer before you create the PrintDocument object.

But me? I'd probably be pissed off if you did that: if I pick the printer in my office and your code then sends it to one three floors away without telling me I'm going to wonder where it is and annoy others as well be trying to print it again ... I'd think pretty carefully about the effects on the user and how to mitigate them before I went any further on this route!
 
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