Print RDLC Report without Preview | Print Receipt Without Preview in C#

print receipt without preview print rdlc report without preview

Sometimes you have a report or when developing a POS software in which you create a receipt, which you want to print without showing a preview in ReportViewer. You can print an RDLC report programmatically using LocalReport object and CreateStreamCallback callback function. This class will convert and print the report you have in ReportViewer Control. Just copy and paste this class in the form where ReportViewer is and call the example given below in form load or in reportviewer’s rendering complete method to print rdlc report without preview or print receipt without preview in C# which you created using Report designer tool.

There is an article in MSDN which describes how to print an RDLC report programmatically. Based on the idea of that article, I created an extension method for LocalReport class, called Print to make it easier to print the report without showing the report or any dialog:

It has two overloads:

  • Print(): It uses the default page settings of the report.
  • Print(PageSettings): It uses the page settings object which is passed to the method.

As an example, you can easily use it this way:

this.reportViewer1.LocalReport.Print();

Steps To Use The Class

  • First add a RDLC report in the project and design it as you want to.
  • Then add a ReportViewer Control in the form from the toolbox or just create an object of ReportViewer class, whichever you prefer.
  • Now connect the ReportViewer with the Local report you have created.
  • Add the the class code given below in your form.
  • Just after you have connected the report, add
          this.reportViewer1.LocalReport.Print();
  • Make sure you are running this code in your Form Load method.
  • Now test the project.

Read More C# posts Here

This will do the trick to print RDLC report without preview in ReportViewer control.

LocalReport Print Extension Method

Here is the extension method:

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

public static class LocalReportExtensions
{
    public static void Print(this LocalReport report)
    {
        var 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 =
            $@"
                EMF
                {pageSettings.PaperSize.Width * 100}in
                {pageSettings.PaperSize.Height * 100}in
                {pageSettings.Margins.Top * 100}in
                {pageSettings.Margins.Left * 100}in
                {pageSettings.Margins.Right * 100}in
                {pageSettings.Margins.Bottom * 100}in
            ";

        Warning[] warnings;
        var streams = new List();
        var currentPageIndex = 0;

        report.Render("Image", deviceInfo, 
            (name, fileNameExtension, encoding, mimeType, willSeek) => 
            {
                var 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("Error: no stream to print.");

        var printDocument = new PrintDocument();
        printDocument.DefaultPageSettings = pageSettings;
        if (!printDocument.PrinterSettings.IsValid)
            throw new Exception("Error: cannot find the default printer.");
        else
        {
            printDocument.PrintPage += (sender, e) =>
            {
                Metafile pageImage = new Metafile(streams[currentPageIndex]);
                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);
                currentPageIndex++;
                e.HasMorePages = (currentPageIndex < 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();
        }
    }
}

Originally posted at: Reza Aghaei