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
C#
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
C#
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();
}
Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 21, 2024, 3:27 PM EDT
Hi Gcobani,
I reviewed your code and it looks like you are trying to dynamically select a printer and print directly from a Windows Forms application using Crystal Reports. There are a few points in your code where you are setting the printer name, but it seems like it might not be correctly configured or refreshed before the printing starts.
I will show you more clear approach to handle printing with a selected printer for both A4 and thermal printers, you can take refrence rom this code:
Note: Ensure that the printer name is set correctly and the report is refreshed after setting the printer. This is critical because any changes to the printer settings need to be refreshed in the report before executing the print command.
// This method configures and executes printing based on the selected printer and options. private void PrintReport(string reportName, DataTable dataTable) { string appPath = DevEnvironment == '1' ? Path.Combine(path, $@"Report\{reportName}") : Path.Combine(Application.StartupPath, $@"Report\{reportName}"); cry.Load(appPath); cry.SetDataSource(dataTable); crystalReportViewer1.ReportSource = cry; // Set the selected printer cry.PrintOptions.PrinterName = cboPrinter.Text; cry.PrintToPrinter(1, false, 0, 0); }// Button click event to handle printing private void btnPrint_Click(object sender, EventArgs e) { if (ID > 0) { // Validation and data preparation code here... DataTable dtPrintedBarcode = CreateBarcodeDataTable(); // Fill dtPrintedBarcode with data... string reportName = GetReportNameBasedOnSelection(); // Implement this based on your cboPrintOption logic PrintReport(reportName, dtPrintedBarcode); } else { MessageBox.Show("Please select a record to print.", "Record Selection Required"); } }// Create and configure the barcode data table private DataTable CreateBarcodeDataTable() { DataTable dt = new DataTable(); // Define columns and configure the DataTable... return dt; }// Determine the report name based on user selection private string GetReportNameBasedOnSelection() { switch (cboPrintOption.SelectedValue.ToString()) { case "1": // Thermal Printer Small return Properties.Settings.Default.ReportNameSmallThermal; case "2": // Thermal Printer Large return Properties.Settings.Default.ReportNameLargeThermal; case "3": // A4 if (cboPrinter.Text.Contains("ZDesigner")) { MessageBox.Show("A4 cannot be printed to the selected Printer. Please choose a different printer", "Invalid Printer"); return null; } return Properties.Settings.Default.ReportNameA4; default: throw new InvalidOperationException("Unsupported print option selected."); } }