コード例 #1
0
        public static void ReplacePdfForm()
        {
            var fileNameExisting = @"C:\path\to\existing.pdf";
            var fileNameNew      = @"C:\path\to\new.pdf";

            using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
                using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
                {
                    var pdfReader = new PdfReader(existingFileStream);

                    // PdfStamper, which will create
                    var stamper = new PdfStamper(pdfReader, newFileStream);

                    var form      = stamper.AcroFields;
                    var fieldKeys = form.Fields.Keys;

                    foreach (var fieldKey in fieldKeys)
                    {
                        form.SetField(fieldKey, "REPLACED!");
                    }

                    stamper.FormFlattening = true;

                    // You can also specify fields to be flattened, which
                    // leaves the rest of the form still be editable/usable
                    stamper.PartialFormFlattening("field1");

                    stamper.Close();
                    pdfReader.Close();
                }
        }
コード例 #2
0
        public void ChangeText()
        {
            string existingPdfFile = Path.Combine(GetDllPath(), @"..\..\data\Spec.pdf");
            string newPdfFile      = Path.Combine(GetDllPath(), @"..\..\data\newSpec.pdf");

            Assert.That(File.Exists(existingPdfFile), Is.True);

            using (var newFileStream = new FileStream(newPdfFile, FileMode.Create))
                using (PdfReader pdfReader = new PdfReader(existingPdfFile))
                    using (var stamper = new PdfStamper(pdfReader, newFileStream)) // PdfStamper, which will create
                    {
                        var  form       = stamper.AcroFields;
                        bool xfaPresent = form.Xfa.XfaPresent;
                        var  fieldKeys  = form.Fields.Keys;
                        foreach (string fieldKey in fieldKeys)
                        {
                            form.SetField(fieldKey, "REPLACED!");
                        }
                        // "Flatten" the form so it wont be editable/usable anymore
                        stamper.FormFlattening = true;

                        // You can also specify fields to be flattened, which
                        // leaves the rest of the form still be editable/usable
                        stamper.PartialFormFlattening("field1");
                    }
        }
コード例 #3
0
        private void EscreverPdf(Invoice invoice, string path)
        {
            _invoiceMap   = new InvoiceMapper();
            _invoiceModel = new InvoiceModel();
            _invoiceModel.Create(invoice);

            string modeloPdf = AppDomain.CurrentDomain.BaseDirectory + "\\..\\..\\..\\Projeto_NFe.Infra.PDF\\ModelsPDF\\NFEM_PAG1.pdf";
            string newPdf    = path;

            using (var modeloPdfStream = new FileStream(modeloPdf, FileMode.Open))
                using (var newPdfStream = new FileStream(newPdf, FileMode.Create))
                {
                    var pdfReader = new PdfReader(modeloPdfStream);

                    var pdfStamp = new PdfStamper(pdfReader, newPdfStream);

                    _invoiceMap.Map(pdfStamp, _invoiceModel);

                    pdfStamp.FormFlattening = true;
                    pdfStamp.PartialFormFlattening("field1");

                    pdfStamp.Close();
                    pdfReader.Close();
                }
        }
コード例 #4
0
        private void UtilizarTemplatePdf()
        {
            const string fileNameExisting = @"C:\Users\mario.chan\Documents\GitHub\Benchmark\Library_benchmark\Content\Resource\DummyTemplate.pdf";
            var          fileNameNew      = @"C:\Users\mario.chan\Documents\GitHub\Benchmark\Library_benchmark\PdfUploads\" + "prueba.pdf";

            using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
                using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
                {
                    var pdfReader = new PdfReader(existingFileStream);
                    var stamper   = new PdfStamper(pdfReader, newFileStream);

                    var form      = stamper.AcroFields;
                    var fieldKeys = form.Fields.Keys;

                    foreach (var fieldKey in fieldKeys)
                    {
                        switch (fieldKey)
                        {
                        case "Fields.truck.FieldValue":
                            form.SetField(fieldKey, _informacion.Truck);
                            break;

                        case "body":

                            break;

                        case "Labels.pageNumberAndCount":
                            form.SetField(fieldKey, "2");
                            break;
                        }
                    }



                    stamper.FormFlattening = true;

                    // You can also specify fields to be flattened, which
                    // leaves the rest of the form still be editable/usable
                    stamper.PartialFormFlattening("field1");

                    stamper.Close();
                    pdfReader.Close();
                }
        }
コード例 #5
0
// ---------------------------------------------------------------------------
        public byte[] ManipulatePdf(byte[] src, Dictionary <string, TextField> cache,
                                    string name, string login)
        {
            using (MemoryStream ms = new MemoryStream()) {
                PdfReader reader = new PdfReader(src);
                using (PdfStamper stamper = new PdfStamper(reader, ms)) {
                    AcroFields form = stamper.AcroFields;
                    form.FieldCache = cache;
                    form.SetExtraMargin(2, 0);
                    form.RemoveField("personal.password");
                    form.SetField("personal.name", name);
                    form.SetField("personal.loginname", login);
                    form.RenameField("personal.reason", "personal.motivation");
                    form.SetFieldProperty(
                        "personal.loginname", "setfflags", TextField.READ_ONLY, null
                        );
                    stamper.FormFlattening = true;
                    stamper.PartialFormFlattening("personal.name");
                }
                return(ms.ToArray());
            }
        }
コード例 #6
0
 /// <summary>
 /// Initialize the PDF signature field.
 /// </summary>
 private void InitSignatureField(PdfStamper stamper)
 {
     if (_acroFieldsWorker.HasSignatureField(SignatureFieldName))
     {
         _signatureAppearance.SetVisibleSignature(SignatureFieldName);
     }
     else
     {
         if (!string.IsNullOrWhiteSpace(SignatureFieldName) &&
             _acroFieldsWorker.HasField(SignatureFieldName))
         {
             var textField = _acroFieldsWorker.FieldLocationByName(SignatureFieldName);
             _signatureAppearance.SetVisibleSignature(
                 textField.Rectangle, textField.Page,
                 _signatureAppearance.GetNewSigName()
                 );
             stamper.FormFlattening = true;
             stamper.PartialFormFlattening(SignatureFieldName);
         }
         else if (SignatureBox != null)
         {
             _signatureAppearance.SetVisibleSignature(
                 SignatureBox,
                 // reader.NumberOfPages,
                 1,
                 _signatureAppearance.GetNewSigName()
                 );
             stamper.FormFlattening = true;
             // stamper.PartialFormFlattening(SignatureFieldName);
         }
         else
         {
             throw new InvalidOperationException("field does not exist");
         }
     }
 }
コード例 #7
0
        public void ReplacePdfForm(string outputhpath)
        {
            try
            {
                String strAppPath      = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                String strFilePath     = Path.Combine(strAppPath, "Resources");
                String strFullFilename = Path.Combine(strFilePath, "SolicitudCN.pdf");

                //string fileNameExisting = System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/Solicitud.pdf";
                string fileNameExisting = strFullFilename;
                //string fileNameNew = @"C:\Users\Victor\Downloads\new" + filename + ".pdf";
                string fileNameNew = @"" + outputhpath + "";

                using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
                    using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
                    {
                        // Open existing PDF
                        var pdfReader = new PdfReader(existingFileStream);

                        // PdfStamper, which will create
                        var stamper = new PdfStamper(pdfReader, newFileStream);

                        var form      = stamper.AcroFields;
                        var fieldKeys = form.Fields.Keys;
                        int x         = 1;
                        foreach (string fieldKey in fieldKeys)
                        {
                            // form.SetField(fieldKey, x.ToString());
                            switch (x)
                            {
                            case (1):
                                form.SetField(fieldKey, INFOMADE.gradoActual(cb_GradoActual.Text, Convert.ToInt16(INFOMADE.calcular_edad(Convert.ToDateTime(mtb_f_nacimiento.Text))), true));
                                break;

                            case (2):
                                form.SetField(fieldKey, INFOMADE.nuevogrado(cb_GradoActual.Text, Convert.ToInt16(INFOMADE.calcular_edad(Convert.ToDateTime(mtb_f_nacimiento.Text))), true));
                                break;

                            case (3):
                                form.SetField(fieldKey, nud_Parciales.Text);
                                break;

                            case (4):
                                form.SetField(fieldKey, txt_nombre.Text);
                                break;

                            case (5):
                                form.SetField(fieldKey, INFOMADE.fechaLarga(mtb_f_nacimiento.Text));
                                break;

                            case (6):
                                form.SetField(fieldKey, INFOMADE.calcular_edad(Convert.ToDateTime(mtb_f_nacimiento.Text)));
                                break;

                            case (7):
                                form.SetField(fieldKey, txt_Ocupacion.Text);
                                break;

                            case (8):
                                form.SetField(fieldKey, INFOMADE.fechaLarga(mtb_f_ExamenAnterior.Text));
                                break;

                            case (9):
                                form.SetField(fieldKey, INFOMADE.fechaLarga(mtb_fIngreso.Text));
                                break;

                            case (10):
                                form.SetField(fieldKey, txt_Comentarios.Text);
                                break;

                            default:
                                break;
                            }
                            x++;
                        }

                        // "Flatten" the form so it wont be editable/usable anymore
                        stamper.FormFlattening = true;

                        // You can also specify fields to be flattened, which
                        // leaves the rest of the form still be editable/usable
                        stamper.PartialFormFlattening("field1");

                        stamper.Close();
                        pdfReader.Close();
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #8
0
        public void AddText()
        {
            string existingPdfFile = Path.Combine(GetDllPath(), @"..\..\data\JTS-2016TaxpayerAnnualLocalEITReturnACCTCD-9.pdf");
//            string existingPdfFile = Path.Combine(GetDllPath(), @"..\..\data\fixedJTS.pdf");
            string newPdfFile = Path.Combine(GetDllPath(), @"..\..\data\newJTS-9.pdf");

            Assert.That(File.Exists(existingPdfFile), Is.True);

            using (var newFileStream = new FileStream(newPdfFile, FileMode.Create))
                using (PdfReader pdfReader = new PdfReader(existingPdfFile))
                    using (var stamper = new PdfStamper(pdfReader, newFileStream)) // PdfStamper, which will create
                    {
                        var  form       = stamper.AcroFields;
                        bool xfaPresent = form.Xfa.XfaPresent;
                        var  fieldKeys  = form.Fields.Keys;
                        form.SetField("LAST NAME, FIRST NAME, MIDDLE INITIAL", "Smith Joe");
                        foreach (string fieldKey in fieldKeys)
                        {
                            form.SetField(fieldKey, "REPLACED!");
                            form.SetFieldProperty(fieldKey, "textcolor", BaseColor.BLUE, null);
                        }

                        form.SetField("name", "Smith Joe");
                        form.SetField("street_address", "123  Main St");
                        form.SetField("city_address", "Pittsburgh");
                        form.SetField("state_address", "PA");
                        form.SetField("zip_address", "15235");
                        form.SetField("resident_code_1", "7");
                        form.SetField("resident_code_2", "0");
                        form.SetField("resident_code_3", "0");
                        form.SetField("resident_code_4", "1");
                        form.SetField("resident_code_5", "0");
                        form.SetField("resident_code_6", "1");

                        string[] states = form.GetAppearanceStates("single");
                        form.SetField("single", "Yes");

                        form.SetField("1_1", "55,000");
                        form.SetField("2_1", "0");
                        form.SetField("3_1", "0");
                        form.SetField("4_1", "55,000");
                        form.SetField("5_1", "0");
                        form.SetField("6_1", "0");
                        form.SetField("7_1", "0");
                        form.SetField("8_1", "55,000");
                        form.SetField("9_0", "3%");
                        form.SetField("9_1", "1,650");
                        form.SetField("10_1", "1,650");
                        form.SetField("11_1", "0");
                        form.SetField("12_1", "0");
                        form.SetField("13_1", "1,650");
                        form.SetField("14_1", "0");
                        form.SetField("15_1", "0");
                        form.SetField("16_1", "0");
                        form.SetField("17_1", "0");
                        form.SetField("18_1", "0");
                        form.SetField("19_1", "0");

                        Console.WriteLine("xfaPresent: {0}", xfaPresent);
                        Console.WriteLine("Keys Count: {0}", form.Fields.Count);

                        // "Flatten" the form so it wont be editable/usable anymore
                        stamper.FormFlattening = true;

                        // You can also specify fields to be flattened, which
                        // leaves the rest of the form still be editable/usable
                        stamper.PartialFormFlattening("field1");
                    }
        }
コード例 #9
0
        public void ReplacePdfForm(string outputhpath, string newGrado, string fExamen, string gActual, string nombre, string fNacimiento, string edad, string examenAnterior, string fIngreso)
        {
            try
            {
                String strAppPath      = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                String strFilePath     = Path.Combine(strAppPath, "Resources");
                String strFullFilename = Path.Combine(strFilePath, "Solicitud.pdf");

                //string fileNameExisting = System.AppDomain.CurrentDomain.BaseDirectory + "/Resources/Solicitud.pdf";
                string fileNameExisting = strFullFilename;
                //string fileNameNew = @"C:\Users\Victor\Downloads\new" + filename + ".pdf";
                string fileNameNew = @"" + outputhpath + "/" + nombre + ".pdf";

                using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
                    using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
                    {
                        // Open existing PDF
                        var pdfReader = new PdfReader(existingFileStream);

                        // PdfStamper, which will create
                        var stamper = new PdfStamper(pdfReader, newFileStream);

                        var form      = stamper.AcroFields;
                        var fieldKeys = form.Fields.Keys;
                        int x         = 1;
                        foreach (string fieldKey in fieldKeys)
                        {
                            //form.SetField(fieldKey, x.ToString());

                            switch (x)
                            {
                            case (1):
                                form.SetField(fieldKey, "");
                                break;

                            case (2):
                                form.SetField(fieldKey, infomade.gradoActual(gActual, Convert.ToInt16(edad)));
                                break;

                            case (3):
                                form.SetField(fieldKey, newGrado);
                                break;

                            case (4):
                                form.SetField(fieldKey, nombre);
                                break;

                            case (5):
                                form.SetField(fieldKey, infomade.fechaLarga(fNacimiento));
                                break;

                            case (6):
                                form.SetField(fieldKey, edad);
                                break;

                            case (7):
                                form.SetField(fieldKey, infomade.fechaLarga(examenAnterior));
                                break;

                            case (8):
                                form.SetField(fieldKey, infomade.fechaLarga(fIngreso));
                                break;

                            case (9):
                                form.SetField(fieldKey, infomade.fechaLarga(fExamen));
                                break;

                            default:
                                break;
                            }
                            x++;
                        }

                        // "Flatten" the form so it wont be editable/usable anymore
                        stamper.FormFlattening = true;

                        // You can also specify fields to be flattened, which
                        // leaves the rest of the form still be editable/usable
                        stamper.PartialFormFlattening("field1");

                        stamper.Close();
                        pdfReader.Close();
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }