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 user details to PDF on ASP.Net GridView Print Button click using iTextSharp<p>Hi <a class="username" rel="sindiyen">sindiyen</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>If you want to more details about iTextSharp please refer below links</p> <p><a href="https://www.aspsnippets.com/Articles/Create-PDF-Report-from-database-in-ASPNet-using-C-and-VBNet.aspx" target="_blank">https://www.aspsnippets.com/Articles/Create-PDF-Report-from-database-in-ASPNet-using-C-and-VBNet.aspx</a></p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;gvContact&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; AllowPaging=&#34;false&#34; OnRowCommand=&#34;gvContact_RowCommand&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;EmployeeId&#34; HeaderText=&#34;EmployeeId&#34; /&gt; &lt;asp:BoundField DataField=&#34;Name&#34; HeaderText=&#34;Name&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Header1&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:Button ID=&#34;btnedit&#34; runat=&#34;server&#34; Text=&#34;View&#34; CommandName=&#34;View&#34; CommandArgument=&#34;&lt;%#((GridViewRow) Container).RowIndex %&gt;&#34; /&gt; &lt;asp:Button ID=&#34;Button1&#34; runat=&#34;server&#34; Text=&#34;Print&#34; CommandName=&#34;Print&#34; CommandArgument=&#34;&lt;%#((GridViewRow) Container).RowIndex %&gt;&#34; /&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&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 System.Data; using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser; using System.Configuration; using System.Data.SqlClient;</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 (!IsPostBack) { this.BindEmployeesDropDown(); } } private void BindEmployeesDropDown() { gvContact.DataSource = GetData(&#34;SELECT EmployeeId, (FirstName + &#39; &#39; + LastName) Name FROM Employees&#34;); gvContact.DataBind(); } private DataTable GetData(string query) { string conString = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataTable dt = new DataTable()) { sda.Fill(dt); return dt; } } } } protected void gvContact_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = gvContact.Rows[index]; if (e.CommandName == &#34;View&#34;) { Response.Redirect(&#34;~/Details.aspx?EmployeeID=&#34; + row.Cells[0].Text); } if (e.CommandName == &#34;Print&#34;) { DataRow dr = GetData(&#34;SELECT EmployeeId, (FirstName + &#39; &#39; + LastName) Name FROM Employees where EmployeeID = &#34; + row.Cells[0].Text).Rows[0]; Document document = new Document(PageSize.A3, 88f, 88f, 10f, 10f); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); PdfPTable table = null; document.Open(); table = new PdfPTable(3); table.AddCell(PhraseCell(new Phrase(&#34;Image&#34;))); table.AddCell(PhraseCell(new Phrase(&#34;Id&#34;))); table.AddCell(PhraseCell(new Phrase(&#34;Name&#34;))); table.AddCell(PhraseCell(new Phrase(&#34; &#34;))); table.AddCell(PhraseCell(new Phrase(&#34; &#34;))); table.AddCell(PhraseCell(new Phrase(&#34; &#34;))); table.AddCell(ImageCell(&#34;~/images/Chrysanthemum.jpg&#34;, 15f, PdfPCell.ALIGN_LEFT)); table.AddCell(PhraseCell(new Phrase(dr[&#34;EmployeeId&#34;].ToString()))); table.AddCell(PhraseCell(new Phrase(dr[&#34;Name&#34;].ToString()))); document.Add(table); document.Close(); byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); Response.Clear(); Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;Content-Disposition&#34;, &#34;attachment; filename=Employee.pdf&#34;); Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bytes); Response.End(); Response.Close(); } } } private static PdfPCell PhraseCell(Phrase phrase) { PdfPCell cell = new PdfPCell(phrase); cell.BorderColor = Color.WHITE; cell.VerticalAlignment = PdfCell.ALIGN_TOP; cell.PaddingBottom = 2f; cell.PaddingTop = 0f; return cell; } private static PdfPCell ImageCell(string path, float scale, int align) { iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath(path)); image.ScalePercent(scale); PdfPCell cell = new PdfPCell(image); cell.BorderColor = Color.WHITE; cell.VerticalAlignment = PdfCell.ALIGN_TOP; cell.HorizontalAlignment = align; cell.PaddingBottom = 0f; cell.PaddingTop = 0f; return cell; }</pre> <p><span style="text-decoration: underline;"><strong>Screenshots</strong></span></p> <p><img src="https://i.imgur.com/TNH2FmC.gif" alt="" width="270" height="89" /></p> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/530451/Export-user-details-to-PDF-on-ASPNet-GridView-Print-Button-click-using-iTextSharp/https://www.aspforums.net:443/Threads/530451/Export-user-details-to-PDF-on-ASPNet-GridView-Print-Button-click-using-iTextSharp/Thu, 17 May 2018 06:42:25 GMT