@@ -1658,17 +1658,22 @@ public PdfString getDefaultAppearance() {
1658
1658
* Sets default appearance string containing a sequence of valid page-content graphics or text state operators that
1659
1659
* define such properties as the field's text size and color.
1660
1660
*
1661
+ * If form field has the same default appearance (incl. inherited) it won't be updated.
1662
+ *
1661
1663
* @param defaultAppearance a valid sequence of PDF content stream syntax
1662
1664
* @return the edited field
1663
1665
*/
1664
1666
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 )));
1670
1676
}
1671
- getPdfObject ().put (PdfName .DA , new PdfString (new String (b )));
1672
1677
return this ;
1673
1678
}
1674
1679
@@ -2047,10 +2052,8 @@ public boolean regenerateField() {
2047
2052
AppearanceXObject appearance = null ;
2048
2053
if (asNormal != null ) {
2049
2054
appearance = new AppearanceXObject (asNormal );
2050
-
2051
2055
appearance .setBBox (new PdfArray (new float []{0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()}));
2052
- }
2053
- if (appearance == null ) {
2056
+ } else {
2054
2057
appearance = new AppearanceXObject (new Rectangle (0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()));
2055
2058
}
2056
2059
appearance .addFontFromDR (localFontName , localFont );
@@ -2182,8 +2185,11 @@ public boolean regenerateField() {
2182
2185
private float normalizeFontSize (float fs , PdfFont localFont , PdfArray bBox , String value ) {
2183
2186
if (fs == 0 ) {
2184
2187
if (isMultiline ()) {
2188
+ //We do not support autosize with multiline.
2185
2189
fontSize = DEFAULT_FONT_SIZE ;
2186
2190
} else {
2191
+ // Save it for Default Appearance.
2192
+ fontSize = 0 ;
2187
2193
fs = approximateFontSizeToFitBBox (localFont , bBox .toRectangle (), value );
2188
2194
}
2189
2195
}
@@ -2624,7 +2630,21 @@ protected static PdfArray processOptions(String[] options) {
2624
2630
return array ;
2625
2631
}
2626
2632
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
+ */
2627
2641
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
+ }
2628
2648
PdfStream stream = new PdfStream ();
2629
2649
PdfCanvas canvas = new PdfCanvas (stream , res , getDocument ());
2630
2650
canvas .setFontAndSize (font , fontSize );
@@ -2762,10 +2782,7 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
2762
2782
PdfResources resources = appearance .getResources ();
2763
2783
PdfCanvas canvas = new PdfCanvas (stream , resources , getDocument ());
2764
2784
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 ));
2769
2786
2770
2787
float height = rect .getHeight ();
2771
2788
float width = rect .getWidth ();
@@ -3181,10 +3198,8 @@ protected PdfFormXObject drawPushButtonAppearance(float width, float height, Str
3181
3198
} else {
3182
3199
drawButton (canvas , 0 , 0 , width , height , text , font , fontSize );
3183
3200
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 );
3188
3203
}
3189
3204
xObject .getPdfObject ().getOutputStream ().writeBytes (stream .getBytes ());
3190
3205
@@ -3233,7 +3248,6 @@ protected void drawCheckBox(PdfCanvas canvas, float width, float height, float f
3233
3248
DrawingUtil .drawCross (canvas , width , height , borderWidth );
3234
3249
return ;
3235
3250
}
3236
- //TODO what is current font actually?
3237
3251
PdfFont ufont = getFont ();
3238
3252
if (fontSize <= 0 ) {
3239
3253
fontSize = approximateFontSizeToFitBBox (ufont , new Rectangle (width , height ), text );
0 commit comments