Skip to content

Commit e345553

Browse files
committed
Update PdfFormField#setDefaultAppearance()
Save original font and fontSize for /DA. Do not override /DA if inherited is the same. DEVSIX-1916
1 parent eceaa07 commit e345553

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,17 +1658,22 @@ public PdfString getDefaultAppearance() {
16581658
* Sets default appearance string containing a sequence of valid page-content graphics or text state operators that
16591659
* define such properties as the field's text size and color.
16601660
*
1661+
* If form field has the same default appearance (incl. inherited) it won't be updated.
1662+
*
16611663
* @param defaultAppearance a valid sequence of PDF content stream syntax
16621664
* @return the edited field
16631665
*/
16641666
public PdfFormField setDefaultAppearance(String defaultAppearance) {
1665-
byte[] b = defaultAppearance.getBytes(StandardCharsets.UTF_8);
1666-
int len = b.length;
1667-
for (int k = 0; k < len; ++k) {
1668-
if (b[k] == '\n')
1669-
b[k] = 32;
1667+
PdfString prev = getDefaultAppearance();
1668+
if (prev == null || !defaultAppearance.trim().equals(prev.getValue().trim())) {
1669+
byte[] b = defaultAppearance.getBytes(StandardCharsets.UTF_8);
1670+
int len = b.length;
1671+
for (int k = 0; k < len; ++k) {
1672+
if (b[k] == '\n')
1673+
b[k] = 32;
1674+
}
1675+
getPdfObject().put(PdfName.DA, new PdfString(new String(b)));
16701676
}
1671-
getPdfObject().put(PdfName.DA, new PdfString(new String(b)));
16721677
return this;
16731678
}
16741679

@@ -2047,10 +2052,8 @@ public boolean regenerateField() {
20472052
AppearanceXObject appearance = null;
20482053
if (asNormal != null) {
20492054
appearance = new AppearanceXObject(asNormal);
2050-
20512055
appearance.setBBox(new PdfArray(new float[]{0, 0, bBox.toRectangle().getWidth(), bBox.toRectangle().getHeight()}));
2052-
}
2053-
if (appearance == null) {
2056+
} else {
20542057
appearance = new AppearanceXObject(new Rectangle(0, 0, bBox.toRectangle().getWidth(), bBox.toRectangle().getHeight()));
20552058
}
20562059
appearance.addFontFromDR(localFontName, localFont);
@@ -2182,8 +2185,11 @@ public boolean regenerateField() {
21822185
private float normalizeFontSize(float fs, PdfFont localFont, PdfArray bBox, String value) {
21832186
if (fs == 0) {
21842187
if (isMultiline()) {
2188+
//We do not support autosize with multiline.
21852189
fontSize = DEFAULT_FONT_SIZE;
21862190
} else {
2191+
// Save it for Default Appearance.
2192+
fontSize = 0;
21872193
fs = approximateFontSizeToFitBBox(localFont, bBox.toRectangle(), value);
21882194
}
21892195
}
@@ -2624,7 +2630,21 @@ protected static PdfArray processOptions(String[] options) {
26242630
return array;
26252631
}
26262632

2633+
/**
2634+
* Generate default appearance, /DA key.
2635+
*
2636+
* @param font preferred font. If {@link #getFont()} is not null, it will be used instead.
2637+
* @param fontSize preferred font size. If {@link PdfFormField#fontSize} is valid,
2638+
* it will be used instead.
2639+
* @return generated string
2640+
*/
26272641
protected String generateDefaultAppearanceString(PdfFont font, float fontSize, Color color, PdfResources res) {
2642+
if (this.fontSize >= 0) {
2643+
fontSize = this.fontSize;
2644+
}
2645+
if (this.font != null) {
2646+
font = this.font;
2647+
}
26282648
PdfStream stream = new PdfStream();
26292649
PdfCanvas canvas = new PdfCanvas(stream, res, getDocument());
26302650
canvas.setFontAndSize(font, fontSize);
@@ -2762,10 +2782,7 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
27622782
PdfResources resources = appearance.getResources();
27632783
PdfCanvas canvas = new PdfCanvas(stream, resources, getDocument());
27642784

2765-
//TODO seems that FontName shall be passed
2766-
if (getDefaultAppearance() == null) {
2767-
setDefaultAppearance(generateDefaultAppearanceString(font, fontSize, color, resources));
2768-
}
2785+
setDefaultAppearance(generateDefaultAppearanceString(font, fontSize, color, resources));
27692786

27702787
float height = rect.getHeight();
27712788
float width = rect.getWidth();
@@ -3181,10 +3198,8 @@ protected PdfFormXObject drawPushButtonAppearance(float width, float height, Str
31813198
} else {
31823199
drawButton(canvas, 0, 0, width, height, text, font, fontSize);
31833200
xObject.addFontFromDR(fontName, font);
3184-
if (getDefaultAppearance() == null) {
3185-
setDefaultAppearance(generateDefaultAppearanceString(font, fontSize, color, new PdfResources()));
3186-
xObject.getResources().addFont(getDocument(), font);
3187-
}
3201+
setDefaultAppearance(generateDefaultAppearanceString(font, fontSize, color, new PdfResources()));
3202+
xObject.getResources().addFont(getDocument(), font);
31883203
}
31893204
xObject.getPdfObject().getOutputStream().writeBytes(stream.getBytes());
31903205

@@ -3233,7 +3248,6 @@ protected void drawCheckBox(PdfCanvas canvas, float width, float height, float f
32333248
DrawingUtil.drawCross(canvas, width, height, borderWidth);
32343249
return;
32353250
}
3236-
//TODO what is current font actually?
32373251
PdfFont ufont = getFont();
32383252
if (fontSize <= 0) {
32393253
fontSize = approximateFontSizeToFitBBox(ufont, new Rectangle(width, height), text);
Binary file not shown.

0 commit comments

Comments
 (0)