Skip to content

Commit 83e94be

Browse files
committed
Forms. Fix issue with inherited default appearance
DA shall be taken from AcroForm, if there is no parent. DEVSIX-1677
1 parent eaf2d91 commit 83e94be

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ public PdfString getDefaultAppearance() {
14971497
}
14981498
}
14991499
}
1500+
//DA is an inherited key, therefore AcroForm shall be checked if there is no parent or no DA in parent.
1501+
if (defaultAppearance == null) {
1502+
defaultAppearance = (PdfString) getAcroFormKey(PdfName.DA, PdfObject.STRING);
1503+
}
15001504
return defaultAppearance;
15011505
}
15021506

@@ -1885,9 +1889,7 @@ public boolean regenerateField() {
18851889
if (appearance == null) {
18861890
appearance = new PdfFormXObject(new Rectangle(0, 0, bBox.toRectangle().getWidth(), bBox.toRectangle().getHeight()));
18871891
}
1888-
if (matrix != null) {
1889-
appearance.put(PdfName.Matrix, matrix);
1890-
}
1892+
appearance.put(PdfName.Matrix, matrix);
18911893
//Create text appearance
18921894
if (PdfName.Tx.equals(type)) {
18931895
if (!isMultiline()) {
@@ -2007,6 +2009,18 @@ public boolean regenerateField() {
20072009
return true;
20082010
}
20092011

2012+
private PdfObject getAcroFormKey(PdfName key, int type) {
2013+
PdfObject acroFormKey = null;
2014+
PdfDocument document = getDocument();
2015+
if (document != null) {
2016+
PdfDictionary acroFormDictionary = document.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
2017+
if (acroFormDictionary != null) {
2018+
acroFormKey = acroFormDictionary.get(key);
2019+
}
2020+
}
2021+
return (acroFormKey != null && acroFormKey.getType() == type) ? acroFormKey : null;
2022+
}
2023+
20102024
/**
20112025
* According to spec (ISO-32000-1, 12.7.3.3) zero font size should interpretaded as auto size.
20122026
*/
@@ -2464,12 +2478,7 @@ protected Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
24642478
PdfDictionary normalResources = null;
24652479
PdfDictionary defaultResources = null;
24662480
PdfDocument document = getDocument();
2467-
if (document != null) {
2468-
PdfDictionary acroformDictionary = document.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
2469-
if (acroformDictionary != null) {
2470-
defaultResources = acroformDictionary.getAsDictionary(PdfName.DR);
2471-
}
2472-
}
2481+
defaultResources = (PdfDictionary) getAcroFormKey(PdfName.DR, PdfObject.DICTIONARY);
24732482
if (asNormal != null) {
24742483
normalResources = asNormal.getAsDictionary(PdfName.Resources);
24752484
}

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,7 @@ public void formFlatteningTest01() throws IOException, InterruptedException {
7979

8080
doc.close();
8181

82-
CompareTool compareTool = new CompareTool();
83-
String errorMessage = compareTool.compareByContent(filename, sourceFolder + "cmp_formFlatteningTest01.pdf", destinationFolder, "diff_");
84-
if (errorMessage != null) {
85-
Assert.fail(errorMessage);
86-
}
82+
Assert.assertNull(new CompareTool().compareByContent(filename, sourceFolder + "cmp_formFlatteningTest01.pdf", destinationFolder, "diff_"));
8783
}
8884

8985
@Test
@@ -105,11 +101,7 @@ public void formFlatteningTest_DefaultAppearanceGeneration_Rot0() throws IOExcep
105101

106102
doc.close();
107103

108-
CompareTool compareTool = new CompareTool();
109-
String errorMessage = compareTool.compareByContent(dest, cmp, destinationFolder, "diff_");
110-
if (errorMessage != null) {
111-
Assert.fail(errorMessage);
112-
}
104+
Assert.assertNull(new CompareTool().compareByContent(dest, cmp, destinationFolder, "diff_"));
113105
}
114106

115107
}
@@ -133,11 +125,7 @@ public void formFlatteningTest_DefaultAppearanceGeneration_Rot90() throws IOExce
133125

134126
doc.close();
135127

136-
CompareTool compareTool = new CompareTool();
137-
String errorMessage = compareTool.compareByContent(dest, cmp, destinationFolder, "diff_");
138-
if (errorMessage != null) {
139-
Assert.fail(errorMessage);
140-
}
128+
Assert.assertNull(new CompareTool().compareByContent(dest, cmp, destinationFolder, "diff_"));
141129
}
142130

143131
}
@@ -161,11 +149,7 @@ public void formFlatteningTest_DefaultAppearanceGeneration_Rot180() throws IOExc
161149

162150
doc.close();
163151

164-
CompareTool compareTool = new CompareTool();
165-
String errorMessage = compareTool.compareByContent(dest, cmp, destinationFolder, "diff_");
166-
if (errorMessage != null) {
167-
Assert.fail(errorMessage);
168-
}
152+
Assert.assertNull(new CompareTool().compareByContent(dest, cmp, destinationFolder, "diff_"));
169153
}
170154

171155
}
@@ -189,12 +173,7 @@ public void formFlatteningTest_DefaultAppearanceGeneration_Rot270() throws IOExc
189173

190174
doc.close();
191175

192-
CompareTool compareTool = new CompareTool();
193-
String errorMessage = compareTool.compareByContent(dest, cmp, destinationFolder, "diff_");
194-
if (errorMessage != null) {
195-
Assert.fail(errorMessage);
196-
}
176+
Assert.assertNull(new CompareTool().compareByContent(dest, cmp, destinationFolder, "diff_"));
197177
}
198-
199178
}
200179
}

0 commit comments

Comments
 (0)