コード例 #1
0
        public static void ImportaTemplate(string filePath)
        {
            FileNameParser parser = new FileNameParser(filePath);

            Template template = new Template
            {
                DTINS = DateTime.Now,
                DESCR = parser.DESCR,
                PATH  = parser.PATH,
            };

            TemplateDB.SaveTemplate(template);

            TemplateDB.RunScriptTemplate(template.KEY_TEMPL, template.DESCR);

            List <PdfAssociation> pdfAssociations = PdfAssociationDB.LoadPdfAssociation(template.KEY_TEMPL); // chiave template

            if (pdfAssociations == null)
            {
                throw new Exception("Associazioni field/pdf non valorizzate");
            }
        }
        private static string CreatePdf(string strFileNameInput, string strFileNameOutput, Users user, int intKey)
        {
            PdfReader   reader = new PdfReader(strFileNameInput);
            PdfDocument pdf    = new PdfDocument(reader, new PdfWriter(strFileNameOutput));

            try
            {
                PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, false);

                List <PdfAssociation> pdfAssociations = PdfAssociationDB.LoadPdfAssociation(intKey); // chiave template

                if (pdfAssociations == null)
                {
                    throw new Exception("Associazioni field/pdf non valorizzate");
                }

                if (pdfAssociations.Count == 1)
                {
                    MessageBox.Show("Template non correttamente configurato", "Errore Configurazione", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(null);
                }

                // ciclo la lista e per ciascun ciclo vado a leggere il campo TIPO CAMPO
                foreach (var itempdf in pdfAssociations)
                {
                    if (form.GetFormFields().ContainsKey(itempdf.PDF_FIELD)) // "txtMail, txtNameSurname ecc ecc"
                    {
                        switch (itempdf.TYPE)                                //"TXT","SIG" ecc ecc
                        {
                        case "TXT":
                            MergePdfTxt(form, itempdf, user);

                            break;

                        case "SIG":
                            AddSignature(form, itempdf);
                            break;
                        }
                    }
                }

                #region
                //IDictionary<string, PdfFormField> keyValuePairs = form.GetFormFields();
                //foreach (var item in keyValuePairs)
                //{

                //   item.Key --> Search modello + item.Key su pdf associati.
                //   se esiste
                //   in base al tipo campo fai switch
                //   se txt :
                //   se valore associato = "#CURRENTDATE#" fai insert data
                //   se diverso fai verificare che non contenga campo ","
                //   se non la contiene associ ute.campo corrispondente
                //   se contiene virgola fai split
                //switch (item.Key)
                //{
                //    case "TXT":
                //        MergePdfTxt(pdffieldName, Associazione rk)
                //        break;
                //    case "SIG":
                //        AddSignature(pdffieldName)
                //        break;
                //    case default:
                //        MessageBox.Show("Tipo Associazione non valido");
                //        break;
                //}

                //switch (item.Key)
                //{
                //    case "txtNameSurname":
                //        item.Value.SetValue(ute.COGNOME + " " + ute.NOME);
                //        break;
                //    case "txtDate":
                //        item.Value.SetValue(DateTime.Now.ToString("mm/dd/yyyy"));
                //        break;
                //    case "txtMail":
                //        item.Value.SetValue(ute.MAIL);
                //        break;
                //    case "txtCell":
                //        item.Value.SetValue(ute.CELL);
                //        break;
                //    case "txtSignature":
                //        PdfArray position = form.GetField("txtSignature").GetWidgets()[0].GetRectangle();
                //        rectangle = position.ToRectangle();
                //        removeSigForBiometric = true;
                //        break;
                //}
                //}
                #endregion

                form.FlattenFields();
                pdf.Close();

                reader.Close();

                return(strFileNameOutput);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Errore: " + ex.Message, "Configurazione errata", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(null);
            }
            finally
            {
                pdf.Close();
            }
        }