ASP.NET Create PDF From GridView

In this example i'm explaining how to Generate Or Create PDF From GridView In Asp.Net Using ITextSharp and C# VB

Create PDF From GridView In Asp.Net
Create BIN Folder by right clicking in solution explorer and selecting Add Asp.Net Folder option and Put itextsharp.dll in it.

Place one GridView on aspx page and populate it from database using SQLDataSource

Place One Button on page to Create PDF in Click Event of it.

Add itextsharp namespace refrences in code behind of page



using iTextSharp.text;
using iTextSharp.text.pdf;


HTML SOURCE OF PAGE
   1:  <asp:GridView ID="GridView1" runat="server" 
   2:                AutoGenerateColumns="False" 
   3:                AllowPaging="true" PageSize="5" 
   4:                DataSourceID="SqlDataSource1">
   5:  <Columns>
   6:  <asp:BoundField DataField="Name" HeaderText="Name"/>
   7:  <asp:BoundField DataField="Location" 
   8:                  HeaderText="Location"/>
   9:  </Columns>
  10:  </asp:GridView>
  11:   
  12:  <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  13:  ConnectionString
  14:  ="<%$ ConnectionStrings:ConnectionString %>" 
  15:  SelectCommand="SELECT [Name], [Location] 
  16:                 FROM [Test]">
  17:  </asp:SqlDataSource>
  18:   
  19:  <asp:Button ID="btnPdf" runat="server" 
  20:              Text="Create PDF" 
  21:              onclick="btnPdf_Click" />

Place one button to create PDF from Gridview and write below mentioned code in Click Event of Button.

C# CODE
using iTextSharp.text;
using iTextSharp.text.pdf;
protected void btnPdf_Click(object sender, EventArgs e)
    {
        int columns = GridView1.Columns.Count;
        int rows = GridView1.Rows.Count;
        int tableRows = rows + 3;
        iTextSharp.text.Table gvTable = new iTextSharp.text.Table(columns, tableRows);
        gvTable.BorderWidth = 1;
        gvTable.BorderColor = new Color(0, 0, 255);
        gvTable.Cellpadding = 5;
        gvTable.Cellspacing = 5;
        Cell c1 = new Cell("Create PDF From GridView Example In Asp.Net");
        c1.Header = true;
        c1.Colspan = 2;
        gvTable.AddCell(c1);
        Cell c2 = new Cell("By www.CsharpAspNetArticles.com");
        c2.Colspan = 2;
        gvTable.AddCell(c2);
        gvTable.AddCell("Name");
        gvTable.AddCell("Location");

        for (int rowCounter = 0; rowCounter < rows; rowCounter++)
        {
            for (int columnCounter = 0; columnCounter < columns; columnCounter++)
            {
                string strValue = GridView1.Rows[rowCounter].Cells[columnCounter].Text;
                gvTable.AddCell(strValue);
            }
        }
        Document Doc = new Document();
        PdfWriter.GetInstance(Doc, Response.OutputStream);
        Doc.Open();
        Doc.Add(gvTable);
        Doc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=GridView.pdf");
        Response.End();
    }
VB.NET CODE
Protected Sub btnPdf_Click(sender As Object, e As EventArgs)
 Dim columns As Integer = GridView1.Columns.Count
 Dim rows As Integer = GridView1.Rows.Count
 Dim tableRows As Integer = rows + 3
 Dim gvTable As New iTextSharp.text.Table(columns, tableRows)
 gvTable.BorderWidth = 1
 gvTable.BorderColor = New Color(0, 0, 255)
 gvTable.Cellpadding = 5
 gvTable.Cellspacing = 5
 Dim c1 As New Cell("Create PDF From GridView Example In Asp.Net")
 c1.Header = True
 c1.Colspan = 2
 gvTable.AddCell(c1)
 Dim c2 As New Cell("By www.CsharpAspNetArticles.com")
 c2.Colspan = 2
 gvTable.AddCell(c2)
 gvTable.AddCell("Name")
 gvTable.AddCell("Location")

 For rowCounter As Integer = 0 To rows - 1
  For columnCounter As Integer = 0 To columns - 1
   Dim strValue As String = GridView1.Rows(rowCounter).Cells(columnCounter).Text
   gvTable.AddCell(strValue)
  Next
 Next
 Dim Doc As New Document()
 PdfWriter.GetInstance(Doc, Response.OutputStream)
 Doc.Open()
 Doc.Add(gvTable)
 Doc.Close()
 Response.ContentType = "application/pdf"
 Response.AddHeader("content-disposition", "attachment; filename=GridView.pdf")
 Response.[End]()
End Sub

Download Sample Code

If you like this post than join us or share

1 comments:

Unknown said...

This comment has been removed by a blog administrator.


Find More Articles