How to Convert Gridview to Pdf


How to Convert Gridview to Pdf

This Application is used to convert Gridview to PDF document
using itext itextsharp(Its a free tool).
Download the itextsharp from link given below and add this dll in the bin directory.
sourceforge.net/projects/itextsharp/
or
nodevice.in/dll/itextsharp_dll/item21198.html
After placing the dll in the bin directory write the below code in the aspx.vb page.

In your code-behind, try just adding

Imports iTextSharp.text.pdf
Imports iTextSharp.text

Here with i had attached the sample project.



Imports iTextSharp.text.pdf
Imports iTextSharp.text
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim ds As New DataSet

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
' ShowGridview()
End If
End Sub

Sub ShowGridview()
'bind the database values to Gridveiw using dataset control
Dim con As SqlClient.SqlConnection
Dim da As SqlDataAdapter

Dim str As String
str = "USER=UserID;PASSWORD=PASSWORD;SERVER=SERVER-Name;DATABASE=DATABASE-Name"
con = New SqlConnection(str)
da = New SqlDataAdapter("select FieldName1,FieldName2 from Table_Name", con)
da.Fill(ds, "Employee")
ViewState("DataSetValue") = ds
grvGrid.DataSource = ds
grvGrid.DataBind()
con.Close()

End Sub
Private Sub ConvertGridviewToPdfDocument(ByVal dstHeader2 As DataSet)
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(Request.PhysicalApplicationPath + "\3.pdf", FileMode.Create))
doc.Open()
Dim table As New PdfPTable(dstHeader2.Tables(0).Columns.Count)
Dim widths As Integer() = {25, 25}
table.WidthPercentage = "100"
For i As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnHeader As New Paragraph(dstHeader2.Tables(0).Columns(i).ColumnName.ToString(), FontFactory.GetFont("verdana", 11))
ColumnHeader.Font.SetStyle(Font.BOLD)
Dim cell As New PdfPCell(New Phrase(ColumnHeader))
cell.HorizontalAlignment = Element.ALIGN_CENTER
table.AddCell(cell)
Next

For k As Integer = 0 To dstHeader2.Tables(0).Rows.Count - 1
For j As Integer = 0 To dstHeader2.Tables(0).Columns.Count - 1
Dim ColumnValue As New Paragraph(dstHeader2.Tables(0).Rows(k)(j).ToString(), FontFactory.GetFont("verdana", 10))
table.AddCell(ColumnValue)
Next
Next
table.SetWidths(widths)
doc.Add(table)
doc.Add(New Paragraph(vbLf))
doc.Close()
Response.Redirect("~/3.pdf")
End Sub

Protected Sub btnConvertGridviewToPDF_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvertGridviewToPDF.Click
'ConvertGridviewToPdfDocument(ViewState("DataSetValue"))
Dim str As String = ""
Dim doc As Document = New Document
PdfWriter.GetInstance(doc, New FileStream(FileUpload1.PostedFile.FileName, FileMode.Create))
'string strFileNameWithPath = FileUpload1.PostedFile.FileName;
doc.Open()
doc.Close()

End Sub
End Class


Attachments

  • Full Asp.net code (38879-9536-941-1054-CreatePDFDocument.rar)
  • Comments

    Author: Phagu Mahato02 Dec 2013 Member Level: Gold   Points : 10

    This code snippet used to download zip file


    protected void Page_Load(object sender, EventArgs e)
    {
    if(!IsPostBack)
    {
    SqlConnection cn = new SqlConnection(
    System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
    cn.Open();
    SqlCommand cmd = new SqlCommand("Select * from Show", cn);
    SqlDataReader dtr = cmd.ExecuteReader();

    if (dtr.HasRows)
    {
    dtr.Read();
    gv.DataSource = dtr;
    gv.DataBind();
    }
    cn.Close();
    }
    }


    Now , namespace of converting file

    using System.IO;
    using System.Data;
    using System.Net.Mail;
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html.simpleparser;

    Example of code Behind under mouse click event


    protected void btnword_Click(object sender, EventArgs e)
    {
    if(DropDownList1.SelectedIndex!=0)
    {
    if (DropDownList1.SelectedValue=="Excel")
    {
    string attachment = "attachment; filename=Export.xls";
    Response.ClearContent();
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);
    HtmlForm frm = new HtmlForm();
    gv.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(gv);
    frm.RenderControl(htw);
    Response.Write(sw.ToString());
    Response.End();
    }
    if(DropDownList1.SelectedValue=="Word")
    {
    Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.word";

    StringWriter stringWrite = new StringWriter();
    HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    HtmlForm frm = new HtmlForm();
    gv.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(gv);
    frm.RenderControl(htmlWrite);

    Response.Write(stringWrite.ToString());
    Response.End();
    }
    if(DropDownList1.SelectedValue=="PDF")
    {
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=Example.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    HtmlForm frm = new HtmlForm();
    gv.Parent.Controls.Add(frm);
    frm.Attributes["runat"] = "server";
    frm.Controls.Add(gv);
    frm.RenderControl(hw);
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
    }
    }
    }

    Author: Ashish Sayaji Nagare07 Dec 2013 Member Level: Bronze   Points : 0

    Good Program ,really useful for everyone and time saving.

    Author: Vetriselvan10 Dec 2013 Member Level: Silver   Points : 0

    http://www.aspdotnet-suresh.com/2013/09/csharp-export-gridview-data-to-excel-using-aspnet.html

    Author: srikanth05 Jan 2016 Member Level: Silver   Points : 0

    Export Gridview to Excel http://allittechnologies.blogspot.in/2015/05/gridview-data-export-to-excel.html



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: