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 HTML to PDF and save file to Folder (Directory) using C# in ASP.Net<p>Hi <a class="username" rel="KatieNgoc"> KatieNgoc</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;asp:Panel ID=&#34;pnlDetails&#34; runat=&#34;server&#34;&gt; &lt;asp:Image ID=&#34;imgPhoto&#34; ImageUrl=&#34;~/E-Sign.png&#34; runat=&#34;server&#34; /&gt;&lt;br /&gt; &lt;p&gt; 1. Get A Discount On A New Walk In Tub&lt;/p&gt; &lt;br /&gt; &lt;p&gt; Walk in tubs are a perfect addition for seniors living with mobility challenges or anyone worried about fall related injuries. Statistics tell us that falls happen in your bathroom more than any other room in the home.&lt;/p&gt; &lt;br /&gt; &lt;p&gt; In fact two-thirds of all bathroom injuries happen near the shower or bath-tub, according to The Center for Disease Control. Most falls typically happen when your stepping over the tub, slipping on a wet floor or when leaning on a glass door that wasn&#39;t designed to support your weight&lt;/p&gt; &lt;br /&gt; &lt;p&gt; A Walk-in tub is an investment in your future a simple modification to your bathroom can change your life. Keeping your independence and dignity, plus peace of mind for your loved ones all ad up to a better quality of life. Right now, Walk-in tub manufacturers all over the country are offering big senior discounts. Visit the walk-in tub website to find discounts and get a free quote on a new Walk-in tub.&lt;/p&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>&nbsp;<strong>Default2.aspx</strong></p> <pre class="brush: html">&lt;asp:GridView ID=&#34;GridView1&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Id&#34; HeaderText=&#34;Id&#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:Button Text=&#34;Export&#34; runat=&#34;server&#34; OnClick=&#34;Export&#34; /&gt;</pre> <p>&nbsp;</p> <p>&nbsp;</p> <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.IO; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf;</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void btnExport_Click(object sender, EventArgs e) { imgPhoto.ImageUrl = GetUrl(&#34;E-Sign.png&#34;); if (!Directory.Exists(Server.MapPath(&#34;~/Files/&#34;))) { Directory.CreateDirectory(Server.MapPath(&#34;~/Files/&#34;)); } FileStream file = new FileStream(Server.MapPath(&#34;~/Files/&#34;) + &#34;&#34; + pnlDetails.ID + &#34;.PDF&#34;, FileMode.Create, System.IO.FileAccess.Write); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); pnlDetails.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); MemoryStream ms = new MemoryStream(); PdfWriter.GetInstance(pdfDoc, file); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); file.Close(); Response.Redirect(&#34;Default2.aspx&#34;); } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } protected string GetUrl(string imagepath) { string[] splits = Request.Url.AbsoluteUri.Split(&#39;/&#39;); if (splits.Length &gt;= 2) { string url = splits[0] + &#34;//&#34;; for (int i = 2; i &lt; splits.Length - 1; i++) { url += splits[i]; url += &#34;/&#34;; } return url + imagepath; } return imagepath; }</pre> <p><strong>Defauult2.aspx.cs</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[3] { new DataColumn(&#34;Id&#34;, typeof(int)), new DataColumn(&#34;Name&#34;, typeof(string)), new DataColumn(&#34;Country&#34;,typeof(string)) }); dt.Rows.Add(1, &#34;John Hammond&#34;, &#34;United States&#34;); dt.Rows.Add(2, &#34;Mudassar Khan&#34;, &#34;India&#34;); dt.Rows.Add(3, &#34;Suzanne Mathews&#34;, &#34;France&#34;); dt.Rows.Add(4, &#34;Robert Schidner&#34;, &#34;Russia&#34;); GridView1.DataSource = dt; GridView1.DataBind(); } } protected void Export(object sender, EventArgs e) { if (!Directory.Exists(Server.MapPath(&#34;~/Files/&#34;))) { Directory.CreateDirectory(Server.MapPath(&#34;~/Files/&#34;)); } FileStream file = new FileStream(Server.MapPath(&#34;~/Files/&#34;) + &#34;&#34; + GridView1.ID + &#34;.PDF&#34;, FileMode.Create, System.IO.FileAccess.Write); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); MemoryStream ms = new MemoryStream(); PdfWriter.GetInstance(pdfDoc, file); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); file.Close(); MergingFile(); } public override void VerifyRenderingInServerForm(Control control) { /* Verifies that the control is rendered */ } private void MergingFile() { List&lt;PdfReader&gt; pdfReaderList = new List&lt;PdfReader&gt;(); foreach (string filePath in Directory.GetFiles(Server.MapPath(&#34;~/Files&#34;))) { PdfReader pdfReader = new PdfReader(filePath); pdfReaderList.Add(pdfReader); } Response.ClearContent(); Response.ClearHeaders(); Response.Buffer = true; Document document = new Document(PageSize.A4, 0, 0, 0, 0); PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(Server.MapPath(&#34;~/Folder/&#34;) + &#34;OutPut.pdf&#34;, FileMode.Create)); document.Open(); foreach (PdfReader reader in pdfReaderList) { for (int i = 1; i &lt;= reader.NumberOfPages; i++) { PdfImportedPage page = writer.GetImportedPage(reader, i); document.Add(iTextSharp.text.Image.GetInstance(page)); } } document.Close(); Byte[] FileBuffer = File.ReadAllBytes(Server.MapPath(&#34;~/Folder/&#34;) + &#34;OutPut.pdf&#34;); if (FileBuffer != null) { Response.ContentType = &#34;application/pdf&#34;; Response.AddHeader(&#34;content-length&#34;, FileBuffer.Length.ToString()); Response.BinaryWrite(FileBuffer); } }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/567596/Export-HTML-to-PDF-and-save-file-to-Folder-Directory-using-C-in-ASPNet/https://www.aspforums.net:443/Threads/567596/Export-HTML-to-PDF-and-save-file-to-Folder-Directory-using-C-in-ASPNet/Fri, 06 Sep 2019 02:53:04 GMT