Export ASP.Net GridView with Company logo to PDF using C# and VB.Net in ItextSharp

Last Reply 6 days ago By pandeyism

Posted 6 days ago

I have a Gridview control populated from code behind and when I do the PDF export on button click I would like to place the company logo above the gridview control.

This is my current code to export the gridview to PDF:

        Try
            Using sw As New StringWriter()
                Using hw As New HtmlTextWriter(sw)
                    'To Export all pages
                    GridView1.AllowPaging = False
                    Me.LoadDataGrid()
                    GridView1.RenderControl(hw)
                    Dim sr As New StringReader(sw.ToString())
                    Dim pdfDoc As New Document(PageSize.A2, 10.0F, 10.0F, 10.0F, 0.0F)
                    Dim htmlparser As New HTMLWorker(pdfDoc)
                    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
                    pdfDoc.Open()
                    htmlparser.Parse(sr)
                    pdfDoc.Close()

                    Response.ContentType = "application/pdf"
                    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf")
                    Response.Cache.SetCacheability(HttpCacheability.NoCache)
                    Response.Write(pdfDoc)
                    Response.[End]()
                End Using
            End Using
        Catch ex As Exception
        End Try

 

Posted 6 days ago

Hi Tevin,

Take a Panel and inside Panel place your GridView and Image and Set visible false to Image on page and during exporting set visible true when panel is render then again set visible false to image.

Refer below sample.

HTML

<asp:Panel runat="server" ID="pnlPrint">
    <img src="http://www.aspsnippets.com/images/Blue/Logo.png" runat="server" visible="false"
        id="imgLogo" /><br />
    <asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="Country" HeaderText="Country" />
        </Columns>
    </asp:GridView>
</asp:Panel>
<asp:Button ID="btnExport" runat="server" Text="Export" OnClick="btnExport_Click" />

Namespaces

C#

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

VB.Net

Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("CustomerId", typeof(int)), new DataColumn("Name", typeof(string)), new DataColumn("Country", typeof(string)) });
        dt.Rows.Add(1, "Mudassar", "India");
        dt.Rows.Add(2, "John", "USA");
        dt.Rows.Add(3, "Robert", "Indonesia");
        gvCustomers.DataSource = dt;
        gvCustomers.DataBind();
    }
}

protected void btnExport_Click(object sender, EventArgs e)
{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    StringWriter sw = new StringWriter();
    HtmlTextWriter hw = new HtmlTextWriter(sw);
    imgLogo.Visible = true;
    pnlPrint.RenderControl(hw);
    imgLogo.Visible = false;
    StringReader sr = new StringReader(sw.ToString());
    Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 100f, 0f);
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    Response.Write(pdfDoc);
    Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{
    /* Verifies that the control is rendered */
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("CustomerId", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
        dt.Rows.Add(1, "Mudassar", "India")
        dt.Rows.Add(2, "John", "USA")
        dt.Rows.Add(3, "Robert", "Indonesia")
        gvCustomers.DataSource = dt
        gvCustomers.DataBind()
    End If
End Sub

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs)
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf")
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Dim sw As StringWriter = New StringWriter()
    Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
    imgLogo.Visible = True
    pnlPrint.RenderControl(hw)
    imgLogo.Visible = False
    Dim sr As StringReader = New StringReader(sw.ToString())
    Dim pdfDoc As Document = New Document(PageSize.A2, 10.0F, 10.0F, 100.0F, 0.0F)
    Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc)
    PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
    pdfDoc.Open()
    htmlparser.Parse(sr)
    pdfDoc.Close()
    Response.Write(pdfDoc)
    Response.End()
End Sub

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
End Sub

Screenshot