Hi KatieNgoc,
Refer below sample code.
I have create below sample by taking reference of below link -
HTML
<asp:DropDownList runat="server" ID="ddlFiles" AutoPostBack="true" OnSelectedIndexChanged="ConverToPDF">
<asp:ListItem Text="----Files----" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="Button1" Text="Download " runat="server" OnClick="Download" />
Namespaces
C#
using System.IO;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using Ionic.Zip;
VB.Net
Imports System.IO
Imports iTextSharp.text
Imports iTextSharp.text.html.simpleparser
Imports iTextSharp.text.pdf
Imports Ionic.Zip
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
foreach (string filePath in Directory.GetFiles(Server.MapPath("~/HtmlFiles")))
{
i = i + 1;
ddlFiles.Items.Insert(i, new System.Web.UI.WebControls.ListItem(filePath, "" + i + ""));
}
}
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("~/File/") + "HTMLExport" + ddlFiles.SelectedItem.Value + ".pdf", 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("Files");
foreach (var filePath in Directory.GetFiles(Server.MapPath("~/HtmlFiles")))
{
i = i + 1;
zip.AddFile(Server.MapPath("~/File/") + "HTMLExport" + i + ".pdf", "Files");
}
Response.Clear();
Response.BufferOutput = false;
string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
zip.Save(Response.OutputStream);
Response.End();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim i As Integer = 0
For Each filePath As String In Directory.GetFiles(Server.MapPath("~/HtmlFiles"))
i = i + 1
ddlFiles.Items.Insert(i, New System.Web.UI.WebControls.ListItem(filePath, "" & i & ""))
Next
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("~/File/") & "HTMLExport" + ddlFiles.SelectedItem.Value & ".pdf", 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("Files")
For Each filePath In Directory.GetFiles(Server.MapPath("~/HtmlFiles"))
i = i + 1
zip.AddFile(Server.MapPath("~/File/") & "HTMLExport" + i & ".pdf", "Files")
Next
Response.Clear()
Response.BufferOutput = False
Dim zipName As String = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"))
Response.ContentType = "application/zip"
Response.AddHeader("content-disposition", "attachment; filename=" & zipName)
zip.Save(Response.OutputStream)
Response.End()
End Using
End Sub