Category / Section
Silently printing SSRS, RDL and RDLC reports programmatically in c# without preview
2 mins read
This section describes how to programmatically print a report without viewing in the report viewer. This can be achieved by exporting a report into a PDF format using the Report Writer and resultant stream, which is used in the PdfDocumentView for printing.
You can create a simple Report Writer sample in WPF with the help of below Getting Started documentation. The following code snippet is used to export an RDLC report to a PDF document.
string reportPath = @"../../ReportTemplate/Product Details.rdlc"; ReportWriter reportWriter = new ReportWriter(reportPath); reportWriter.ReportProcessingMode = ProcessingMode.Local; reportWriter.DataSources.Clear(); reportWriter.DataSources.Add(new ReportDataSource { Name = "DataSet1", Value = ProductCatalog.GetData() }); MemoryStream stream = new MemoryStream(); reportWriter.Save(stream, WriterFormat.PDF);
Load the exported stream into the PdfDocumentView using the PdfDocumentView.Load API and add the available printers to a list for printing the report using the Windows PrintDialog as given in the following code example.
PdfDocumentView pdfdoc = new PdfDocumentView(); pdfdoc.Load(stream); var doc = pdfdoc.PrintDocument as IDocumentPaginatorSource; PrintDialog printDialog = new PrintDialog(); List<string> printersList = new List<string>(); List<string> serversList = new List<string>(); var server = new PrintServer(); var queues = server.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local }); foreach (var queue in queues) { if (!serversList.Contains(queue.HostingPrintServer.Name)) { serversList.Add(queue.HostingPrintServer.Name); } printersList.Add(queue.FullName); } server = new PrintServer(serversList[0].ToString()); PrintQueue printer1 = server.GetPrintQueue(printersList[2].ToString()); printDialog.PrintQueue = printer1; printDialog.PrintDocument(doc.DocumentPaginator, "PDF PRINTER");