Guest User

Untitled

a guest
Dec 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. public ActionResult GetDocFile(int LetterId, string FileName, string LetterField, string Content)
  2. {
  3. Content = Content.Replace(""", "");
  4. MemoryStream stream = new MemoryStream();
  5. OperationDetails op = new OperationDetails();
  6. if (!Directory.Exists(Server.MapPath("~/Uploads/LetterTemplate")))
  7. {
  8. Directory.CreateDirectory(Server.MapPath("~/Uploads/LetterTemplate"));
  9. }
  10. string filename = Server.MapPath("~/Uploads/LetterTemplate/" + FileName + ".docx");
  11. CommonHelper.CommonHelper.DeleteSingleFile(filename);
  12. string html = Content;
  13. try
  14. {
  15. using (WordprocessingDocument package = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
  16. {
  17. MainDocumentPart mainPart = package.MainDocumentPart;
  18. if (mainPart == null)
  19. {
  20. mainPart = package.AddMainDocumentPart();
  21. new Document(new Body()).Save(mainPart);
  22. }
  23.  
  24. HtmlConverter converter = new HtmlConverter(mainPart);
  25.  
  26. converter.ParseHtml(html);
  27.  
  28. var body = package.MainDocumentPart.Document.Body;
  29.  
  30. mainPart.Document.Save();
  31. }
  32.  
  33. System.IO.File.WriteAllBytes(filename, stream.ToArray());
  34. op = objReportingDB.UpdateLetterTemplate(LetterId, FileName, LetterField, LetterId == 0 ? 1 : 2, Content);
  35. if (op.Success)
  36. {
  37. op.Message = "Letter template saved successfully";
  38. }
  39. else
  40. {
  41. op.Message = "Error while saving letter template! ";
  42. }
  43.  
  44. }
  45. catch (Exception ex)
  46. {
  47. op.Success = false;
  48. op.Message = "Error while saving letter template! ";
  49. }
  50.  
  51. return Json(op, JsonRequestBehavior.AllowGet);
  52. }
Add Comment
Please, Sign In to add comment