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 files to PDF and save as Zip file using C# and VB.Net 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> <pre class="brush: html">&lt;asp:DropDownList runat=&#34;server&#34; ID=&#34;ddlFiles&#34; AutoPostBack=&#34;true&#34; OnSelectedIndexChanged=&#34;ConverToPDF&#34;&gt; &lt;asp:ListItem Text=&#34;----Files----&#34; Value=&#34;&#34;&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID=&#34;Button1&#34; Text=&#34;Download &#34; runat=&#34;server&#34; OnClick=&#34;Download&#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.IO; using iTextSharp.text; using iTextSharp.text.html.simpleparser; using iTextSharp.text.pdf; using Ionic.Zip;</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 Imports Ionic.Zip</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) { int i = 0; foreach (string filePath in Directory.GetFiles(Server.MapPath(&#34;~/HtmlFiles&#34;))) { i = i + 1; ddlFiles.Items.Insert(i, new System.Web.UI.WebControls.ListItem(filePath, &#34;&#34; + i + &#34;&#34;)); } } } protected void ConverToPDF(object sender, EventArgs e) { using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter hw = new HtmlTextWriter(sw)) { using (StreamReader sr = new StreamReader(ddlFiles.SelectedItem.Text)) { using (FileStream stream = new FileStream(Server.MapPath(&#34;~/File/&#34;) + &#34;HTMLExport&#34; + ddlFiles.SelectedItem.Value + &#34;.pdf&#34;, FileMode.Create)) { Document pdfDoc = new Document(PageSize.A2, 10f, 10f, 10f, 0f); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); stream.Close(); } } } } } protected void Download(object sender, EventArgs e) { int i = 0; using (ZipFile zip = new ZipFile()) { zip.AlternateEncodingUsage = ZipOption.AsNecessary; zip.AddDirectoryByName(&#34;Files&#34;); foreach (var filePath in Directory.GetFiles(Server.MapPath(&#34;~/File&#34;))) { i = i + 1; zip.AddFile(filePath, &#34;Files&#34;); } Response.Clear(); Response.BufferOutput = false; string zipName = String.Format(&#34;Zip_{0}.zip&#34;, DateTime.Now.ToString(&#34;yyyy-MMM-dd-HHmmss&#34;)); Response.ContentType = &#34;application/zip&#34;; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment; filename=&#34; + zipName); zip.Save(Response.OutputStream); Response.End(); } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim i As Integer = 0 For Each filePath As String In Directory.GetFiles(Server.MapPath(&#34;~/HtmlFiles&#34;)) i = i + 1 ddlFiles.Items.Insert(i, New System.Web.UI.WebControls.ListItem(filePath, &#34;&#34; &amp; i &amp; &#34;&#34;)) Next End If End Sub Protected Sub ConverToPDF(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(ddlFiles.SelectedItem.Text) Using stream As FileStream = New FileStream(Server.MapPath(&#34;~/File/&#34;) &amp; &#34;HTMLExport&#34; + ddlFiles.SelectedItem.Value &amp; &#34;.pdf&#34;, FileMode.Create) Dim pdfDoc As Document = New Document(PageSize.A2, 10F, 10F, 10F, 0F) Dim htmlparser As HTMLWorker = New HTMLWorker(pdfDoc) PdfWriter.GetInstance(pdfDoc, Response.OutputStream) PdfWriter.GetInstance(pdfDoc, stream) pdfDoc.Open() htmlparser.Parse(sr) pdfDoc.Close() stream.Close() End Using End Using End Using End Using End Sub Protected Sub Download(ByVal sender As Object, ByVal e As EventArgs) Dim i As Integer = 0 Using zip As ZipFile = New ZipFile() zip.AlternateEncodingUsage = ZipOption.AsNecessary zip.AddDirectoryByName(&#34;Files&#34;) For Each filePath In Directory.GetFiles(Server.MapPath(&#34;~/File&#34;)) i = i + 1 zip.AddFile(filePath, &#34;Files&#34;) Next Response.Clear() Response.BufferOutput = False Dim zipName As String = String.Format(&#34;Zip_{0}.zip&#34;, DateTime.Now.ToString(&#34;yyyy-MMM-dd-HHmmss&#34;)) Response.ContentType = &#34;application/zip&#34; Response.AddHeader(&#34;content-disposition&#34;, &#34;attachment; filename=&#34; &amp; zipName) zip.Save(Response.OutputStream) Response.End() End Using End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/5JoiQ95.gif" alt="" width="472" height="217" /></p>https://www.aspforums.net:443/Threads/514672/Convert-HTML-files-to-PDF-and-save-as-Zip-file-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/514672/Convert-HTML-files-to-PDF-and-save-as-Zip-file-using-C-and-VBNet-in-ASPNet/Fri, 09 Aug 2019 02:09:05 GMT