Skip to content

Commit 6ed0578

Browse files
author
Eugene Bochilo
committed
Add merging for dirty annotation in checkboxes and fix AddIndexStrategy bug
DEVSIX-8806
1 parent dee84be commit 6ed0578

File tree

8 files changed

+48
-20
lines changed

8 files changed

+48
-20
lines changed

forms/src/main/java/com/itextpdf/forms/fields/PdfFormFieldMergeUtil.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,15 @@ public static void processDirtyAnnotations(PdfFormField parentField, boolean thr
136136
for (PdfFormField field : parentField.getChildFormFields()) {
137137
PdfDictionary formDict = field.getPdfObject();
138138
// Process form fields without PdfName.Widget having only annotations as children
139-
if (field.getChildFields().size() > 0 && field.getChildFormFields().size() == 0) {
139+
if (!field.getChildFields().isEmpty() && field.getChildFormFields().isEmpty()) {
140140
boolean shouldBeMerged = true;
141141

142-
// If parent is radio button or signature we don't care about field related keys, always merge
142+
// If parent is radio button, checkbox or signature we don't care about field related keys, always merge
143143
// If not - go over all fields to compare with parent's fields
144-
if (!(PdfName.Btn.equals(parentField.getFormType()) &&
145-
parentField.getFieldFlag(PdfButtonFormField.FF_RADIO)) &&
146-
!PdfName.Sig.equals(parentField.getFormType())) {
147-
if (formDict.containsKey(PdfName.T)) {
148-
// We only want to perform the merge if field doesn't contain any name (even empty one)
149-
continue;
150-
}
144+
boolean isRadioOrCheckbox = PdfName.Btn.equals(parentField.getFormType()) &&
145+
!parentField.getFieldFlag(PdfButtonFormField.FF_PUSH_BUTTON);
146+
boolean isSignature = PdfName.Sig.equals(parentField.getFormType());
147+
if (!isRadioOrCheckbox && !isSignature) {
151148
for (final PdfName key : formDict.keySet()) {
152149
// Everything except Parent and Kids must be identical to allow the merge
153150
if (!PdfName.Parent.equals(key) && !PdfName.Kids.equals(key) &&
@@ -158,6 +155,13 @@ public static void processDirtyAnnotations(PdfFormField parentField, boolean thr
158155
}
159156
}
160157

158+
boolean isRadioButton = PdfName.Btn.equals(parentField.getFormType()) &&
159+
parentField.getFieldFlag(PdfButtonFormField.FF_RADIO);
160+
if (formDict.containsKey(PdfName.T) && !isRadioButton && !isSignature) {
161+
// We only want to perform the merge if field doesn't contain any name (even empty one).
162+
// The only exceptions are radio buttons and signatures.
163+
continue;
164+
}
161165
if (shouldBeMerged) {
162166
parentField.removeChild(field);
163167
formDict.remove(PdfName.Parent);

forms/src/main/java/com/itextpdf/forms/fields/merging/AddIndexStrategy.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ public AddIndexStrategy() {
6868
* @param secondField the second field
6969
* @param throwExceptionOnError if true, an exception will be thrown
7070
*
71-
* @return true if the second field was renamed successfully, false otherwise
71+
* @return returns {@code false} value, since {@link AddIndexStrategy} never merges fields.
7272
*/
7373
@Override
7474
public boolean execute(PdfFormField firstField, PdfFormField secondField, boolean throwExceptionOnError) {
75-
if (firstField == null || secondField == null) {
76-
return false;
75+
if (firstField != null && secondField != null &&
76+
firstField.getFieldName() != null && secondField.getFieldName() != null) {
77+
String originalFieldName = firstField.getFieldName().toUnicodeString();
78+
String fieldToAddNewName = originalFieldName + separator + getNextIndex(originalFieldName);
79+
secondField.setFieldName(fieldToAddNewName);
7780
}
78-
if (firstField.getFieldName() == null || secondField.getFieldName() == null) {
79-
return true;
80-
}
81-
String originalFieldName = firstField.getFieldName().toUnicodeString();
82-
String fieldToAddNewName = originalFieldName + separator + getNextIndex(originalFieldName);
83-
secondField.setFieldName(fieldToAddNewName);
84-
return true;
81+
return false;
8582
}
8683

8784
int getNextIndex(String name) {

forms/src/main/java/com/itextpdf/forms/fields/merging/OnDuplicateFormFieldNameStrategy.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface OnDuplicateFormFieldNameStrategy {
3737
* @param secondField the second field
3838
* @param throwExceptionOnError if true, an exception will be thrown
3939
*
40-
* @return true if the second field was renamed successfully, false otherwise
40+
* @return true if the second field was merged successfully, false otherwise
4141
*/
4242
boolean execute(PdfFormField firstField, PdfFormField secondField, boolean throwExceptionOnError);
4343
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,19 @@ public void pdfWithSignatureAndFontInBuilderFieldTest() throws IOException, Inte
19641964
"diff_"));
19651965
}
19661966

1967+
@Test
1968+
public void dirtyCheckBoxAnnotationMergedTest() throws IOException, InterruptedException {
1969+
String outputFileName = destinationFolder + "dirtyCheckBoxAnnotationMergedTest.pdf";
1970+
String inputFileName = sourceFolder + "dirtyCheckBoxAnnotationMergedTest.pdf";
1971+
String cmpFileName = sourceFolder + "cmp_dirtyCheckBoxAnnotationMergedTest.pdf";
1972+
try (PdfDocument pdf = new PdfDocument(new PdfReader(inputFileName), new PdfWriter(outputFileName))) {
1973+
PdfFormCreator.getAcroForm(pdf, false);
1974+
// Do nothing.
1975+
}
1976+
1977+
Assertions.assertNull(new CompareTool().compareByContent(outputFileName, cmpFileName, destinationFolder, "diff_"));
1978+
}
1979+
19671980
@Test
19681981
@LogMessages(messages = @LogMessage(messageTemplate = FormsLogMessageConstants.FORM_FIELD_HAS_CYCLED_PARENT_STRUCTURE,
19691982
ignore = true))

forms/src/test/java/com/itextpdf/forms/fields/merging/OnDuplicateFormFieldNameStrategyTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,20 @@ public void flattenReadOnlyAddIndexTo() throws IOException, InterruptedException
260260
SOURCE_FOLDER + "cmp_flattenReadOnlyAddIndexTo.pdf", DESTINATION_FOLDER, "diff_"));
261261
}
262262

263+
@Test
264+
public void fieldsWithSameNamesTest() throws IOException {
265+
String source = SOURCE_FOLDER + "fieldsWithSameNamesTest.pdf";
266+
try (PdfDocument pdfDocument = new PdfDocument(new PdfReader(source), new PdfWriter(new ByteArrayOutputStream()))) {
267+
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDocument, false, new AddIndexStrategy());
268+
269+
Assertions.assertNotNull(form.getField("test"));
270+
Assertions.assertNotNull(form.getField("test_1"));
271+
Assertions.assertNotNull(form.getField("test_2"));
272+
Assertions.assertNotNull(form.getField("bingbong"));
273+
Assertions.assertNotNull(form.getField("bingbong_1"));
274+
Assertions.assertNotNull(form.getField("bingbong_2"));
275+
}
276+
}
263277

264278
@Test
265279
public void addIndexStrategySeparatesTheFields() throws IOException, InterruptedException {
Binary file not shown.

0 commit comments

Comments
 (0)