Hi KatieNgoc,
Refer below sample.
HTML
CS.aspx
<asp:DropDownList ID="FormList" runat="server">
</asp:DropDownList>
<asp:Button ID="Redirect" runat="server" Text="Load A Form" OnClick="Next_Click" />
Namespaces
C#
using System.Data;
using System.Text;
using System.IO;
using iTextSharp.text.pdf;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using Ionic.Zip;
Code
C#
CS.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["ListItems"] == null)
{
DataTable dt = DropDownItems();
FormList.DataSource = dt;
FormList.DataTextField = "Text";
FormList.DataValueField = "Value";
FormList.DataBind();
Session["ListItems"] = dt;
}
else
{
DataTable dt = (DataTable)Session["ListItems"];
FormList.DataSource = dt;
FormList.DataTextField = "Text";
FormList.DataValueField = "Value";
FormList.DataBind();
}
}
}
private DataTable DropDownItems()
{
DataTable dt = new DataTable();
dt.Columns.Add("Text");
dt.Columns.Add("Value");
dt.Rows.Add("Select one item, click on [Load a Form] button", "0");
dt.Rows.Add("1. W-4 IRS Form", "1");
dt.Rows.Add("2. Sexual Harassment Policy", "2");
dt.Rows.Add("3. Arbitration Agreement", "3");
dt.Rows.Add("4. Disciplinary & Attendance Policy", "4");
dt.Rows.Add("5. Paid Sick Leave", "5");
dt.Rows.Add("6. Authorization for Release of Information", "6");
dt.Rows.Add("7. General Code of Safe Practices", "7");
dt.Rows.Add("8. A Summary of Your Rights Under the Fair Credit Reporting Act", "8");
dt.Rows.Add("9. Meal Period Policy", "9");
dt.Rows.Add("10. Meal Break Waiver Agreement", "10");
dt.Rows.Add("11. Rest Period Policy", "11");
dt.Rows.Add("12. Employee Safety Orientation Sign-Off Sheet", "12");
dt.Rows.Add("13. Policy Statement Firearms, Weapons-Free Workplace", "13");
dt.Rows.Add("14. Assignment Abandonment", "14");
dt.Rows.Add("15. Remedying the Effects of Identity Theft", "15");
dt.Rows.Add("16. Drug and Alcohol Policy", "16");
dt.Rows.Add("17. General Safety Video", "17");
return dt;
}
protected void Next_Click(object sender, EventArgs e)
{
DataTable newDt = new DataTable();
newDt.Columns.Add("Text");
newDt.Columns.Add("Value");
DataTable dt = (DataTable)Session["ListItems"];
ListItemCollection liCol = FormList.Items;
for (int i = 0; i < dt.Rows.Count; i++)
{
ListItem li = liCol[i];
if (li.Selected)
{
if (FormList.SelectedItem.Value == "1")
{
GetNewDt(newDt, dt, i, "Default.aspx");
}
else if (FormList.SelectedItem.Value == "2")
{
GetNewDt(newDt, dt, i, "Default.aspx");
}
else if (FormList.SelectedItem.Value == "3")
{
GetNewDt(newDt, dt, i, "Default.aspx");
}
}
}
}
private void GetNewDt(DataTable newDt, DataTable dt, int i, string url)
{
dt.Rows[i].Delete();
newDt.Rows.Add(FormList.SelectedItem.Text, FormList.SelectedItem.Value);
Session["newdt"] = newDt;
Response.Redirect(url);
}
protected void Download(object sender, EventArgs e)
{
using (ZipFile zip = new ZipFile())
{
zip.AlternateEncodingUsage = ZipOption.AsNecessary;
zip.AddDirectoryByName("Files");
foreach (var filePath in Directory.GetFiles(Server.MapPath("~/File")))
{
zip.AddFile(filePath, "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();
}
}
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = Session["newdt"] as DataTable;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dt.Rows.Count; i++)
{
sb.Append("<p>");
sb.Append(dt.Rows[i]["Text"] + "" + dt.Rows[i]["Value"]);
sb.Append("</p>");
File.WriteAllText(Server.MapPath("~/Filehtml/") + dt.Rows[i]["Value"] + ".htm", sb.ToString());
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
using (StreamReader sr = new StreamReader(Server.MapPath("~/Filehtml/") + dt.Rows[i]["Value"] + ".htm"))
{
using (FileStream stream = new FileStream(Server.MapPath("~/File/") + "HTMLExport" + dt.Rows[i]["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();
}
}
}
}
}
}
}