Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8.  
  9. using System.Data;
  10. using iTextSharp.text;
  11. using iTextSharp.text.html.simpleparser;
  12. using iTextSharp.text.pdf;
  13. using System.Text;
  14.  
  15. namespace HTMLtoPDF
  16. {
  17. public partial class HTMLtoPDF : System.Web.UI.Page
  18. {
  19.  
  20. protected void Page_Load(object sender, EventArgs e)
  21. {
  22.  
  23. }
  24. protected void btnClick_Click(object sender, EventArgs e)
  25. {
  26.  
  27. DownloadAsPDF();
  28. }
  29.  
  30. public void DownloadAsPDF()
  31. {
  32. try
  33. {
  34. string strHtml = string.Empty;
  35. string pdfFileName = Request.PhysicalApplicationPath + "\files\" + "CASEID2.pdf";
  36. //string testPath = Server.MapPath("~/files/test.pdf");
  37.  
  38. string template = File.ReadAllText(Server.MapPath("~/Incomplete-Pdf-temp.html"));
  39.  
  40.  
  41.  
  42. string case_id = "12345";
  43. string student_id = "";
  44. string student_name_input = "";
  45. string campus_input = "Falls Church";
  46. string email_input = "xyz@gmail.com";
  47. string phone_input = "";
  48. string term = "2";
  49. string address_input = "";
  50. string course_input = "Mobile Application Development";
  51. string reason_appeal = "Family Problems";
  52. string faculty_name = "XYZ";
  53. string remaining_work = "Task-1,Task-2,Task-3";
  54. string deadline_date = "May 1st";
  55.  
  56. template = template.Replace("[CASEID]", case_id);
  57. template = template.Replace("[STUDENTID]", student_id);
  58. template = template.Replace("[STUDENTNAME]", student_name_input);
  59. template = template.Replace("[CAMPUS]", campus_input);
  60. template = template.Replace("[EMAIL]", email_input);
  61. template = template.Replace("[PHONE]", phone_input);
  62. template = template.Replace("[TERM]", term);
  63. template = template.Replace("[ADDRESS]", address_input);
  64. template = template.Replace("[COURSEID] [COURSENAME]", course_input);
  65. template = template.Replace("[REASON]", reason_appeal);
  66. template = template.Replace("[FACULTYNAME]", faculty_name);
  67. template = template.Replace("[REMAININGCOURSEWORK]", remaining_work);
  68. template = template.Replace("[DEADLINE]",deadline_date);
  69.  
  70.  
  71.  
  72. //template.Replace("[DEADLINE]", Request[deadline_date]);
  73.  
  74.  
  75.  
  76. //StringWriter sw = new StringWriter();
  77. //HtmlTextWriter hw = new HtmlTextWriter(sw);
  78. //dvHtml.RenderControl(hw);
  79. //StringReader sr = new StringReader(sw.ToString());
  80.  
  81. //strHtml = sr.ReadToEnd();
  82. //sr.Close();
  83.  
  84. //string temp2 = template.Replace("<!DOCTYPE html>rn<html>rn<head>rn <title></title>rnt<meta charset="utf-8" />rn</head>rn<body>rn", "");
  85.  
  86.  
  87.  
  88. CreatePDFFromHTMLFile(template, pdfFileName);
  89.  
  90. Response.ContentType = "application/x-download";
  91. Response.AddHeader("Content-Disposition", string.Format("attachment; filename="{0}"", "CASEID2.pdf"));
  92. Response.AddHeader("Refresh", "2;URL=SendEmail.aspx");
  93. Response.WriteFile(pdfFileName);
  94. Response.Flush();
  95. Response.End();
  96.  
  97.  
  98.  
  99.  
  100. }
  101. catch (Exception ex)
  102. {
  103. Response.Write(ex.Message);
  104. }
  105. }
  106.  
  107.  
  108.  
  109. public void CreatePDFFromHTMLFile(string HtmlStream, string FileName)
  110. {
  111. try
  112. {
  113. object TargetFile = FileName;
  114.  
  115. string ModifiedFileName = string.Empty;
  116. string FinalFileName = string.Empty;
  117.  
  118.  
  119.  
  120.  
  121. GeneratePDF.HtmlToPdfBuilder builder = new GeneratePDF.HtmlToPdfBuilder(iTextSharp.text.PageSize.A4);
  122. GeneratePDF.HtmlPdfPage first = builder.AddPage();
  123. first.AppendHtml(HtmlStream);
  124. byte[] file = builder.RenderPdf();
  125. File.WriteAllBytes(TargetFile.ToString(), file);
  126.  
  127. iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(TargetFile.ToString());
  128. ModifiedFileName = TargetFile.ToString();
  129. ModifiedFileName = ModifiedFileName.Insert(ModifiedFileName.Length - 4, "1");
  130.  
  131. iTextSharp.text.pdf.PdfEncryptor.Encrypt(reader, new FileStream(ModifiedFileName, FileMode.Append), iTextSharp.text.pdf.PdfWriter.STRENGTH128BITS, "", "", iTextSharp.text.pdf.PdfWriter.AllowPrinting);
  132. reader.Close();
  133. if (File.Exists(TargetFile.ToString()))
  134. File.Delete(TargetFile.ToString());
  135. FinalFileName = ModifiedFileName.Remove(ModifiedFileName.Length - 5, 1);
  136. File.Copy(ModifiedFileName, FinalFileName);
  137. if (File.Exists(ModifiedFileName))
  138. File.Delete(ModifiedFileName);
  139.  
  140. }
  141. catch (Exception ex)
  142. {
  143. throw ex;
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement