Redone
Redone

Reputation: 1313

How to directly print rdlc report without showing PrintDialog() in C#?

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)
        {
        }
    }

Upvotes: 16

Views: 57572

Answers (4)

Chamod Kavinda
Chamod Kavinda

Reputation: 11

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();
                }
            }
        }
    }
}

Upvotes: 1

shakee93
shakee93

Reputation: 5386

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

Upvotes: 13

Sunil Motwani
Sunil Motwani

Reputation: 1

public void PrintSales(object sender, RenderingCompleteEventArgs e)
{
    try
    {
        reportViewerSales.PageSetupDailog();
        reportViewerSales.PrintDialog();
        reportViewerSales.Clear();
        reportViewerSales.LocalReport.ReleaseSandboxAppDomain();
    }
    catch (Exception ex)
    {
    }
}

Upvotes: -3

tezzo
tezzo

Reputation: 11105

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

Upvotes: 14

Related Questions