Printing a RDLC without preview

Last post 10-29-2009 3:03 PM by sravankasyapk. 3 replies.

Sort Posts:

  • Printing a RDLC without preview

    06-11-2009, 5:08 PM
    • Member
      4 point Member
    • mainiabhay
    • Member since 09-23-2008, 8:21 PM
    • Itasca,US
    • Posts 4

    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 PrintPageEvents

    public 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.

    abhay maini
  • Re: Printing a RDLC without preview

    07-21-2009, 8:05 AM
    • Member
      4 point Member
    • ik78
    • Member since 07-01-2008, 8:17 AM
    • Posts 13

    Hi

    Is thier any way to do the same with out saving to a file, (to avoid deleting the file)

    Thanks
    Itzik

  • Re: Printing a RDLC without preview

    09-20-2009, 11:54 AM
    • Member
      2 point Member
    • jbenzan
    • Member since 09-20-2009, 3:52 PM
    • Posts 1

    Hi, i just used this code and it works perfectly but it only prints when i run it from visual studio. But when i compile the web app and run it from IIS it generates the image but doesn't print and doesn't throw any error.

    Any sugestions?

  • Re: Printing a RDLC without preview

    10-29-2009, 3:03 PM
    • Member
      6 point Member
    • sravankasyapk
    • Member since 04-12-2008, 11:57 AM
    • Posts 3

    Hi, this is a great article... i am not that familiar with the Reporting services printing concepts. I am a sharepoint developer. I have created an asp.net web application. I wanted to print a control on web page say datagrid without print preview. Can this be acheived with above code? What my actual doubt is that, if we open the webform from a different computer than server, what happens? will the document still be printed on the server or client machine? Please let me know. I really appreciate your time and any help will be greatly appreciated.

    My email id is sravankasyapk@gmail.com

    Thanks & Regards,

    Sravan Kasyap Karanam

Page 1 of 1 (4 items)