In this article I will explain how to Export ASP.Net GridView to PDF Document using iTextSharp and then sending it as file attachment in email without saving the exporting PDF file on disk.
 
HTML Markup
The HTML Markup consists of an ASP.Net GridView with three columns and a Button to export GridView to PDF and send as email.
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="80" />
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<br />
<asp:Button ID="btnExport" runat="server" Text="Export To PDF and Send email" OnClick="ExportToPDF" />
 
 

Namespaces
You will need to import the following namespaces
C#
using System.IO;
using System.Data;
using System.Net.Mail;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Net.Mail
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
 
 
Binding the GridView
I am populating the GridView with some dummy records using DataTable.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
        dt.Rows.Add(1, "John Hammond", "United States");
        dt.Rows.Add(2, "Mudassar Khan", "India");
        dt.Rows.Add(3, "Suzanne Mathews", "France");
        dt.Rows.Add(4, "Robert Schidner", "Russia");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As New DataTable()
        dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
        dt.Rows.Add(1, "John Hammond", "United States")
        dt.Rows.Add(2, "Mudassar Khan", "India")
        dt.Rows.Add(3, "Suzanne Mathews", "France")
        dt.Rows.Add(4, "Robert Schidner", "Russia")
        GridView1.DataSource = dt
        GridView1.DataBind()
    End If
End Sub
 
Export GridView to PDF and send PDF File as email attachment in ASP.Net
 
Exporting GridView to PDF document and sending as File Attachment in Email
Below inside the Button click event handler, firstly the GridView is exported to PDF document using iTextSharp and the generated PDF document is saved to MemoryStream and the iTextSharp PDF document is closed.
The MemoryStream is converted to Array of bytes and then added as attachment to the MailMessage object.
Finally email is being sent via GMAIL account along with the exported GridView PDF document.
C#
protected void ExportToPDF(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
 
                MailMessage mm = new MailMessage("sender.com", "receiver@gmail.com");
                mm.Subject = "GridView Exported PDF";
                mm.Body = "GridView Exported PDF Attachment";
                mm.Attachments.Add(new Attachment(new MemoryStream(bytes), "GridViewPDF.pdf"));
                mm.IsBodyHtml = true;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                NetworkCred.UserName = "sender@gmail.com";
                NetworkCred.Password = "<passord>";
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }
    }
}
 
public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}
 
VB.Net
Protected Sub ExportToPDF(sender As Object, e As EventArgs)
    Using sw As New StringWriter()
        Using hw As New HtmlTextWriter(sw)
            GridView1.RenderControl(hw)
            Dim sr As New StringReader(sw.ToString())
            Dim pdfDoc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
            Dim htmlparser As New HTMLWorker(pdfDoc)
            Using memoryStream As New MemoryStream()
                PdfWriter.GetInstance(pdfDoc, memoryStream)
                pdfDoc.Open()
                htmlparser.Parse(sr)
                pdfDoc.Close()
                Dim bytes As Byte() = memoryStream.ToArray()
                memoryStream.Close()
 
                Dim mm As New MailMessage("sender@gmail.com", "receiver@gmail.com")
                mm.Subject = "GridView Exported PDF"
                mm.Body = "GridView Exported PDF Attachment"
                mm.Attachments.Add(New Attachment(New MemoryStream(bytes), "GridViewPDF.pdf"))
                mm.IsBodyHtml = True
                Dim smtp As New SmtpClient()
                smtp.Host = "smtp.gmail.com"
                smtp.EnableSsl = True
                Dim NetworkCred As New System.Net.NetworkCredential()
                NetworkCred.UserName = "sender@gmail.com"
                NetworkCred.Password = "<password>"
                smtp.UseDefaultCredentials = True
                smtp.Credentials = NetworkCred
                smtp.Port = 587
                smtp.Send(mm)
            End Using
        End Using
    End Using
End Sub
 
Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered
End Sub
 
Exported GridView PDF as attachment in Gmail Mailbox

Export GridView to PDF and send PDF File as email attachment in ASP.Net

Exported GridView PDF opened up in Google Document Viewer

Export GridView to PDF and send PDF File as email attachment in ASP.Net
 
Downloads