Search code examples
c#asp.netpdfcrystal-reportscrystal-reports-2010

Crystal Reports directly download into pdf C#


I want to directly download crystal reports into pdf together viewing in CommonReportViewer.aspx page, I am running my project on local server with source MYPROJECT and (.rpt) reports on D:\MYPROJECT\Reporting\Deposit\Reports. I have tried different solutions at Stackoverflow i.e ExportToDisk,ExportOptions and more but can't get the solution. In my case to view bank reports i supplied report parameters through a form and finally reaching report parameters and report is displayed. My Problem is how can i download that report directly in pdf as it is required for me to print multiple pages of report.

CommonReportViewer.aspx.cs:

  public partial class CommonReportViewer : System.Web.UI.Page
    {
        ReportDocument mainDoc = new ReportDocument();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {

                CrystalReport report = new CrystalReport();
                report = (CrystalReport)Session["NLK_REPORT"];
                DateTime t1 = DateTime.Now;
                mainDoc.Load(report.ReportFilePath, OpenReportMethod.OpenReportByDefault);
                mainDoc.RecordSelectionFormula = report.SelectionCriteria;

                mainDoc.Refresh();
                string server = "NLKNEWDB";
                string userID = report.UserID;
                string password = report.Password;

                this.ApplyLogonInfo(mainDoc, server, userID, password);


                foreach (ReportParameter param in report.ParamList)
                {
                    mainDoc.SetParameterValue(param.ParamName, param.ParamValue);
                }

                foreach (SubReport sr in report.SubReportList)
                {
                    this.ApplyLogonInfo(mainDoc, server, userID, password);
                    foreach (ReportParameter param in sr.ParamList)
                    {

                        mainDoc.SetParameterValue(param.ParamName, param.ParamValue, sr.SubReportName);
                    }
                }

                mainDoc.SetDatabaseLogon(userID, password, server, "");

                this.crptViewer.ReportSource = mainDoc;

                mainDoc.DataSourceConnections.Clear();


            }
            finally
            {

            }
        }

        void ApplyLogonInfo(ReportDocument report, string server, string userID, string password)
        {
            TableLogOnInfo info = new TableLogOnInfo();
            ConnectionInfo cinfo = new ConnectionInfo();

            cinfo.ServerName = server;
            cinfo.UserID = userID;
            cinfo.Password = password;
            info.ConnectionInfo = cinfo;

            foreach (CrystalDecisions.CrystalReports.Engine.Table tbl in report.Database.Tables)
            {
                tbl.ApplyLogOnInfo(info);
            }
        }

        private void Page_Unload(object sender, System.EventArgs e)
        {
            if (mainDoc != null)
            {
                mainDoc.Close();
                mainDoc.Dispose();
            }

        }
    }

Solution

  • Some changes should be done to export it to PDF format. When the page loads, changes in the Page_Load Method as.

     protected void Page_Load(object sender, EventArgs e)
            {
                try
                {
    
                    CrystalReport report = new CrystalReport();
                    report = (CrystalReport)Session["NLK_REPORT"];
                    DateTime t1 = DateTime.Now;
                    mainDoc.Load(report.ReportFilePath, OpenReportMethod.OpenReportByDefault);
                    mainDoc.RecordSelectionFormula = report.SelectionCriteria;
                    mainDoc.Refresh();
    
                    string server = "NLKNEWDB";
                    string userID = report.UserID;
                    string password = report.Password;
                    string empId = null;
    
                    this.ApplyLogonInfo(mainDoc, server, userID, password);
    
                    foreach (ReportParameter param in report.ParamList)
                    {
                        mainDoc.SetParameterValue(param.ParamName, param.ParamValue);
                        if (param.ParamName == "P_EMPLOYER_ID")
                        {
                            empId = param.ParamValue.ToString();
                        }
                    }
    
                    foreach (SubReport sr in report.SubReportList)
                    {
                        this.ApplyLogonInfo(mainDoc, server, userID, password);
                        foreach (ReportParameter param in sr.ParamList)
                        {
    
                            mainDoc.SetParameterValue(param.ParamName, param.ParamValue, sr.SubReportName);
                        }
                    }
    
                    mainDoc.SetDatabaseLogon(userID, password, server, "");
                    mainDoc.ExportToHttpResponse(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, Response, true, empId);
                    this.crptViewer.ReportSource = mainDoc;
    
                    mainDoc.DataSourceConnections.Clear();
    
    
    
    
                }
                finally
                {
    
                }
            }