Using Setting PrinterSettings.PrinterName leads to printing of junk value on print of crystal report or even when directly printing a pdf document junk values printed

krishna 466 Reputation points
2022-02-13T08:51:14.687+00:00

Hi,
I am trying to print a crystal report in WPF C# by sending it directly to printer without a viewer .The user can select different printers based on a drop down .However when i try to set PrinterSettings.PrinterName="PrinterName" ,the printer is printing junk values.I am using VS2017,WPF ,OS-Windows 10,.NET Framework-4.7.2

ReportDocument ObjDoc = new ReportDocument();
    System.Drawing.Printing.PrintDocument pDoc = new System.Drawing.Printing.PrintDocument();
    CrystalDecisions.Shared.PrintLayoutSettings PrintLayout = new CrystalDecisions.Shared.PrintLayoutSettings();
    System.Drawing.Printing.PrinterSettings printerSettings = new System.Drawing.Printing.PrinterSettings();
    System.Drawing.Printing.PageSettings pSettings = new System.Drawing.Printing.PageSettings(printerSettings);
    System.Drawing.Printing.PageSettings pageSettings = new System.Drawing.Printing.PageSettings(printerSettings);
    //Fetch Values in dataset cdtUTDocEng
      ObjDoc.Load("//ServerName//Crystal_Reports//VHRSSALEFDE002.rpt");
                                    if (cdtUTDocEng.Rows.Count > 0)
                                        ObjDoc.SetDataSource(cdtUTDocEng);
                                    else
                                    {
                                        lsPrintMessage = "Printing Failed -Failed to fetch Undertaking English details";
                                        break;
                                    }


                                    if (cEnvironment.Production == psEnvironment)
                                    {

                                        ObjDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Default;
                                        lsPrinterName = cmbPrinter.SelectedValue.ToString().Trim();
                                        if (CheckifPrinterInstalled(cmbPrinter.SelectedValue.ToString().Trim()) == true)
                                        {


    System.Drawing.Printing.PrinterSettings.PrinterName =cmbPrinter.SelectedValue.ToString().Trim();
                                            ObjDoc.PrintToPrinter(printerSettings, pSettings, false, PrintLayout);
                                            ObjDoc.Dispose();


 lsPrintMessage = lsPrintMessage + "Full documentation English " + 
pdtPrintDetails.Rows[lirow]["COPY_TYPE"].ToString().Trim() + " has been 
sent to printer " + cmbPrinter.SelectedValue.ToString().Trim() + 
Environment.NewLine;
                                        }
                                        else

 lsPrintMessage = "This printer " + 
cmbPrinter.SelectedValue.ToString().Trim() + " is not installed on 
server,cannot print";
                                    }

This line causes the issues

 System.Drawing.Printing.PrinterSettings.PrinterName =cmbPrinter.SelectedValue.ToString().Trim();

If i remove this line,the printer prints to my default printer and print comes correctly

I tried various combinations of setting printer name with the below values,all print junk values

 1)printerSettings.PrinterName= "Dyna_Offshore";
       2)pSettings.PrinterSettings.PrinterName= "Dyna_Offshore";
       3)System.Drawing.Printing.PrinterSettings.PrinterName = "\\\\My PC IP\\Dyna_Offshore";

I tried setting printer name on report document like this ,but the code ignores this setting and takes PrinterName from printerSettings

1)ObjDoc.PrintOptions.PrinterName= "Dyna_Offshore";

I then tried downloading the report to my desktop ,the document was downloaded with correct content .Then i used PrintDocument to print the document in my desktop ,junk values were printing again .I think its some issue with PrinterSettings property.

ExportFormatType formatType = ExportFormatType.NoFormat;
                                        formatType = ExportFormatType.PortableDocFormat;
                                       ObjDoc.ExportToDisk(formatType, exportPath + "\\UndertakingDoc.pdf");
                                        ObjDoc.Dispose();
                                        PrintDocument pd = new PrintDocument();
                                        pd.PrinterSettings.PrintFileName= exportPath + "\\UndertakingDoc.pdf";
                                        pd.PrinterSettings.PrinterName = cmbPrinter.SelectedValue.ToString().Trim();

pd.Print();
Developer technologies | Windows Presentation Foundation
{count} votes

2 answers

Sort by: Most helpful
  1. krishna 466 Reputation points
    2022-02-15T06:03:15.717+00:00

    Hi ,
    @Hui Liu-MSFT

    https://drive.google.com/file/d/1VcMWckf0ouxKiKKaKVtjW-HaCdOqt-Qk/view?usp=sharing

    This is a working test application with just enough code to print a document and reproduce the issue .Please Click on print button ,i populated a dataset with sample data and added my report to the project,i am getting junk value printed in this case in printer.


  2. krishna 466 Reputation points
    2022-02-20T10:17:37.34+00:00

    It was printer driver issue.In WPF VS2017 ,when we are printing what ever driver name is printed on the physical printer .The printer needs to be installed with the exact driver name .If there is even a slight mismatch in the driver name ,it was causing junk values to be printed when printing programmatically through VS2017.Normal printing will work if there is mismatch in drivers.Also this issue is not there in VS2010 with Webforms .I have tested this in 2 printers in different countries and i was able to print successfully.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.