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 ListView inside Panel to PDF using iTextSharp, C# and VB.Net<p>Hey <a class="username" rel="nauna">nauna</a>,</p> <p>Please refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:Panel ID=&#34;pnlCustomers&#34; runat=&#34;server&#34;&gt; &lt;asp:ListView ID=&#34;lvCustomers&#34; runat=&#34;server&#34; GroupPlaceholderID=&#34;groupPlaceHolder1&#34; ItemPlaceholderID=&#34;itemPlaceHolder1&#34;&gt; &lt;LayoutTemplate&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; CustomerId &lt;/th&gt; &lt;th&gt; ContactName &lt;/th&gt; &lt;th&gt; Country &lt;/th&gt; &lt;/tr&gt; &lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;groupPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/table&gt; &lt;/LayoutTemplate&gt; &lt;GroupTemplate&gt; &lt;tr&gt; &lt;asp:PlaceHolder runat=&#34;server&#34; ID=&#34;itemPlaceHolder1&#34;&gt;&lt;/asp:PlaceHolder&gt; &lt;/tr&gt; &lt;/GroupTemplate&gt; &lt;ItemTemplate&gt; &lt;td&gt; &lt;%# Eval(&#34;CustomerId&#34;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;ContactName&#34;) %&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;Country&#34;) %&gt; &lt;/td&gt; &lt;/ItemTemplate&gt; &lt;/asp:ListView&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.Data; using System.Data.SqlClient; using System.Configuration; using System.IO; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data Imports System.Data.SqlClient Imports System.Configuration Imports System.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf</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) { this.BindListView(); } } private void BindListView() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT TOP 10 CustomerId, ContactName, Country FROM Customers&#34;; cmd.Connection = con; using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { DataTable dt = new DataTable(); sda.Fill(dt); lvCustomers.DataSource = dt; lvCustomers.DataBind(); } } } } protected void btnExport_Click(object sender, EventArgs e) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment;filename=Panel.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); pnlCustomers.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 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(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load If Not Me.IsPostBack Then Me.BindListView() End If End Sub Private Sub BindListView() Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Using con As New SqlConnection(constr) Using cmd As New SqlCommand() cmd.CommandText = &#34;SELECT TOP 10 CustomerId, ContactName, Country FROM Customers&#34; cmd.Connection = con Using sda As New SqlDataAdapter(cmd) Dim dt As New DataTable() sda.Fill(dt) lvCustomers.DataSource = dt lvCustomers.DataBind() End Using End Using End Using 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=Panel.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Dim sw As StringWriter = New StringWriter() Dim hw As HtmlTextWriter = New HtmlTextWriter(sw) pnlCustomers.RenderControl(hw) Dim sr As StringReader = New StringReader(sw.ToString()) Dim pdfDoc As Document = New Document(PageSize.A4, 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</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><strong>Listview</strong></p> <p><strong><img src="https://i.imgur.com/u6LKn46.jpg" alt="" width="301" height="326" /></strong></p> <p><strong>PDF</strong></p> <p><strong><img src="https://i.imgur.com/U7wVVaG.jpg" alt="" width="389" height="290" /></strong></p>https://www.aspforums.net:443/Threads/119774/Export-ASPNet-ListView-inside-Panel-to-PDF-using-iTextSharp-C-and-VBNet/https://www.aspforums.net:443/Threads/119774/Export-ASPNet-ListView-inside-Panel-to-PDF-using-iTextSharp-C-and-VBNet/Mon, 10 Dec 2018 23:33:34 GMT