cancel
Showing results for 
Search instead for 
Did you mean: 

Bug Crystal Report getting good printer with reportDocument.PrinttoPrinter()

0 Kudos

I have an ASP.NET app on a domain server, linked to network printers over IP ports.

When I Use PrintToPrinter() function with the good printer name found, it sometimes not goes through the good printer.

Code:

public void Print()
{
   if (((TraceSwitch)Tracing.Switches["development"]).Level >= TraceLevel.Verbose)
   {
      string printerName = "Printer Laser 1"
      string installedPrinters = "Installed Printers: " + String.Join(",", PrinterSettings.InstalledPrinters.Cast<string>().ToList());
      Tracing.Write(installedPrinters, TraceLevel.Verbose);
      Tracing.Write("Report printer name: " + printerName, TraceLevel.Verbose);
   }
   
   bool printerExists = false;
   
   // Not using linq here, i want see where the compare does not work at the client site.
   foreach (string printer in PrinterSettings.InstalledPrinters)
   {
      Tracing.Write("Compare Installed printer: " + printer + ", Report printer: " + printerName, TraceLevel.Verbose);
      if (printer.Equals(printerName, StringComparison.InvariantCultureIgnoreCase))
      {
         Tracing.Write("Compare successful", TraceLevel.Verbose);
         printerExists = true;
         break;
      }
   }

   if (printerExists)
   {
      Tracing.Write("Printer found: " + printerName, TraceLevel.Verbose);
      Print(printerName);
   }
   else
   {
      Print(null);
   }
}

public void Print(string printerName)
{
   CrystalReportSource crs  = null;
   ReportDocument reportDoc = null;
   PrintDocument docToPrint = null;

   try
   {
      crs = GetCrystalReportSource(this.DataSet(), FullReportName);
      reportDoc = crs.ReportDocument;

      //A print Document is needed to be able to loop through the PaperSize based on a selected printer
      docToPrint = new PrintDocument();

      //If the printer exists, it will set it as the printer then the PaperSize collection will change and we can get the right one.
      if (printerName != null)
      {
         docToPrint.PrinterSettings.PrinterName = printerName;
         reportDoc.PrintOptions.NoPrinter = false;
         reportDoc.PrintOptions.PrinterName = printerName;

         PaperSize sizes = null;

         foreach (PaperSize size in docToPrint.PrinterSettings.PaperSizes)
         {
            if (size.PaperName == this.AppReport.PaperName)
               sizes = size;
         }
         if (sizes != null)
         {
            docToPrint.PrinterSettings.DefaultPageSettings.PaperSize = sizes;
            reportDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)sizes.RawKind;
         }
      }

      reportDoc.PrintToPrinter(1, false, 0, 0);
   }
   finally
   {
      // Release disposable resource
      if (docToPrint != null)
      {
         docToPrint.Dispose();
         docToPrint = null;
      }
      if (crs != null)
      {
         DisposeCrystalReportSource(crs); //dispose every sub section of the report
         crs = null;
         GC.Collect();         // Force garbage collection to alleviate infamous Crystal Reports leaks.
      }
   }
}
DellSC
Active Contributor

Does this happen with a specific printer? Or do specific printers sometimes work and sometimes not work?

-Dell

0 Kudos

dell.stinnett-christy

We experiment this issue with a Zebra Printer for labelling connected on the server with a TCP/IP Port, and a HP LaserJet printer.

I don't know the specific model numbers of the printers.

I wrote sometimes, but we experimented some random behaviors using other domain accounts instead of AppIdentityPool in the IIS settings.

View Entire Topic
0 Kudos

What SDK are you using?

What SP are you using?

I had R&D work this into the .NET components, when designing reports set the printer to the Zebra printer and use a custom paper size and save the report.

Then create the same Paper size Name on all printers.

When CR prints it will look for the custom paper size by name and use it.

Also check on Dissociate.

So, update your reports to use the Zebra printer, don't use a HP printer driver. P2P will always try to print to the printer saved in the RPT file.

Otherwise use PrintOutputController to set the printer name and paper size.

P2P will always default to the saved printer name, that's how it works and is by design.

Don