Click here to Skip to main content
15,921,371 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hello, i created an RDLC Receipt for POS system in c#, now i want ti print it directly on button click, it also has search parameters it. Like it generated in Report Viewer and from report viewer i want to print it directly without showing any printer setting window or print preview window..
Please help me

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms;
using System.IO;

namespace testReport
    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 =
                    <PageWidth>{pageSettings.PaperSize.Width * 50}in</PageWidth>
                    <PageHeight>{pageSettings.PaperSize.Height * 80}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>
            Warning[] warnings;
            var streams = new List<Stream>();
            var pageIndex = 0;
            report.Render("Image", deviceInfo,
                (name, fileNameExtension, encoding, mimeType, willSeek) =>
                    MemoryStream stream = new MemoryStream();
                    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.");
                    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);
                        e.HasMorePages = (pageIndex < streams.Count);
                        e.Graphics.DrawRectangle(Pens.Red, adjustedRect);
                    printDocument.EndPrint += (Sender, e) =>
                        if (streams != null)
                            foreach (Stream stream in streams)
                            streams = null;



 private void btnprint_Click(object sender, EventArgs e)
            this.Sales_InfoTableAdapter.FillByid(this.DataSet1.Sales_Info, int.Parse(textBox1.Text));


            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Application.StartupPath + "\\Report1.rdlc";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900