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.Convert HTML File to PDF using C# and VB.Net in ASP.Net<p>Hi&nbsp;<a class="username" rel="salini">salini,</a></p> <p>Check this example. Now please take its reference and correct your code.</p> <p>You will need to download <span style="text-decoration: underline;">ITextSharp</span> and add its reference to your project. <span style="text-decoration: underline;">ITextSharp</span> is a free HTML to PDF Library. You can download it using the following download link.</p> <p><a href="http://sourceforge.net/projects/itextsharp/" target="_blank">Download ITextSharp</a></p> <p><strong><span style="text-decoration: underline;">Html File</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;table border=&#34;1&#34; cellpadding=&#34;1&#34; cellspacing=&#34;1&#34; style=&#34;width: 300px;&#34;&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Id&lt;/th&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Country&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;1&lt;/td&gt; &lt;td&gt;John Hammond&lt;/td&gt; &lt;td&gt;United States&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2&lt;/td&gt; &lt;td&gt;Mudassar Khan&lt;/td&gt; &lt;td&gt;India&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;3&lt;/td&gt; &lt;td&gt;Suzanne Mathews&lt;/td&gt; &lt;td&gt;France&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;4&lt;/td&gt; &lt;td&gt;Robert Schidner&lt;/td&gt; &lt;td&gt;Russia&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><strong><span style="text-decoration: underline;">HTML</span></strong></p> <pre class="brush: html">&lt;asp:Button Text=&#34;Convert&#34; runat=&#34;server&#34; OnClick=&#34;Download&#34; /&gt;</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">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.IO Imports iTextSharp.text Imports iTextSharp.text.html.simpleparser Imports iTextSharp.text.pdf</pre> <p><strong><span style="text-decoration: underline;">Code</span></strong></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Download(object sender, EventArgs e) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { using (StreamReader sr = new StreamReader(Server.MapPath(&#34;~/Customers.htm&#34;))) { Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 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=HTMLExport.pdf&#34;); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Write(pdfDoc); Response.End(); } } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs) Using sw As StringWriter = New StringWriter() Using hw As HtmlTextWriter = New HtmlTextWriter(sw) Using sr As StreamReader = New StreamReader(Server.MapPath(&#34;~/Customers.htm&#34;)) Dim pdfDoc As Document = New Document(PageSize.A2, 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=HTMLExport.pdf&#34;) Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.Write(pdfDoc) Response.End() End Using End Using End Using End Sub</pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/5OweUUP.jpg" alt="" width="472" height="118" /></span></strong></p>https://www.aspforums.net:443/Threads/172884/Convert-HTML-File-to-PDF-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/172884/Convert-HTML-File-to-PDF-using-C-and-VBNet-in-ASPNet/Tue, 12 Mar 2019 06:19:58 GMT