コード例 #1
0
        public ActionResult Download(RegisterExternalLoginModel mReg, string Command, int id = 0)
        {
            if (Command == "MS Word")
            {
                try
                {
                    var userGuid = new Guid(User.Identity.GetUserId()); //WebSecurity.GetUserId(User.Identity.Name);
                    var userId   = BitConverter.ToInt32(userGuid.ToByteArray(), 0);

                    var responses  = _db.Responses.Where(x => x.UserId == userId).OrderBy(x => x.Ordinal).ThenBy(x => x.SubOrdinal).ThenBy(x => x.QQOrd).ToList();
                    var categories = new List <string> {
                        "Personal Information", "Employment", "Education", "Coursework", "Certifications", "Licenses", "Credentials", "Training"
                    };
                    var fui       = new FormatUserInformation(responses, categories);
                    var formatted = fui.Format();
                    var ms        = MakeWordFile.CreateDocument(formatted);
                    var ms2       = new MemoryStream(ms.ToArray());


                    Spire.Doc.Document doc = new Spire.Doc.Document(ms2);

                    doc.SaveToFile("Portfolio.docx", Spire.Doc.FileFormat.Docx, System.Web.HttpContext.Current.Response, HttpContentType.Attachment);

                    //Response.Clear();
                    //Response.AddHeader("content-disposition", "attachment; filename=\"Portfolio.docx\"");
                    //Response.ContentType = "application/msword";
                    //ms2.WriteTo(Response.OutputStream);
                    //Response.End();
                }
                catch (Exception ex)
                { Response.Write(ex.Message); }
            }
            else if (Command == "Pdf")
            {
                try
                {
                    var userGuid = new Guid(User.Identity.GetUserId()); //WebSecurity.GetUserId(User.Identity.Name);
                    var userId   = BitConverter.ToInt32(userGuid.ToByteArray(), 0);

                    var responses  = _db.Responses.Where(x => x.UserId == userId).OrderBy(x => x.Ordinal).ThenBy(x => x.SubOrdinal).ThenBy(x => x.QQOrd).ToList();
                    var categories = new List <string> {
                        "Personal Information", "Employment", "Education", "Coursework", "Certifications", "Licenses", "Credentials", "Training"
                    };
                    var fui       = new FormatUserInformation(responses, categories);
                    var formatted = fui.Format();
                    var ms        = MakeWordFile.CreateDocument(formatted);
                    var ms2       = new MemoryStream(ms.ToArray());

                    Spire.Doc.Document doc = new Spire.Doc.Document(ms2);

                    doc.SaveToFile("Portfolio.pdf", Spire.Doc.FileFormat.PDF, System.Web.HttpContext.Current.Response, HttpContentType.Attachment);
                }
                catch (Exception ex)
                { Response.Write(ex.Message); }
            }
            else if (Command == "Certificate")
            {
                var userGuid  = new Guid(User.Identity.GetUserId());
                var userIdStr = User.Identity.GetUserId();
                var userId    = BitConverter.ToInt32(userGuid.ToByteArray(), 0);
                var user      = _udb.Users.FirstOrDefault(s => s.Id == userIdStr);

                UserLevel userLevel        = _db.UserLevels.Where(q => q.UserId == userId).First();
                var       certificateDate  = ((DateTime)userLevel.FinalStepLevelDate).ToString("d");
                var       certificateLevel = userLevel.FinalStepLevel;
                var       signature        = "Zelda Boyd";

                string firstName     = "";
                string lastName      = "";
                string middleInitial = "";

                if (user != null)
                {
                    if (user.FirstName != null && user.FirstName != "")
                    {
                        firstName = user.FirstName;
                    }
                    else
                    {
                        firstName = "FirstName";
                    }

                    if (user.LastName != null && user.LastName != "")
                    {
                        lastName = user.LastName;
                    }
                    else
                    {
                        lastName = "LastName";
                    }

                    if (user.MiddleInitial != null && user.MiddleInitial != "")
                    {
                        middleInitial = user.MiddleInitial;
                    }
                    else
                    {
                        middleInitial = "";
                    }
                }

                firstName     = user.FirstName;
                middleInitial = user.MiddleInitial;
                lastName      = user.LastName;

                string fullName = firstName + " " + (middleInitial != "" ? middleInitial + " " : "") + lastName;

                var appRoot = Request.PhysicalApplicationPath;
                var file    = appRoot + "Content\\VPDR_Certificate_10.docx";
                var newFile = appRoot + "Content\\VPDR_Certificate_" + lastName + "_" + firstName + ".docx";
                var newPdf  = appRoot + "Content\\VPDR_Certificate_" + lastName + "_" + firstName + ".pdf";

                Spire.Doc.Document doc = new Spire.Doc.Document();
                doc.LoadFromFile(file);
                doc.Replace("PROVIDER", fullName, true, true);
                doc.Replace("LEVEL", certificateLevel, true, true);
                doc.Replace("DATE", certificateDate, true, true);
                doc.Replace("SIGNATURE", signature, true, true);
                //doc.SaveToFile(newPdf, Spire.Doc.FileFormat.PDF);
                //doc.SaveToFile(newFile, Spire.Doc.FileFormat.Docx);

                var newPdfName = "VPDR_Certificate_" + lastName + "_" + firstName + ".pdf";
                doc.SaveToFile(newPdfName, Spire.Doc.FileFormat.PDF, System.Web.HttpContext.Current.Response, HttpContentType.Attachment);
            }

            if (ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Download"));
        }
コード例 #2
0
        public ActionResult Download(RegisterExternalLoginModel mReg, string Command, int id = 0)
        {
            if (Command == "MS Word")
            {
                try
                {
                    var userId     = WebSecurity.GetUserId(User.Identity.Name);
                    var responses  = _db.Responses.Where(x => x.UserId == userId).OrderBy(x => x.Ordinal).ThenBy(x => x.SubOrdinal).ThenBy(x => x.QQOrd).ToList();
                    var categories = new List <string> {
                        "Personal Information", "Employment", "Education", "Coursework", "Certifications", "Licenses", "Credentials", "Training"
                    };
                    var fui       = new FormatUserInformation(responses, categories);
                    var formatted = fui.Format();
                    var ms        = MakeWordFile.CreateDocument(formatted);
                    var ms2       = new MemoryStream(ms.ToArray());

                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment; filename=\"Portfolio.docx\"");
                    Response.ContentType = "application/msword";
                    ms2.WriteTo(Response.OutputStream);
                    Response.End();
                }
                catch (Exception ex)
                { Response.Write(ex.Message); }
            }
            else if (Command == "Pdf")
            {
                try
                {
                    var userId     = WebSecurity.GetUserId(User.Identity.Name);
                    var responses  = _db.Responses.Where(x => x.UserId == userId).OrderBy(x => x.Ordinal).ThenBy(x => x.SubOrdinal).ThenBy(x => x.QQOrd).ToList();
                    var categories = new List <string> {
                        "Personal Information", "Employment", "Education", "Coursework", "Certifications", "Licenses", "Credentials", "Training"
                    };
                    var fui       = new FormatUserInformation(responses, categories);
                    var formatted = fui.Format();
                    var ms        = MakeWordFile.CreateDocument(formatted);
                    var ms2       = new MemoryStream(ms.ToArray());

                    Aspose.Words.Document doc = new Aspose.Words.Document(ms2);
                    var ms3 = new MemoryStream();
                    doc.Save(ms3, SaveFormat.Pdf);

                    Response.Clear();
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-disposition", "attachment; filename=\"Portfolio.pdf\"");

                    ms3.WriteTo(Response.OutputStream);
                    Response.End();
                }
                catch (Exception ex)
                { Response.Write(ex.Message); }
            }

            if (ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            return(View());
        }