cancel
Showing results for 
Search instead for 
Did you mean: 

Setting PrinterSettings.PrinterName leads to printing of junk value on print of crystal report

0 Kudos

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,

CrystalDecisions.CrystalReports.Engine-13.0.2000.0

CrystalDecisions.Shared-13.0.2000.0



My code is

    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";

Accepted Solutions (1)

Accepted Solutions (1)

ido_millet
Active Contributor

Sounds like a bad/incompatible printer driver.

0 Kudos

As you said 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.

Answers (5)

Answers (5)

0 Kudos

The point is using my test app to see if it works, you can use P2P or POC to do the printing. You don't have to preview it first.

P2P uses your default printer the report was designed with, it won't change to to your default printer.

As Ido suggested it sounds like your default printer is not what was used when you designed the report.

0 Kudos

Yes my default printer is not the same when i designed the report .The project was developed in a different country in VB.NET webforms and was running successfully.I changed to a different country and was converting the project to WPF from web forms and thats when the trouble started .The crystal reports .rpt files used are still from the older source.I thought may be thats the issue and created crystal reports new .rpt files in the WPF project .But still getting junk values.

I changed the printer drivers too and put settings similar to a printer from old source where printing was successful ,but still it prints same junk value.

I will use your test app and print now and get back to you soon.

0 Kudos

Cool report, not much help but it looks interesting...

So are you using PrintToPrinter (P2P) or PrintOutputController (POC) to print?

CR will not attach a licensed font when exporting to PDF, not allowed to.

What CR or Adobe does is use Font substitutions, it looks like that is what is happening and it's either using some Symbol font or some none unicode font that is being interpreted to single or double byte.

So what font are you using?

Try a different one

Try exporting to a different format like Word

Did you test using my Printer test app?

What happens in CR Designer when you export and print?

0 Kudos

Hi,

I am using ReportDocuments PrintToPrinter method .

I was using Arial Font in the crystal report .I changed the Font to Consolas and i still get the same issue .

I can export to Disk successfully into PDF format ,only when i print a document i face the issue .

No i didnt test using your printer test app because my requirement of project is that user shouldnt get an option where he chooses printer or number of copies ,no print dialog or any interface.

In CR Desginer,in the preview tab when i print the report by using print button i get junk value again.

0 Kudos

What do you mean by junk characters?

If it's boxes itt means the font is missing.

Take a screen shot of the viewer so we can see what you mean?

0 Kudos

There is no viewer in my project ,i load the report and send directly to printer without a viewer .I could export to disk successfully with correct content .But when i try to print directly to printer i get junk values in print out ,there are no boxes .Please find attached screenshot of the printout .I get around 15 -20 pages of single line printed on each page as below screenshot

0 Kudos

On my WIKI page I wrote a simple Printer test app:

Printing Crystal Reports in .NET

Save the report with data and see if that works using both P2P and POC routines.

Don

0 Kudos

I downloaded 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 .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();
pguss_sri
Participant
0 Kudos

I see you are using .NET Framework 4.7.2, but have .NET Framework 2.0 version of Crystal 13.0.2000.0.
You should use .NET Framework 4.X version of Crystal 13.0.4000.0. This would be SP 26 or higher.
Please Review this Wiki for how to upgrade: https://wiki.scn.sap.com/wiki/display/BOBJ/Crystal+Reports%2C+Developer+for+Visual+Studio+Downloads

0 Kudos

I changed my crystal report dll to 13.0.4000.0 but still facing the same issue

pguss_sri
Participant
0 Kudos

I would try test printing to Windows 10's built in Microsoft Print to PDF Printer to see if it is an issue with your Printer Driver or Crystal. FYI, I have very similar code running in an application using C# .NET Framework 4.6.2 with Crystal Runtime SP29 and it is working well at several sites.

0 Kudos

I downloaded 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 .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();