ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Export ASP.Net GridView with Company logo to PDF using C# and VB.Net in ItextSharp<p>Hi <a class="username" rel="Tevin"> Tevin</a>,</p> <p>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.</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:Panel runat=&#34;server&#34; ID=&#34;pnlPrint&#34;&gt; &lt;img src=&#34;http://www.aspsnippets.com/images/Blue/Logo.png&#34; runat=&#34;server&#34; visible=&#34;false&#34; id=&#34;imgLogo&#34; /&gt;&lt;br /&gt; &lt;asp:GridView ID=&#34;gvCustomers&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CustomerId&#34; HeaderText=&#34;CustomerId&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;/asp:Panel&gt; &lt;asp:Button ID=&#34;btnExport&#34; runat=&#34;server&#34; Text=&#34;Export&#34; OnClick=&#34;btnExport_Click&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using System.Data; </pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf Imports System.Data</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn(&#34;CustomerId&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;, typeof(string)) }); dt.Rows.Add(1, &#34;Mudassar&#34;, &#34;India&#34;); dt.Rows.Add(2, &#34;John&#34;, &#34;USA&#34;); dt.Rows.Add(3, &#34;Robert&#34;, &#34;Indonesia&#34;); gvCustomers.DataSource = dt; gvCustomers.DataBind(); } } protected void btnExport_Click(object sender, EventArgs e) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=TestPage.pdf&#34;); 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 */ }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;CustomerId&#34;, GetType(Integer)), New DataColumn(&#34;Name&#34;, GetType(String)), New DataColumn(&#34;Country&#34;, GetType(String))}) dt.Rows.Add(1, &#34;Mudassar&#34;, &#34;India&#34;) dt.Rows.Add(2, &#34;John&#34;, &#34;USA&#34;) dt.Rows.Add(3, &#34;Robert&#34;, &#34;Indonesia&#34;) gvCustomers.DataSource = dt gvCustomers.DataBind() End If End Sub Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As EventArgs) Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=TestPage.pdf&#34;) 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/Hn0dmRS.gif" alt="" width="443" height="320" /></p>https://www.aspforums.net:443/Threads/298368/Export-ASPNet-GridView-with-Company-logo-to-PDF-using-C-and-VBNet-in-ItextSharp/https://www.aspforums.net:443/Threads/298368/Export-ASPNet-GridView-with-Company-logo-to-PDF-using-C-and-VBNet-in-ItextSharp/Wed, 12 Jun 2019 00:48:21 GMT