Create a PDF file using Crystal Report in C#

Crystal Report’s “ExportToStream” function can export a pre designed report into various file format and “ExportToDisk” function can export the entire formated data into a specified file. In this article we’ll use the “ExportToDisk” function, which allows us to convert the report into a PDF file and save it in a folder. The file can then be emailed or printed.

Create PDF using Crystal report

Note: We assume that you have read our article on How to create Crystal Report in Asp.Net?

Please read the above article first, since in this article we will use the same file and database format for the demo.

Once you have created a project with the Crystal report, just write another function (CreatePDF) which will create a PDF file using the data extracted from the database.

 

Also add few crystal report references to your project. To add references, click “Website” from the top menu select “Add Reference…” menu. In the “.Net” tab search for,

1) CrystalDecisions.CrystalReports.Engine
2) CrystalDecisions.Shared

 

using System;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;                  // FOR DataSet().
using CrystalDecisions.Shared;      // FOR ExportFormatType.

public partial class _Default : System.Web.UI.Page 
{
    // CONNECTION PROPERTIES.
    SqlConnection myConn = default(SqlConnection);
    SqlCommand sqComm = default(SqlCommand);

    public CrystalDecisions.CrystalReports.Engine.ReportDocument Report = 
        new CrystalDecisions.CrystalReports.Engine.ReportDocument();

    protected void btPDF_Click(object sender, EventArgs e)
    {
        if (setConn())
        {
            string sQuery = "SELECT *FROM dbo.Employee";
            CreatePDF(sQuery, "Employee.rpt", "Employee");
        }
    }

    private bool CreatePDF(string sQuery, string rptFileName, string sTableName)
    {
        System.Data.SqlClient.SqlDataAdapter objDataAdapter = 
            new System.Data.SqlClient.SqlDataAdapter(sQuery, myConn);

        DataSet objDataSet = new DataSet()>;
        objDataAdapter.SelectCommand.CommandTimeout = 100;
        objDataAdapter.Fill(objDataSet, sTableName);

        Report.Load(Server.MapPath("report\\") + rptFileName);
        Report.SetDataSource(objDataSet);                  // SET REPORT DATA SOURCE.

        // FINALLY, CREATE THE PDF FILE USING EMPLOYEE DETAILS.
        Report.ExportToDisk(ExportFormatType.PortableDocFormat, 
            Server.MapPath("Crystal-to-PDF\\EmployeeDetails.pdf"));

        return true;
   }

    private bool setConn()
    {
        // SET DATABASE CONNECTION.
        try
        {
            myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DNA_DB"].ConnectionString);
            myConn.Open();            sqComm = new SqlCommand();
            sqComm.Connection = myConn;
        }
        catch (Exception ex) { return false; }
        return true;
    }
}

Our function “CreatePDF” is called from the button click event and it takes 3 parameters.

1) sQuery: The “Sql” query that will fetch the details from the “Employee” table.

2) rtpFileName: The “Crystal Report” file which we have designed earlier. This is the file which will be converted into “PDF”.

3) sTableName: The name of the “DataSet” which holds the table data and will be used to design the report.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s