15

I have an application where I have to print a RDLC report without showing the printDialog and using the default specified printer defined in the application. Below is my test implementaion code.

    Microsoft.Reporting.WinForms.ReportViewer reportViewerSales = new    Microsoft.Reporting.WinForms.ReportViewer();
    Microsoft.Reporting.WinForms.ReportDataSource reportDataSourceSales = new Microsoft.Reporting.WinForms.ReportDataSource();

    reportViewerSales.Reset();
        reportViewerSales.LocalReport.ReportPath = @"Sales.rdlc";

        reportDataSourceSales.Name = "SalesTableDataSet";

        int i = 1;
        foreach (Product item in ProductSalesList)
        {
            dataset.CurrentSales.AddCurrentSalesRow(i, item.Name, item.Quantity.ToString(), item.Price.ToString(), item.Price.ToString());
            i++;
        }
        reportDataSourceSales.Value = dataset.CurrentSales;
        reportViewerSales.LocalReport.DataSources.Add(reportDataSourceSales);
        dataset.EndInit();

        reportViewerSales.RefreshReport();
        reportViewerSales.RenderingComplete += new RenderingCompleteEventHandler(PrintSales);

And here is my Rendering Complete Method

public void PrintSales(object sender, RenderingCompleteEventArgs e)
    {
        try
        {

            reportViewerSales.PrintDialog();
            reportViewerSales.Clear();
            reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
        }
        catch (Exception ex)
        {
        }
    }
StuartLC
  • 104,537
  • 17
  • 209
  • 285
Redone
  • 1,253
  • 5
  • 18
  • 37

4 Answers4

13

I just gave a quick look to a class I created to print directly and I think I took some ideas from this walkthrough: Printing a Local Report without Preview

Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
tezzo
  • 10,858
  • 1
  • 25
  • 48
12

i have made an extension class to @tezzos answer. which might make it more easier.

use this Gist Here to get the extension class i wrote. include it to your project. don't for get namespace :D

LocalReport report = new LocalReport();
            report.ReportEmbeddedResource = "Your.Reports.Path.rdlc";
            report.DataSources.Add(new ReportDataSource("DataSet1", getYourDatasource()));
            report.PrintToPrinter();

PrintToPrinter Method will be available on LocalReport. Might Help someone

shakee93
  • 4,976
  • 2
  • 28
  • 32
  • 1
    did help someone. and even better: it also solves the problem of unattended server-side printing. The only thing I had to adjust was the page dimensions, as we're using DIN-A4 here, easily done in `deviceInfo`. Is it really necessary to clear the canvas with an opaque white rectangle? – Cee McSharpface Jun 25 '18 at 10:34
0

Download C# full Project file

using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            this.UiLabelUpTableAdapter.Fill(this.POSDataSet.UiLabelUp);

            this.reportViewer1.RefreshReport();
        }

        private void buttonPrintReport_Click(object sender, EventArgs e)
        {
            ReportViewer InvoiceReport = new ReportViewer();            
            InvoiceReport.LocalReport.ReportPath = @"C:\Users\Chamod\source\repos\WindowsFormsApp\WindowsFormsApp\Report1.rdlc";

            ReportDataSource DataSet1 = new ReportDataSource("DataSet1", UiLabelUpBindingSource);

            InvoiceReport.LocalReport.DataSources.Add(DataSet1);

            LocalReport lr = InvoiceReport.LocalReport;
            lr.PrintToPrinter();
            InvoiceReport.Clear();
        }
    }
}




using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;

namespace WindowsFormsApp
{
    public static class LocalReportExtensions
    {

        public static void PrintToPrinter(this LocalReport report)
        {
            PageSettings pageSettings = new PageSettings();
            pageSettings.PaperSize = report.GetDefaultPageSettings().PaperSize;
            pageSettings.Landscape = report.GetDefaultPageSettings().IsLandscape;
            pageSettings.Margins = report.GetDefaultPageSettings().Margins;
            Print(report, pageSettings);
        }

        public static void Print(this LocalReport report, PageSettings pageSettings)
        {
            string deviceInfo =
                $@"<DeviceInfo>
                    <OutputFormat>EMF</OutputFormat>
                    <PageWidth>{pageSettings.PaperSize.Width * 100}in</PageWidth>
                    <PageHeight>{pageSettings.PaperSize.Height * 100}in</PageHeight>
                    <MarginTop>{pageSettings.Margins.Top * 100}in</MarginTop>
                    <MarginLeft>{pageSettings.Margins.Left * 100}in</MarginLeft>
                    <MarginRight>{pageSettings.Margins.Right * 100}in</MarginRight>
                    <MarginBottom>{pageSettings.Margins.Bottom * 100}in</MarginBottom>
                </DeviceInfo>";
            Warning[] warnings;
            var streams = new List<Stream>();
            var pageIndex = 0;
            report.Render("Image", deviceInfo,
                (name, fileNameExtension, encoding, mimeType, willSeek) =>
                {
                    MemoryStream stream = new MemoryStream();
                    streams.Add(stream);
                    return stream;
                }, out warnings);
            foreach (Stream stream in streams)
                stream.Position = 0;
            if (streams == null || streams.Count == 0)
                throw new Exception("No stream to print.");
            using (PrintDocument printDocument = new PrintDocument())
            {
                printDocument.DefaultPageSettings = pageSettings;
                if (!printDocument.PrinterSettings.IsValid)
                    throw new Exception("Can't find the default printer.");
                else
                {
                    printDocument.PrintPage += (sender, e) =>
                    {
                        Metafile pageImage = new Metafile(streams[pageIndex]);
                        Rectangle adjustedRect = new Rectangle(e.PageBounds.Left - (int)e.PageSettings.HardMarginX, e.PageBounds.Top - (int)e.PageSettings.HardMarginY, e.PageBounds.Width, e.PageBounds.Height);
                        e.Graphics.FillRectangle(Brushes.White, adjustedRect);
                        e.Graphics.DrawImage(pageImage, adjustedRect);
                        pageIndex++;
                        e.HasMorePages = (pageIndex < streams.Count);
                        e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
                    };
                    printDocument.EndPrint += (Sender, e) =>
                    {
                        if (streams != null)
                        {
                            foreach (Stream stream in streams)
                                stream.Close();
                            streams = null;
                        }
                    };
                    printDocument.Print();
                }
            }
        }
    }
}
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 12 '22 at 20:40
-3
public void PrintSales(object sender, RenderingCompleteEventArgs e)
{
    try
    {
        reportViewerSales.PageSetupDailog();
        reportViewerSales.PrintDialog();
        reportViewerSales.Clear();
        reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
    }
    catch (Exception ex)
    {
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    Welcome to Stackoverflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. You also should use code formatting when having code inside an answer. – Max Feb 17 '16 at 15:56
  • I don't think to. `PageSetupDialog`, if spelled like this, would open an interactive dialog which is the opposite of OP's intention (*without showing...*) – Cee McSharpface Jun 25 '18 at 10:31