Skip to content

Commit 6425595

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 824d4b6 commit 6425595

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,10 @@ public PdfString getDefaultAppearance() {
16241624
}
16251625
}
16261626
}
1627+
//DA is an inherited key, therefore AcroForm shall be checked if there is no parent or no DA in parent.
1628+
if (defaultAppearance == null) {
1629+
defaultAppearance = (PdfString) getAcroFormKey(PdfName.DA, PdfObject.STRING);
1630+
}
16271631
return defaultAppearance;
16281632
}
16291633

@@ -2019,9 +2023,7 @@ public boolean regenerateField() {
20192023
if (appearance == null) {
20202024
appearance = new PdfFormXObject(new Rectangle(0, 0, bBox.toRectangle().getWidth(), bBox.toRectangle().getHeight()));
20212025
}
2022-
if (matrix != null) {
2023-
appearance.put(PdfName.Matrix, matrix);
2024-
}
2026+
appearance.put(PdfName.Matrix, matrix);
20252027
//Create text appearance
20262028
if (PdfName.Tx.equals(type)) {
20272029
if (!isMultiline()) {
@@ -2585,19 +2587,15 @@ protected Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
25852587
PdfDictionary normalResources = null;
25862588
PdfDictionary defaultResources = null;
25872589
PdfDocument document = getDocument();
2588-
if (document != null) {
2589-
PdfDictionary acroformDictionary = document.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
2590-
if (acroformDictionary != null) {
2591-
defaultResources = acroformDictionary.getAsDictionary(PdfName.DR);
2592-
}
2593-
}
2590+
defaultResources = (PdfDictionary) getAcroFormKey(PdfName.DR, PdfObject.DICTIONARY);
25942591
if (asNormal != null) {
25952592
normalResources = asNormal.getAsDictionary(PdfName.Resources);
25962593
}
25972594
if (defaultResources != null || normalResources != null) {
25982595
PdfDictionary normalFontDic = normalResources != null ? normalResources.getAsDictionary(PdfName.Font) : null;
25992596
PdfDictionary defaultFontDic = defaultResources != null ? defaultResources.getAsDictionary(PdfName.Font) : null;
26002597
PdfString defaultAppearance = getDefaultAppearance();
2598+
26012599
if ((normalFontDic != null || defaultFontDic != null) && defaultAppearance != null) {
26022600
Object[] dab = splitDAelements(defaultAppearance.toUnicodeString());
26032601
PdfName fontName = new PdfName(dab[DA_FONT].toString());
@@ -3210,6 +3208,18 @@ protected void drawPdfACheckBox(PdfCanvas canvas, float width, float height, boo
32103208
}
32113209
}
32123210

3211+
private PdfObject getAcroFormKey(PdfName key, int type) {
3212+
PdfObject acroFormKey = null;
3213+
PdfDocument document = getDocument();
3214+
if (document != null) {
3215+
PdfDictionary acroFormDictionary = document.getCatalog().getPdfObject().getAsDictionary(PdfName.AcroForm);
3216+
if (acroFormDictionary != null) {
3217+
acroFormKey = acroFormDictionary.get(key);
3218+
}
3219+
}
3220+
return (acroFormKey != null && acroFormKey.getType() == type) ? acroFormKey : null;
3221+
}
3222+
32133223
private TextAlignment convertJustificationToTextAlignment() {
32143224
Integer justification = getJustification();
32153225
if (justification == null) {

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)