/// <summary> /// Muestra el reporte en un mediante el visor de Crystal Reports o /// manda a imprimir el reporte directamente a la impresora configurada. /// </summary> /// <param name="report"> Documento .rpt </param> /// <param name="reportName"> Nombre del reporte. </param> /// <param name="isDialog"> Se mostrará en una ventana tipo Dialogo. </param> /// <param name="owner"> Ventana Principal que utiliza el visor. </param> /// <param name="isDirectPrint"> Bandera de impresion directa. </param> /// <history> /// [edgrodriguez] 16/Jul/2016 Created /// </history> public static void ShowReport(ReportDocument report, string reportName = "", bool isDialog = false, Window owner = null, EnumPrintDevice PrintDevice = EnumPrintDevice.pdScreen, bool IsInvitation = false, int numCopies = 1) { switch (PrintDevice) { case EnumPrintDevice.pdPrinter: var boPrintReportOptions = new PrintReportOptions(); boPrintReportOptions.PrinterName = (IsInvitation) ? ConfigRegistryHelper.GetConfiguredPrinter("PrintInvit") : boPrintReportOptions.PrinterName; if (IsInvitation && string.IsNullOrEmpty(boPrintReportOptions.PrinterName)) { UIHelper.ShowMessage($"The printer is not configured, please configure your printer."); return; } var boReportClientDocument = report.ReportClientDocument; var boPrintOutputController = boReportClientDocument.PrintOutputController; boPrintReportOptions.JobTitle = reportName; boPrintReportOptions.NumberOfCopies = numCopies; boPrintOutputController.PrintReport(boPrintReportOptions); break; case EnumPrintDevice.pdScreen: var crViewer = new frmViewer(report, reportName); crViewer.Owner = owner; if (isDialog) { crViewer.ShowDialog(); break; } crViewer.Show(); break; } }
public Dictionary <string, object> Execute(OutputDocument document, Dictionary <string, object> namedPassThroughParameters) { Dictionary <string, object> results = new Dictionary <string, object>(); bool goOn = true; int retry = 0; string errorText = string.Empty; while (goOn) //Loop to be able to make retries of printouts in case of exceptions. { //There can not be more than 75 reports open at the same time. bool exitWaitLoop = false; while (!exitWaitLoop) { lock (_NumOfConcurrentJobsLockObj) { if (_NumberOfConcurrentJobs < 75) { _NumberOfConcurrentJobs++; exitWaitLoop = true; } } if (!exitWaitLoop) { Thread.Sleep(25); } } try { errorText = string.Empty; using (ReportDocument report = GetReportDocument(document.ReportID, document.ReportFile, out errorText)) { if (report == null || !string.IsNullOrEmpty(errorText)) { throw new Exception("Error loading report\r\n\r\n" + errorText); } DataSet dataSet = null; try { errorText = string.Empty; if (document.MetaParameters[StdMetaParamNames.DocumentIDKey] == "GenericReport") { ConnectionInfo info = new ConnectionInfo(); info.ServerName = _configurationParameters[CONFPARAM_DBNAME]; info.UserID = _configurationParameters[CONFPARAM_DBUSER]; info.Password = _configurationParameters[CONFPARAM_DBPASS]; SetReportConnectionInfo(info, report); ApplyReportParameters(document.Parameters, report, out errorText); } else { dataSet = new DataSet(); document.Data.Seek(0, SeekOrigin.Begin); dataSet.ReadXml(document.Data); //DateTime before = DateTime.Now; report.SetDataSource(dataSet); //DateTime after = DateTime.Now; //System.Diagnostics.Debug.WriteLine("SetDataSource: " + (after - before).ToString()); } if (!string.IsNullOrEmpty(errorText)) { throw new Exception(errorText); } if (Convert.ToBoolean(_configurationParameters[CONFPARAM_OUTPUTENABLE])) { PrintReportOptions clientReportOptions = new PrintReportOptions(); clientReportOptions.PrinterName = document.PrinterDeviceName; clientReportOptions.NumberOfCopies = int.Parse(document.MetaParameters[StdMetaParamNames.NumberOfCopiesKey]); clientReportOptions.Collated = true; clientReportOptions.JobTitle = document.OutputJobId + "_" + document.OutputJobSequence.ToString(); ISCDReportClientDocument clientDoc = report.ReportClientDocument; try { clientDoc.PrintOutputController.PrintReport(clientReportOptions); } catch (Exception ex) { throw new PrintFailureException("Print out failed", ex); } } try { byte[] exportData = null; Stream exportStream = null; lock (_exportLockObj) //Lock this part to avoid unhandled exceptions { exportStream = report.ExportToStream(ExportFormatType.PortableDocFormat); } exportData = new byte[exportStream.Length]; exportStream.Read(exportData, 0, Convert.ToInt32(exportStream.Length)); exportStream.Close(); if (results.ContainsKey(StdResultParamNames.BinaryResultKey)) { results.Remove(StdResultParamNames.BinaryResultKey); } results.Add(StdResultParamNames.BinaryResultKey, exportData); } catch {} //Ignore exceptions from PDF export. } finally { if (dataSet != null) { dataSet.Dispose(); } try { report.Close(); } catch { } finally { report.Dispose(); } lock (_NumOfConcurrentJobsLockObj) { _NumberOfConcurrentJobs--; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } } goOn = false; } catch (PrintFailureException ex) { //Make three tries to print. if (retry >= 3) { string txt = "Error printing Crystal Report\r\n\r\n" + ex.Message; if (ex.InnerException != null) { txt += "\r\n" + ex.InnerException.Message; if (ex.InnerException.InnerException != null) { txt += "\r\n" + ex.InnerException.InnerException.Message; } } throw new Exception(txt); } else { retry++; goOn = true; System.Threading.Thread.Sleep(1000); } } catch (Exception ex) { goOn = false; string txt = "Error printing Crystal Report\r\n\r\n" + ex.Message; if (ex.InnerException != null) { txt += "\r\n" + ex.InnerException.Message; if (ex.InnerException.InnerException != null) { txt += "\r\n" + ex.InnerException.InnerException.Message; } } throw new Exception(txt); } } return(results); }