public virtual byte[] CreatePartiallyFlattenedForm() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos)); Document doc = new Document(pdfDoc); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); IDictionary <String, PdfFormField> fields = form.GetFormFields(); fields["sunday_1"].SetValue("1"); fields["sunday_2"].SetValue("2"); fields["sunday_3"].SetValue("3"); fields["sunday_4"].SetValue("4"); fields["sunday_5"].SetValue("5"); fields["sunday_6"].SetValue("6"); // Add the fields, identified by name, to the list of fields to be flattened form.PartialFormFlattening("sunday_1"); form.PartialFormFlattening("sunday_2"); form.PartialFormFlattening("sunday_3"); form.PartialFormFlattening("sunday_4"); form.PartialFormFlattening("sunday_5"); form.PartialFormFlattening("sunday_6"); // Only the included above fields are flattened. // If no fields have been explicitly included, then all fields are flattened. form.FlattenFields(); doc.Close(); return(baos.ToArray()); }
public virtual void GetFieldsForFlatteningTest() { String outPdfName = destinationFolder + "flattenedFormField.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "formFieldFile.pdf"), new PdfWriter(outPdfName )); PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, false); NUnit.Framework.Assert.AreEqual(0, form.GetFieldsForFlattening().Count); form.PartialFormFlattening("radioName"); form.PartialFormFlattening("Text1"); PdfFormField radioNameField = form.GetField("radioName"); PdfFormField text1Field = form.GetField("Text1"); NUnit.Framework.Assert.AreEqual(2, form.GetFieldsForFlattening().Count); NUnit.Framework.Assert.IsTrue(form.GetFieldsForFlattening().Contains(radioNameField)); NUnit.Framework.Assert.IsTrue(form.GetFieldsForFlattening().Contains(text1Field)); form.FlattenFields(); pdfDoc.Close(); PdfDocument outPdfDoc = new PdfDocument(new PdfReader(outPdfName)); PdfAcroForm outPdfForm = PdfAcroForm.GetAcroForm(outPdfDoc, false); NUnit.Framework.Assert.AreEqual(2, outPdfForm.GetFormFields().Count); outPdfDoc.Close(); }
public void UpdatePDFFields(string filePath, string prefix, string outputLocation, IEnumerable <PDFField> pdfFields) { using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(filePath), new PdfWriter(outputLocation))) { PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true); //Async causes crash foreach (var value in pdfFields) { form.GetField(value.Name).SetValue(value.Value); form.PartialFormFlattening(value.Name); } //Flattens all fields OR Flattens all fields marked to be flattened form.FlattenFields(); } }