Skip to content

Commit 2e36f5c

Browse files
committed
Add a getter for a set of fields for flattening in PdfAcroForm
ITXT-CR-906
1 parent 2deec7b commit 2e36f5c

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

forms/src/main/java/com/itextpdf/forms/PdfAcroForm.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ This file is part of the iText (R) project.
7171
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
7272

7373
import java.util.ArrayList;
74+
import java.util.Collection;
75+
import java.util.Collections;
7476
import java.util.LinkedHashMap;
7577
import java.util.LinkedHashSet;
7678
import java.util.List;
@@ -288,6 +290,16 @@ public Map<String, PdfFormField> getFormFields() {
288290
return fields;
289291
}
290292

293+
/**
294+
* Gets a collection of {@link PdfFormField form field}s, prepared for flattening using {@link #partialFormFlattening} method.
295+
* If returned collection is empty, all form fields will be flattened on {@link #flattenFields flattenFields} call.
296+
*
297+
* @return a collection of {@link PdfFormField form field}s for flattening
298+
*/
299+
public Collection<PdfFormField> getFieldsForFlattening() {
300+
return Collections.unmodifiableCollection(fieldsForFlattening);
301+
}
302+
291303
/**
292304
* Gets the {@link PdfDocument} this {@link PdfAcroForm} belongs to.
293305
*
@@ -605,7 +617,7 @@ public void setGenerateAppearance(boolean generateAppearance) {
605617

606618
/**
607619
* Flattens interactive {@link PdfFormField form field}s in the document. If
608-
* no fields have been explicitly included via {#link #partialFormFlattening},
620+
* no fields have been explicitly included via {@link #partialFormFlattening},
609621
* then all fields are flattened. Otherwise only the included fields are
610622
* flattened.
611623
*/

forms/src/test/java/com/itextpdf/forms/FormFieldFlatteningTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,36 @@ public static void beforeClass() {
7373
createOrClearDestinationFolder(destinationFolder);
7474
}
7575

76+
@Test
77+
public void getFieldsForFlatteningTest() throws IOException {
78+
String outPdfName = destinationFolder + "flattenedFormField.pdf";
79+
PdfDocument pdfDoc = new PdfDocument(new PdfReader(sourceFolder + "formFieldFile.pdf"), new PdfWriter(outPdfName));
80+
81+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);
82+
83+
Assert.assertEquals(0, form.getFieldsForFlattening().size());
84+
85+
form.partialFormFlattening("radioName");
86+
form.partialFormFlattening("Text1");
87+
88+
PdfFormField radioNameField = form.getField("radioName");
89+
PdfFormField text1Field = form.getField("Text1");
90+
91+
Assert.assertEquals(2, form.getFieldsForFlattening().size());
92+
Assert.assertTrue( form.getFieldsForFlattening().contains(radioNameField));
93+
Assert.assertTrue( form.getFieldsForFlattening().contains(text1Field));
94+
95+
form.flattenFields();
96+
pdfDoc.close();
97+
98+
PdfDocument outPdfDoc = new PdfDocument(new PdfReader(outPdfName));
99+
PdfAcroForm outPdfForm = PdfAcroForm.getAcroForm(outPdfDoc, false);
100+
101+
Assert.assertEquals(2, outPdfForm.getFormFields().size());
102+
103+
outPdfDoc.close();
104+
}
105+
76106
@Test
77107
public void formFlatteningTest01() throws IOException, InterruptedException {
78108
String srcFilename = "formFlatteningSource.pdf";

0 commit comments

Comments
 (0)