Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 450.6k

How to print from the selected printers in c#

Jul 19 2024 9:07 AM

Hi Team

I want to get the value and name to my pdf, so that users can print from the selected printers. Currently i can only do that in A4 size not with printers names when selected for PDF, in simple english i cant get property name(Weight with its value) from the printers when selected.

 

// print method

private void btnPrint_Click(object sender, EventArgs e)
        {
            string paths = "", appPath = "";
            try
            {

                if (ID > 0)
                {
                    if (txtBatchNumber.Text == "" || txtNumberOfLabel.Text == "" || txtWeight.Text == "")
                    {
                        MessageBox.Show("Please fill in all required information.", "Required Information");
                        return;
                    }

                    DataRow[] dr = dt.Select("ID=" + ID);
                    var name = dr[0]["Recipe_Name"].ToString();
                    var description = dr[0]["Recipe_Description"].ToString();


                    string reportName = "";


                    string optionSelected = "";
                    if (cboPrintOption.SelectedValue.ToString() == "1") //Thermal Printer Small
                    {
                        optionSelected = "ThermalSmall";
                        reportName = Properties.Settings.Default.ReportNameSmallThermal;


                    }
                    else if (cboPrintOption.SelectedValue.ToString() == "2") //Thermal Printer Large 
                    {
                        optionSelected = "ThermalLarge";
                        reportName = Properties.Settings.Default.ReportNameLargeThermal;

                    }
                    else if (cboPrintOption.SelectedValue.ToString() == "3") //A4
                    {
                        optionSelected = "A4";
                        reportName = Properties.Settings.Default.ReportNameA4;

                    }

                    #region InsertPrintAudit
                    string names = description;
                    string concatName = string.Concat("M", name.Substring(1));
                    int LabelID = 0;
                    string weightDisplay = "Weight: " + txtWeight.Text;
                    #endregion InsertPrintAudit

                    CreateBarcodeDataTable();

                    if (optionSelected == "A4")
                    {
                        if (cboPrinter.Text.Contains("ZDesigner"))
                        {
                            MessageBox.Show("A4 cannot be printed to selected Printer. Please choose a different printer", "Invalid Printer");
                            return;
                        }
                        LabelID = 12345;
                        for (int i = 1; i <= Convert.ToInt32(txtNumberOfLabel.Text); i++)
                        {
                            if (DevEnvironment != '1')
                                LabelID = this.SavePrintingInfo(name, Convert.ToInt32(txtSeriesNumber.Text), txtBatchNumber.Text, Convert.ToDecimal(txtWeight.Text), concatName);
                            dtPrintedBarcode.Rows.Add("*" + LabelID.ToString().PadLeft(8, '0') + "*", names, txtBatchNumber.Text, name, weightDisplay, DateTime.Now.ToString("yyyy-MM-dd")
                                                   );

                        }

                        if (DevEnvironment == '1')
                        {
                            appPath = Path.Combine(path, @"Report\" + reportName);
                        }
                        else
                        {
                            appPath = Path.Combine(Application.StartupPath, @"Report\" + reportName);
                        }

                        cry.Load(appPath);
                        cry.SetDataSource(dtPrintedBarcode);
                        crystalReportViewer1.ReportSource = cry;

                        string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
                        cry.PrintOptions.PrinterName = PrinterName;
                        printDocument1.PrinterSettings.PrinterName = cboPrinter.Text.ToString();
                        cry.Refresh();
                        cry.PrintOptions.PrinterName = cboPrinter.Text.ToString();
                        cry.PrintToPrinter(1, false, 1, 2);

                    }
                    else if (optionSelected == "ThermalSmall")
                    {
                        PrintToThermal(LabelID, names, name, reportName, paths, appPath, concatName);
                        dtPrintedBarcode.Rows.Add("*" + LabelID.ToString().PadLeft(8, '0') + "*", names, txtBatchNumber.Text, name, weightDisplay, DateTime.Now.ToString("yyyy-MM-dd")
                       ) ;

                    }
                    else if (optionSelected == "ThermalLarge")
                    {
                        PrintToThermal(LabelID, names, name, reportName, paths, appPath, concatName);
                        dtPrintedBarcode.Rows.Add("*" + LabelID.ToString().PadLeft(8, '0') + "*", names, txtBatchNumber.Text, name, weightDisplay, DateTime.Now.ToString("yyyy-MM-dd"));

                    }
                    ClearAllInfo();
                }
                else
                {
                    MessageBox.Show("Please select a record to print.", "Record");
                }
                txtSeriesNumber.Text = "";
                crystalReportViewer1.ReportSource = null;
                crystalReportViewer1.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString() + " Path: " + Environment.CurrentDirectory + @"\");
                if (ex.InnerException.ToString().Contains("cannot find the file"))
                {
                    MessageBox.Show("Path:  " + appPath + " " + paths);
                }
            }

        }

 

// Print to names when selected

public void PrintToThermal(int LabelID, string names, string name, string reportName, string paths, string appPath, string concatName)
        {
            LabelID = 123456;
            string weightDisplay = "Weight: " + txtWeight.Text;
            CreateBarcodeDataTable();
            for (int i = 1; i <= Convert.ToInt32(txtNumberOfLabel.Text); i++)
            {
                if (DevEnvironment == '1')
                {
                    appPath = Path.Combine(path, @"Report\" + reportName);
                }
                else
                {
                    LabelID = this.SavePrintingInfo(name, Convert.ToInt32(txtSeriesNumber.Text), txtBatchNumber.Text, Convert.ToDecimal(txtWeight.Text), concatName);
                    appPath = Path.Combine(Application.StartupPath, @"Report\" + reportName);
                }

                dtPrintedBarcode.Rows.Add("*" + LabelID.ToString().PadLeft(8, '0') + "*", names, txtBatchNumber.Text, weightDisplay, concatName, DateTime.Now.ToString("yyyy-MM-dd"));
                
                cry.Load(appPath);
                cry.SetDataSource(dtPrintedBarcode);
                crystalReportViewer1.ReportSource = cry;

                //Default printer
                //string PrinterName = this.printDocument1.PrinterSettings.PrinterName;
                //cry.PrintOptions.PrinterName = PrinterName;
                printDocument1.PrinterSettings.PrinterName = cboPrinter.Text.ToString();
                cry.Refresh();
                cry.PrintOptions.PrinterName = cboPrinter.Text.ToString();
                cry.PrintToPrinter(1, false, 1, 2);

                dtPrintedBarcode.Rows.Clear();
            }
            cry.SetDataSource(dtAllPrintedBarcodes);
            crystalReportViewer1.ReportSource = cry;
            cry.Refresh();
        }

 


Answers (1)