Hi Guys,
After Lot of searching and 5 hours of head banging i finally solved how we can print reports without preview, this is particularly helpful when you have report like labels.
Below is Printing class:
using
System;
using
System.IO;
using
System.Data;
using
System.Text;
using
System.Drawing.Imaging;
using
System.Drawing.Printing;
using
System.Collections.Generic;
using
Microsoft.Reporting.WebForms;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Windows.Forms;/// <summary>
///
Summary description for Printing
///
this is the cool code found on MSDN site to print labels out without preview
///
abhay maini
///
</summary>
public
class Printing : IDisposable
{
public Printing()
{
//
// TODO: Add constructor logic here
//
}
public int m_currentPageIndex;
public IList<Stream> m_streams;
// Routine to provide to the report renderer, in order to
// save an image for each page of the report.public Stream CreateStream(string name,string fileNameExtension, Encoding encoding,string mimeType, bool willSeek)
{
string CurrentDrive;CurrentDrive = Application.StartupPath.ToString();Stream stream = new FileStream("C:\\Labels\\" + name + "." + fileNameExtension, FileMode.Create);
m_streams.Add(stream);
return stream;
}
public void Export(LocalReport report)
{
string deviceInfo =
"<DeviceInfo>" +" <OutputFormat>EMF</OutputFormat>" +
" <PageWidth>4.0in</PageWidth>" +" <PageHeight>2.0in</PageHeight>" +
" <MarginTop>0.00in</MarginTop>" +" <MarginLeft>0.00in</MarginLeft>" +
" <MarginRight>0.00in</MarginRight>" +" <MarginBottom>0.00in</MarginBottom>" +
"</DeviceInfo>";Warning[] warnings;m_streams = new List<Stream>();
report.Render(
"Image", deviceInfo, CreateStream, out warnings);foreach (Stream stream in m_streams)
stream.Position = 0;
}
// Handler for PrintPageEventspublic void PrintPage(object sender, PrintPageEventArgs ev)
{
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
public void Print(string PrinterName)
{
// const string printerName = PrinterName;
if (m_streams == null || m_streams.Count == 0)
return;PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = PrinterName;
if (!printDoc.PrinterSettings.IsValid)
{
string msg = String.Format(
"Can't find printer \"{0}\".", PrinterName);MessageBox.Show(msg, "Print Error");return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
}
// Create a local report for Report.rdlc, load the data,
// export the report to an .emf file, and print it.public void Run(string ReportName, string PrinterName, DataTable MyDataTable,string DSstring)
{
LocalReport report = new LocalReport();
report.ReportPath = ReportName;
report.DataSources.Clear();
report.DataSources.Add(new ReportDataSource(DSstring, MyDataTable));
Export(report);
m_currentPageIndex = 0;
Print(PrinterName);
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}
}
The above class can be called as below behind text change event:
protected void TxtScanId_TextChanged(object sender, EventArgs e)
{
string sqlPrintScanID = "SELECT [ScanID], [LoadID], [tempVRMA], [CustPalletID], [TypeOfAsset] FROM [SerialScanDetail] WHERE [ScanID]=" + TxtScanId.Text + "";
string strConnection = ConfigurationManager.ConnectionStrings["RevisionConnectionString"].ToString();SqlConnection conn = new SqlConnection(strConnection);
SqlDataAdapter da = new SqlDataAdapter();da.SelectCommand = new SqlCommand(sqlPrintScanID, conn);
DataSet ds = new DataSet();da.Fill(ds, "RevisionDataSet_SerialScanDetail");
//ReportViewer1.LocalReport.Refresh();NewPrinting.Run(@"Reports\Report_ScanID.rdlc", "Eltron 2442", ds.Tables[0], "RevisionDataSet_SerialScanDetail");
}
Please let me know if you have difficulty understanding this code, i will be happy to help you out.