private Stream CreateLetter(NewLetterModel info) { try { var distributor = _distributorService.GetDistributorById(info.DistNumber); var sponsor = _distributorService.GetDistributorById(info.SponsorNumber); var platium = _distributorService.GetDistributorById(info.PlatiumNumber); var oldDistributor = _distributorService.GetDistributorById(info.OldDistNumber); var outputStream = new MemoryStream(); string filePath = Server.MapPath(Url.Content("~/Template/LetterTemplate.docx")); DocX document = DocX.Load(filePath); Stream mainStream = new MemoryStream(); document.SaveAs(mainStream); mainStream.Position = 0; mainStream = CreateLetterTemplate(mainStream, distributor, sponsor, platium, oldDistributor); //TODO: Add contant for file name Stream distributorLetter = CreateLetterAddress(mainStream, distributor); //zip.AddEntry("Re-apps letter-Nguoi dang ky lai.docx", distributorLetter); Stream sponsorLetter = CreateLetterAddress(mainStream, sponsor); //zip.AddEntry("Re-apps letter -Tuyen tren cu.docx", sponsorLetter); DocX docx = DocX.Load(distributorLetter); docx.InsertSectionPageBreak(); docx.InsertDocument(DocX.Load(sponsorLetter)); if (info.SponsorNumber != info.PlatiumNumber) { Stream platiumLetter = CreateLetterAddress(mainStream, platium); //zip.AddEntry("Re-apps letter -Platinum cu.docx", platiumLetter); docx.InsertSectionPageBreak(); docx.InsertDocument(DocX.Load(platiumLetter)); } docx.SaveAs(outputStream); _distributorService.AddNewLetter(new DistributorLetter { DistName = info.DistName, OldDistNumber = info.OldDistNumber, PlatiniumSponsorId = info.PlatiumNumber, SponsorId = info.SponsorNumber, DistNumber = info.DistNumber, LetterDate = DateTime.Now, UserId = _workContext.User.UserID, WHId = _workContext.User.WarehouseId }); outputStream.Position = 0; return(outputStream); } catch (Exception ex) { //TODO: Redirect to error Page return(null); } }