Print Crystal Report Directly in ASP.NET C#

       using System.Drawing.Printing;
       using Crystal Decisions.CrystalReports.Engine;
       using Crystal Decisions.Shared;
       protected void Page_Load(object sender, EventArgs e)
       {
         void();
       }
       public void()
       {
         try
        {
         ReportDocument crystalReport = new ReportDocument();
         crystalReport.Load(Server.MapPath("~/CrystalReport2.rpt"));
         DataSet dsCustomers = GetData("select * from visitor_details where  id ='" + Session["sessionvid"] + "' and  plant ='" + Session["sessionplant"] + "'");

         DataTable dataTable = dsCustomers.Tables[0];   crystalReport.Database.Tables["visitor_details"].SetDataSource((DataTable)dataTable);
          CrystalReportViewer2.ReportSource = crystalReport;
          CrystalReportViewer2.Zoom(100);
          //crystalReportViewer1.ExportReport() ;
          CrystalReportViewer2.RefreshReport();
          crystalReport.PrintOptions.PrinterName = GetDefaultPrinter();
          crystalReport.PrintToPrinter(1, false, 0, 0);
        }
        catch
        {
            Response.Write("<script LANGUAGE='JavaScript' >alert('connect printer settings')</script>");
        }

}


This works for me. You can create your own PageSettings if needed, else just use an emtpy one.

If you want to open a print dialogue, simply use PrintDialog;

using System.Windows.Forms;

//...

ReportClass report = new ReportClass();
report.FileName = @"C:/Layout.rpt";
report.Load();
report.SetDataSource(YourSource);

PrinterSettings settings = new PrinterSettings();

PrintDialog pdialog = new PrintDialog();
if (pdialog.ShowDialog() == DialogResult.OK)
{
    settings = pdialog.PrinterSettings;
}

report.PrintToPrinter(settings, new PageSettings() { }, false);

1. CLIENT-SIDE PRINTING

This is the most ideal method in printing in a Web-based application as most users will be certainly accessing the server remotely.

Put this javascript code inside the head tag of your .aspx page where the crystal report viewer resides.

     <script type="text/javascript">


                function Print() {
                      var dvReport = document.getElementById("dvReport");
                      var frame1 = dvReport.getElementsByTagName("iframe")[0];
                      if (navigator.appName.indexOf("Internet Explorer") != -1) {
                          frame1.name = frame1.id;
                          window.frames[frame1.id].focus();
                          window.frames[frame1.id].print();
                      }
                      else {
                          var frameDoc = frame1.contentWindow ? frame1.contentWindow : frame1.contentDocument.document ? frame1.contentDocument.document : frame1.contentDocument;
                          frameDoc.print();
                      }
                  }
            </script>

With the same page, in the body tag put this

  <body>
        <form id="form1" runat="server">
        <asp:Button ID="btnPrint" runat="server" Text="Print Directly" OnClientClick="Print()"></asp:Button>

          <div id="dvReport">
            <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" ToolPanelView="None" EnableDatabaseLogonPrompt="false"  />


          </div>
        </form>
    </body>

-- take note of the button in there. it should be outside the div that enclosed the crystal report viewer. This will surely work. see Full discussion on this method: http://www.aspsnippets.com/Articles/Print-Crystal-Report-on-Client-Side-on-Button-Click-using-JavaScript-in-ASPNet.aspx


2. SERVER SIDE PRINTING

The most suggested answers here is the printToPrinter() Function. This method is done on server-side, hence, it is limited when you are accessing the server of your Web-based application / website remotely, unless the client can map or have the access with the server printer.


Please read this for further info: http://aspalliance.com/509_Automatically_Printing_Crystal_Reports_in_ASPNET.3