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 filtered ASP.Net GridView record to PDF using iTextSharp in C# and VB.Net<p>Hi <a class="username" rel="Tevin">Tevin</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlCountry&#34; OnSelectedIndexChanged=&#34;drplstDepartment_SelectedIndexChanged&#34; AutoPostBack=&#34;true&#34; AppendDataBoundItems=&#34;true&#34;&gt; &lt;asp:ListItem Text=&#34;Select&#34; Value=&#34;&#34;&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:GridView runat=&#34;server&#34; ID=&#34;gvCustomers&#34; AutoGenerateColumns=&#34;false&#34; AllowPaging=&#34;true&#34; PageSize=&#34;5&#34; OnPageIndexChanging=&#34;gvCustomers_PageIndexChanging&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;CompanyName&#34; HeaderText=&#34;CompanyName&#34; /&gt; &lt;asp:BoundField DataField=&#34;City&#34; HeaderText=&#34;City&#34; /&gt; &lt;asp:BoundField DataField=&#34;Country&#34; HeaderText=&#34;Country&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Export&#34; runat=&#34;server&#34; OnClick=&#34;btnPDF_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.Data; using System.Data.SqlClient; using System.Configuration; using System.IO; using iTextSharp.text.pdf; using iTextSharp.text; using iTextSharp.text.html.simpleparser;</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.SqlClient 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) { LoadDataGrid(&#34;&#34;); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString)) { using (SqlCommand cmd = new SqlCommand(&#34;SELECT DISTINCT Country FROM Customers&#34;, conn)) { SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); ddlCountry.DataSource = dt; ddlCountry.DataTextField = &#34;Country&#34;; ddlCountry.DataValueField = &#34;Country&#34;; ddlCountry.DataBind(); } } } } private void LoadDataGrid(string country) { DataTable dt = new DataTable(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ToString())) { SqlCommand cmd = new SqlCommand(&#34;SELECT CompanyName,City,Country FROM Customers WHERE Country = @Country OR @Country IS NULL&#34;, conn); cmd.Parameters.AddWithValue(&#34;@Country&#34;, !string.IsNullOrEmpty(country) ? country : (object)DBNull.Value); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); gvCustomers.DataSource = dt; gvCustomers.DataBind(); } } protected void drplstDepartment_SelectedIndexChanged(object sender, EventArgs e) { LoadDataGrid(ddlCountry.SelectedItem.Value); } protected void btnPDF_Click(object sender, EventArgs e) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { // To Export all pages gvCustomers.AllowPaging = false; // Rebind the filtered record to GridView. LoadDataGrid(ddlCountry.SelectedItem.Value); gvCustomers.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); } } } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvCustomers.PageIndex = e.NewPageIndex; LoadDataGrid(ddlCountry.SelectedItem.Value); }</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 LoadDataGrid(&#34;&#34;) Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString) Using cmd As SqlCommand = New SqlCommand(&#34;SELECT DISTINCT Country FROM Customers&#34;, conn) Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() sda.Fill(dt) ddlCountry.DataSource = dt ddlCountry.DataTextField = &#34;Country&#34; ddlCountry.DataValueField = &#34;Country&#34; ddlCountry.DataBind() End Using End Using End If End Sub Private Sub LoadDataGrid(ByVal country As String) Dim dt As DataTable = New DataTable() Using conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ToString()) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT CompanyName,City,Country FROM Customers WHERE Country = @Country OR @Country IS NULL&#34;, conn) cmd.Parameters.AddWithValue(&#34;@Country&#34;, If(Not String.IsNullOrEmpty(country), country, CObj(DBNull.Value))) Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) da.Fill(dt) gvCustomers.DataSource = dt gvCustomers.DataBind() End Using End Sub Protected Sub drplstDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) LoadDataGrid(ddlCountry.SelectedItem.Value) End Sub Protected Sub btnPDF_Click(ByVal sender As Object, ByVal e As EventArgs) Using sw As StringWriter = New StringWriter() Using hw As HtmlTextWriter = New HtmlTextWriter(sw) gvCustomers.AllowPaging = False LoadDataGrid(ddlCountry.SelectedItem.Value) gvCustomers.RenderControl(hw) Dim sr As StringReader = New StringReader(sw.ToString()) Dim pdfDoc As Document = New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() Response.ContentType = &#34;application/pdf&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=GridViewExport.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Write(pdfDoc) Response.[End]() End Using End Using End Sub Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control) End Sub Protected Sub gvCustomers_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) gvCustomers.PageIndex = e.NewPageIndex LoadDataGrid(ddlCountry.SelectedItem.Value) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/NqAieyp.gif" alt="" width="423" height="402" /></p>https://www.aspforums.net:443/Threads/190690/Export-filtered-ASPNet-GridView-record-to-PDF-using-iTextSharp-in-C-and-VBNet/https://www.aspforums.net:443/Threads/190690/Export-filtered-ASPNet-GridView-record-to-PDF-using-iTextSharp-in-C-and-VBNet/Thu, 27 Jun 2019 02:56:17 GMT