Ejemplo n.º 1
0
        private static byte[] GeneratePDF(string pathToPDFTemplate, PdfSymbols pdf)
        {
            MemoryStream newFileStream;

            using (var existingFileStream = new FileStream(pathToPDFTemplate, FileMode.Open))
                using (newFileStream = new MemoryStream())
                    using (var pdfReader = new PdfReader(existingFileStream))
                        using (var stamper = new PdfStamper(pdfReader, newFileStream))
                        {
                            var form = stamper.AcroFields;
                            form.GenerateAppearances = true;

                            if (form.Fields.Keys.Contains(nameof(pdf.CertificateName)) && pdf.CertificateName != null)
                            {
                                form.SetField(nameof(pdf.CertificateName), pdf.CertificateName);
                            }

                            if (form.Fields.Keys.Contains(nameof(pdf.Date)) && pdf.Date != null)
                            {
                                form.SetField(nameof(pdf.Date), pdf.Date);
                            }

                            if (form.Fields.Keys.Contains(nameof(pdf.Details)) && pdf.Details != null)
                            {
                                form.SetField(nameof(pdf.Details), pdf.Details);
                            }

                            if (form.Fields.Keys.Contains(nameof(pdf.StudentName)) && pdf.StudentName != null)
                            {
                                form.SetField(nameof(pdf.StudentName), pdf.StudentName);
                            }

                            if (form.Fields.Keys.Contains(nameof(pdf.Author)) && pdf.Author != null)
                            {
                                form.SetField(nameof(pdf.Author), pdf.Author);
                            }

                            if (form.Fields.Keys.Contains(nameof(pdf.Company)) && pdf.Company != null)
                            {
                                form.SetField(nameof(pdf.Company), pdf.Company);
                            }

                            stamper.FormFlattening = true;
                        }
            return(newFileStream.ToArray());
        }
Ejemplo n.º 2
0
        // Example:
        // PdfUtils.GenerateCerfificate(env, "test.pdf", "cerBOficat2.pdf", new PdfSymbols { FirstName = "BO" });

        public static byte[] GenerateCerfificate(IHostingEnvironment env, string templateName, PdfSymbols pdfSymbols)
        {
            var pathWithoutWwwRoot = env.WebRootPath.Substring(0, env.WebRootPath.Length - ("\\wwwroot").Length);
            var templatePath       = pathWithoutWwwRoot + $@"\PDF\Templates\{templateName}";

            return(PdfUtils.GeneratePDF(templatePath, pdfSymbols));
        }