Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Data;
- using iTextSharp.text;
- using iTextSharp.text.html.simpleparser;
- using iTextSharp.text.pdf;
- using System.Text;
- namespace HTMLtoPDF
- {
- public partial class HTMLtoPDF : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void btnClick_Click(object sender, EventArgs e)
- {
- DownloadAsPDF();
- }
- public void DownloadAsPDF()
- {
- try
- {
- string strHtml = string.Empty;
- string pdfFileName = Request.PhysicalApplicationPath + "\files\" + "CASEID2.pdf";
- //string testPath = Server.MapPath("~/files/test.pdf");
- string template = File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));
- string case_id = "12345";
- string student_id = "";
- string student_name_input = "";
- string campus_input = "Falls Church";
- string email_input = "xyz@gmail.com";
- string phone_input = "";
- string term = "2";
- string address_input = "";
- string course_input = "Mobile Application Development";
- string reason_appeal = "Family Problems";
- string faculty_name = "XYZ";
- string remaining_work = "Task-1,Task-2,Task-3";
- string deadline_date = "May 1st";
- template = template.Replace("[CASEID]", case_id);
- template = template.Replace("[STUDENTID]", student_id);
- template = template.Replace("[STUDENTNAME]", student_name_input);
- template = template.Replace("[CAMPUS]", campus_input);
- template = template.Replace("[EMAIL]", email_input);
- template = template.Replace("[PHONE]", phone_input);
- template = template.Replace("[TERM]", term);
- template = template.Replace("[ADDRESS]", address_input);
- template = template.Replace("[COURSEID] [COURSENAME]", course_input);
- template = template.Replace("[REASON]", reason_appeal);
- template = template.Replace("[FACULTYNAME]", faculty_name);
- template = template.Replace("[REMAININGCOURSEWORK]", remaining_work);
- template = template.Replace("[DEADLINE]",deadline_date);
- //template.Replace("[DEADLINE]", Request[deadline_date]);
- //StringWriter sw = new StringWriter();
- //HtmlTextWriter hw = new HtmlTextWriter(sw);
- //dvHtml.RenderControl(hw);
- //StringReader sr = new StringReader(sw.ToString());
- //strHtml = sr.ReadToEnd();
- //sr.Close();
- //string temp2 = template.Replace("<!DOCTYPE html>rn<html>rn<head>rn <title></title>rnt<meta charset="utf-8" />rn</head>rn<body>rn", "");
- CreatePDFFromHTMLFile(template, pdfFileName);
- Response.ContentType = "application/x-download";
- Response.AddHeader("Content-Disposition", string.Format("attachment; filename="{0}"", "CASEID2.pdf"));
- Response.AddHeader("Refresh", "2;URL=SendEmail.aspx");
- Response.WriteFile(pdfFileName);
- Response.Flush();
- Response.End();
- }
- catch (Exception ex)
- {
- Response.Write(ex.Message);
- }
- }
- public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
- {
- try
- {
- object TargetFile = FileName;
- string ModifiedFileName = string.Empty;
- string FinalFileName = string.Empty;
- GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
- GeneratePDF.HtmlPdfPage first = builder.AddPage();
- first.AppendHtml(HtmlStream);
- byte[] file = builder.RenderPdf();
- File.WriteAllBytes(TargetFile.ToString(), file);
- iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
- ModifiedFileName = TargetFile.ToString();
- ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");
- iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
- reader.Close();
- if (File.Exists(TargetFile.ToString()))
- File.Delete(TargetFile.ToString());
- FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
- File.Copy(ModifiedFileName, FinalFileName);
- if (File.Exists(ModifiedFileName))
- File.Delete(ModifiedFileName);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement