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.Create PDF file with data from user input using iTextSharp in ASP.Net<p>Hi <a class="username" rel="Sumeet"> Sumeet</a>,</p> <p>Refer below sample.</p> <p>I have created below sample taking reference to below artilce link -</p> <h1 class="header"><a title="How to generate and download PDF Report from database in ASP.Net using iTextSharp C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/How-to-generate-and-download-PDF-Report-from-database-in-ASPNet-using-iTextSharp-C-and-VBNet.aspx">How to generate and download PDF Report from database in ASP.Net using iTextSharp C# and VB.Net</a></h1> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>CS.aspx</strong></p> <pre class="brush: html">Name : &lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt; &lt;asp:Button Text=&#34;NextPage&#34; runat=&#34;server&#34; OnClick=&#34;NextPage&#34; /&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">Id : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtId&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;NextPage&#34; runat=&#34;server&#34; OnClick=&#34;NextPage&#34; /&gt;</pre> <p><strong>Default2.aspx</strong></p> <pre class="brush: html">Address : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtAddress&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;NextPage&#34; runat=&#34;server&#34; OnClick=&#34;NextPage&#34; /&gt;</pre> <p><strong>Default3.aspx</strong></p> <pre class="brush: html">DOB : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtDob&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;ExportToPdf&#34; runat=&#34;server&#34; OnClick=&#34;Save&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <pre class="brush: csharp">using iTextSharp.text; using iTextSharp.text.pdf;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>CS.aspx.cs</strong></p> <pre class="brush: csharp">protected void NextPage(object sender, EventArgs e) { Session[&#34;Name&#34;] = txtName.Text; Response.Redirect(&#34;Default.aspx&#34;); }</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void NextPage(object sender, EventArgs e) { Session[&#34;Id&#34;] = txtId.Text; Response.Redirect(&#34;Default2.aspx&#34;); }</pre> <p><strong>Default2.aspx.cs</strong></p> <pre class="brush: csharp">protected void NextPage(object sender, EventArgs e) { Session[&#34;Address&#34;] = txtAddress.Text; Response.Redirect(&#34;Default3.aspx&#34;); }</pre> <p><strong>Default3.aspx.cs</strong> </p> <pre class="brush: csharp">protected void Save(object sender, EventArgs e) { string name = Session[&#34;Name&#34;].ToString(); string id = Session[&#34;Id&#34;].ToString(); string address = Session[&#34;Address&#34;].ToString(); string dob = txtDob.Text; Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f); Font NormalFont = FontFactory.GetFont(&#34;Arial&#34;, 12, Font.NORMAL, Color.BLACK); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { PdfWriter writer = PdfWriter.GetInstance(document, memoryStream); Phrase phrase = null; PdfPCell cell = null; PdfPTable table = null; Color color = null; document.Open(); //Header Table table = new PdfPTable(2); table.TotalWidth = 500f; table.LockedWidth = true; table.SetWidths(new float[] { 0.3f, 0.7f }); //Company Logo cell = ImageCell(&#34;~/images/ASPSnippetsLogo.png&#34;, 30f, PdfPCell.ALIGN_CENTER); table.AddCell(cell); phrase = new Phrase(); phrase.Add(new Chunk(&#34;Excelasoft Pvt Ltd\n\n&#34;, FontFactory.GetFont(&#34;Arial&#34;, 16, Font.BOLD, Color.RED))); phrase.Add(new Chunk(&#34;Goregaon,\n&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); phrase.Add(new Chunk(&#34;Mumbai,\n&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); phrase.Add(new Chunk(&#34;Maharashtra, India&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT); cell.VerticalAlignment = PdfCell.ALIGN_TOP; table.AddCell(cell); //Separater Line color = new Color(System.Drawing.ColorTranslator.FromHtml(&#34;#A9A9A9&#34;)); DrawLine(writer, 25f, document.Top - 79f, document.PageSize.Width - 25f, document.Top - 79f, color); DrawLine(writer, 25f, document.Top - 80f, document.PageSize.Width - 25f, document.Top - 80f, color); document.Add(table); table = new PdfPTable(2); table.HorizontalAlignment = Element.ALIGN_LEFT; table.SetWidths(new float[] { 0.3f, 1f }); table.SpacingBefore = 20f; //Employee Details cell = PhraseCell(new Phrase(&#34;Employee Record&#34;, FontFactory.GetFont(&#34;Arial&#34;, 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; table.AddCell(cell); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 30f; table.AddCell(cell); table = new PdfPTable(2); table.SetWidths(new float[] { 0.5f, 2f }); table.TotalWidth = 340f; table.LockedWidth = true; table.SpacingBefore = 20f; table.HorizontalAlignment = Element.ALIGN_RIGHT; table.AddCell(PhraseCell(new Phrase(&#34;Name:&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); table.AddCell(PhraseCell(new Phrase(name, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT)); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 10f; table.AddCell(cell); table.AddCell(PhraseCell(new Phrase(&#34;Designation:&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); table.AddCell(PhraseCell(new Phrase(&#34;Software Engineer&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT)); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 10f; table.AddCell(cell); //Employee Id table.AddCell(PhraseCell(new Phrase(&#34;Employee code:&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); table.AddCell(PhraseCell(new Phrase(&#34;000&#34; + id, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT)); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 10f; table.AddCell(cell); //Address table.AddCell(PhraseCell(new Phrase(&#34;Address:&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); phrase = new Phrase(new Chunk(address + &#34;\n&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); phrase.Add(new Chunk(&#34;Mumbai&#34; + &#34;\n&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); phrase.Add(new Chunk(&#34;Maharashtra&#34; + &#34; &#34; + &#34;India&#34; + &#34; &#34; + &#34;400097&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK))); table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT)); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 10f; table.AddCell(cell); //Date of Birth table.AddCell(PhraseCell(new Phrase(&#34;Date of Birth:&#34;, FontFactory.GetFont(&#34;Arial&#34;, 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT)); table.AddCell(PhraseCell(new Phrase(Convert.ToDateTime(dob).ToString(&#34;dd MMMM, yyyy&#34;), FontFactory.GetFont(&#34;Arial&#34;, 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT)); cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER); cell.Colspan = 2; cell.PaddingBottom = 10f; table.AddCell(cell); 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.ContentType = &#34;application/pdf&#34;; Response.Buffer = true; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.BinaryWrite(bytes); Response.End(); Response.Close(); } } private static void DrawLine(PdfWriter writer, float x1, float y1, float x2, float y2, Color color) { PdfContentByte contentByte = writer.DirectContent; contentByte.SetColorStroke(color); contentByte.MoveTo(x1, y1); contentByte.LineTo(x2, y2); contentByte.Stroke(); } private static PdfPCell PhraseCell(Phrase phrase, int align) { PdfPCell cell = new PdfPCell(phrase); cell.BorderColor = Color.WHITE; cell.VerticalAlignment = PdfCell.ALIGN_TOP; cell.HorizontalAlignment = align; 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>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/Dp9ffEh.jpg" alt="" width="399" height="360" /></p>https://www.aspforums.net:443/Threads/797125/Create-PDF-file-with-data-from-user-input-using-iTextSharp-in-ASPNet/https://www.aspforums.net:443/Threads/797125/Create-PDF-file-with-data-from-user-input-using-iTextSharp-in-ASPNet/Wed, 17 Jul 2019 05:11:38 GMT