Beispiel #1
0
        public IActionResult Pdf()
        {
            string path         = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "template.docx");
            string templatePath = AppDomain.CurrentDomain.BaseDirectory + "temp.docx";

            System.IO.File.Copy(path, templatePath, true);

            var applicationDbContext = _context.Departments.Include(d => d.Faculty).ToList();
            var valuesToFill         = GetContent(applicationDbContext);

            using (var outputDocument = new TemplateProcessor(templatePath)
                                        .SetRemoveContentControls(true))
            {
                outputDocument.FillContent(valuesToFill);
                outputDocument.SaveChanges();
            }

            SautinSoft.Document.DocumentCore dc = SautinSoft.Document.DocumentCore.Load(templatePath);
            dc.Save(AppDomain.CurrentDomain.BaseDirectory + "DocToPDF.pdf");
            var bytes = System.IO.File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "DocToPDF.pdf");

            string file_name = "DepartmentInfo.pdf";

            System.IO.File.Delete(templatePath);
            return(File(bytes, "application/pdf", file_name));
        }
        /// <summary>
        /// <see cref="https://www.nuget.org/packages/sautinsoft.document/"/>
        /// </summary>
        /// <param name="templatePath"></param>
        /// <param name="outputDocumentPath"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static bool InsertTextInPlaceholders_sautinSoft(string templatePath, string outputDocumentPath, Object obj)
        {
            try
            {
                if (!File.Exists(templatePath))
                {
                    throw new FileNotFoundException();
                }

                SautinSoft.Document.DocumentCore dc = SautinSoft.Document.DocumentCore.Load(templatePath);
                dc.MailMerge.Execute(obj);
                dc.Save(outputDocumentPath);

                return(true);
            }
            catch (Exception x)
            {
                XLogger.Error(x);
                return(false);
            }
        }