How to export database data to PDF file in ASP.NET

Article Author : Tauseef Ahmad, Article Posted on : 8/16/2017, Article Category :

Download full source code here: How to export database data to PDF file in ASP.NET
please share this article to help others :

Here i have explained with brief detail that how to export database data to PDF file in ASP.Net.

Just follow the steps:


Step - 1 : Create New Website.

Go to File > New > Website> Select empty web site > Enter Application Name > Click OK. shown in screenshot.





if your project does not contain App_Data folder than add as shown in screenshot.





Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.


Add columns in a table.


Populate table for fetching data follow as shown in screenshot.


add data in each row to the table.


Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish. 






Step-5: Add a Webpage and Design for Show Data in Gridview & Export to PDF


Go to Solution Explorer > Right Click on Website name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.




 <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="false" CellPadding="5">
                            <asp:BoundField DataField="Id" HeaderText="ID" />
                            <asp:BoundField DataField="Name" HeaderText="Name" />
                            <asp:BoundField DataField="Designation" HeaderText="Designation" />                          
                        <asp:Button ID="btnExport" runat="server" Text="Export to PDF" OnClick="btnExport_Click" />


Step-6: Write following code in Page_Load event for Show data in Gridview.

 protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    private void PopualteData()
        using (CompanyEntities dc = new CompanyEntities())
            var getlistRecord =dc.Employees.OrderBy(a => a.Name).ThenBy(a => a.Designation).ToList();
            gvData.DataSource = getlistRecord;


Step-7: Add Reference (itextsharp.dll)

Goto website click add reference as shown in screenshot.



Popup window will be open and select browse and find itextsharp.dll and select itextsharp.dll and click OK. the dll is present in downloaded file which i provide please download at the top of article.



Step-8: Write below code in button click (btnExportPDF_Click) event for export Database data to PDF File.

create folder inside website give name PDFFiles where pdf files will save.


 protected void btnExport_Click(object sender, EventArgs e)
        // This code is for export Database data to PDF file
        string fileName = Guid.NewGuid() + ".pdf";
        string filePath = Path.Combine(Server.MapPath("~/PDFFiles"), fileName);

        Document doc = new Document(PageSize.A4, 2, 2, 2, 2);

        // Create paragraph for show in PDF file header
        Paragraph p = new Paragraph("Export Database data to PDF file in ASP.NET");
      //  p.Alignment("center");

            PdfWriter.GetInstance(doc, new FileStream(filePath, FileMode.Create));
            //Create table here for write database data
            PdfPTable pdfTab = new PdfPTable(4); // here 4 is no of column
            pdfTab.HorizontalAlignment = 1; // 0- Left, 1- Center, 2- right
            pdfTab.SpacingBefore = 20f;
            pdfTab.SpacingAfter = 20f;

            List<Employee> data = new List<Employee>();
            using (CompanyEntities dc = new CompanyEntities())
                data = dc.Employees.OrderBy(a => a.Name).ThenBy(a => a.Designation).ToList();

            foreach (var emp in data)

            byte[] content = File.ReadAllBytes(filePath);
            HttpContext context = HttpContext.Current;

            context.Response.ContentType = "application/pdf";
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        catch (Exception)



Now finally Run the Application thats complete. You can download complete Source code at the top of article.


Related Article

Add your valuable Comments

Success! Comment added successfully.

Name is required

Email is a required field Invalid Email
Message is required