@@ -101,7 +101,7 @@ This file is part of the iText (R) project.
101
101
/**
102
102
* This class represents a single field or field group in an {@link com.itextpdf.forms.PdfAcroForm
103
103
* AcroForm}.
104
- * <p>
104
+ *
105
105
* <br><br>
106
106
* To be able to be wrapped with this {@link PdfObjectWrapper} the {@link PdfObject}
107
107
* must be indirect.
@@ -795,7 +795,7 @@ public static PdfButtonFormField createPushButton(PdfDocument doc, Rectangle rec
795
795
field .font = font ;
796
796
field .fontSize = fontSize ;
797
797
798
- PdfFormXObject xObject = field .drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), caption , font , fontSize );
798
+ PdfFormXObject xObject = field .drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), caption , font , null , fontSize );
799
799
annot .setNormalAppearance (xObject .getPdfObject ());
800
800
801
801
PdfDictionary mk = new PdfDictionary ();
@@ -1513,8 +1513,7 @@ public PdfString getDefaultAppearance() {
1513
1513
*/
1514
1514
public PdfFormField setDefaultAppearance (String defaultAppearance ) {
1515
1515
byte [] b = defaultAppearance .getBytes (StandardCharsets .UTF_8 );
1516
- int len = b .length ;
1517
- for (int k = 0 ; k < len ; ++k ) {
1516
+ for (int k = 0 ; k < b .length ; ++k ) {
1518
1517
if (b [k ] == '\n' )
1519
1518
b [k ] = 32 ;
1520
1519
}
@@ -1805,7 +1804,10 @@ public boolean regenerateField() {
1805
1804
1806
1805
Object [] fontAndSize = getFontAndSize (asNormal );
1807
1806
PdfFont localFont = (PdfFont ) fontAndSize [0 ];
1808
- float fontSize = normalizeFontSize ((float ) fontAndSize [1 ], localFont , bBox , value );
1807
+ PdfName localFontName = (PdfName ) fontAndSize [2 ];
1808
+ // Save it for Default Appearance.
1809
+ this .fontSize = (float ) fontAndSize [1 ];
1810
+ float fontSize = normalizeFontSize (this .fontSize , localFont , bBox , value );
1809
1811
1810
1812
//Apply Page rotation
1811
1813
int pageRotation = 0 ;
@@ -1886,14 +1888,14 @@ public boolean regenerateField() {
1886
1888
bBox = new PdfArray (rect );
1887
1889
}
1888
1890
//Create appearance
1889
- PdfFormXObject appearance = null ;
1891
+ AppearanceXObject appearance ;
1890
1892
if (asNormal != null ) {
1891
- appearance = new PdfFormXObject (asNormal );
1893
+ appearance = new AppearanceXObject (asNormal );
1892
1894
appearance .setBBox (new PdfArray (new float []{0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()}));
1895
+ } else {
1896
+ appearance = new AppearanceXObject (new Rectangle (0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()));
1893
1897
}
1894
- if (appearance == null ) {
1895
- appearance = new PdfFormXObject (new Rectangle (0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()));
1896
- }
1898
+ appearance .addFontFromDR (localFontName , localFont );
1897
1899
appearance .put (PdfName .Matrix , matrix );
1898
1900
//Create text appearance
1899
1901
if (PdfName .Tx .equals (type )) {
@@ -1943,20 +1945,19 @@ public boolean regenerateField() {
1943
1945
apDic = widgets .get (0 ).getPdfObject ().getAsDictionary (PdfName .AP );
1944
1946
}
1945
1947
}
1946
- if (img != null ) {
1947
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , 0 );
1948
- } else if (form != null ) {
1949
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , 0 );
1948
+ if (img != null || form != null ) {
1949
+ appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , null , 0 );
1950
1950
} else {
1951
1951
PdfStream asNormal = null ;
1952
1952
if (apDic != null ) {
1953
1953
asNormal = apDic .getAsStream (PdfName .N );
1954
1954
}
1955
1955
Object [] fontAndSize = getFontAndSize (asNormal );
1956
1956
PdfFont localFont = (PdfFont ) fontAndSize [0 ];
1957
+ PdfName localFontName = (PdfName ) fontAndSize [2 ];
1957
1958
float fontSize = (float ) fontAndSize [1 ];
1958
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , localFont , fontSize );
1959
- appearance . getResources (). addFont ( getDocument (), localFont );
1959
+ appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value ,
1960
+ localFont , localFontName , fontSize );
1960
1961
}
1961
1962
1962
1963
if (apDic == null ) {
@@ -2032,6 +2033,7 @@ private PdfObject getAcroFormKey(PdfName key, int type) {
2032
2033
private float normalizeFontSize (float fs , PdfFont localFont , PdfArray bBox , String value ) {
2033
2034
if (fs == 0 ) {
2034
2035
if (isMultiline ()) {
2036
+ //We do not support autosize with multiline.
2035
2037
fontSize = DEFAULT_FONT_SIZE ;
2036
2038
} else {
2037
2039
float height = bBox .toRectangle ().getHeight () - borderWidth * 2 ;
@@ -2457,7 +2459,21 @@ protected static PdfArray processOptions(String[] options) {
2457
2459
return array ;
2458
2460
}
2459
2461
2462
+ /**
2463
+ * Generate default appearance, /DA key.
2464
+ *
2465
+ * @param font preferred font. If {@link #getFont()} is not null, it will be used instead.
2466
+ * @param fontSize preferred font size. If {@link PdfFormField#fontSize} is valid,
2467
+ * it will be used instead.
2468
+ * @return generated string
2469
+ */
2460
2470
protected String generateDefaultAppearanceString (PdfFont font , float fontSize , Color color , PdfResources res ) {
2471
+ if (this .fontSize >= 0 ) {
2472
+ fontSize = this .fontSize ;
2473
+ }
2474
+ if (this .font != null ) {
2475
+ font = this .font ;
2476
+ }
2461
2477
PdfStream stream = new PdfStream ();
2462
2478
PdfCanvas canvas = new PdfCanvas (stream , res , getDocument ());
2463
2479
canvas .setFontAndSize (font , fontSize );
@@ -2479,7 +2495,7 @@ protected String generateDefaultAppearanceString(PdfFont font, int fontSize, Pdf
2479
2495
}
2480
2496
2481
2497
protected Object [] getFontAndSize (PdfDictionary asNormal ) throws IOException {
2482
- Object [] fontAndSize = new Object [2 ];
2498
+ Object [] fontAndSize = new Object [3 ];
2483
2499
PdfDictionary normalResources = null ;
2484
2500
PdfDictionary defaultResources = null ;
2485
2501
PdfDocument document = getDocument ();
@@ -2494,6 +2510,7 @@ protected Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
2494
2510
if ((normalFontDic != null || defaultFontDic != null ) && defaultAppearance != null ) {
2495
2511
Object [] dab = splitDAelements (defaultAppearance .toUnicodeString ());
2496
2512
PdfName fontName = new PdfName (dab [DA_FONT ].toString ());
2513
+ fontAndSize [2 ] = fontName ;
2497
2514
PdfDictionary requiredFontDictionary = null ;
2498
2515
if (normalFontDic != null && null != normalFontDic .getAsDictionary (fontName )) {
2499
2516
requiredFontDictionary = normalFontDic .getAsDictionary (fontName );
@@ -3036,12 +3053,34 @@ protected void drawPdfA2CheckAppearance(float width, float height, String value,
3036
3053
* @param font a {@link PdfFont}
3037
3054
* @param fontSize the size of the font
3038
3055
* @return a new {@link PdfFormXObject}
3056
+ * @deprecated Will be removed in 7.2.
3057
+ * @see #drawPushButtonAppearance(float, float, String, PdfFont, PdfName, float)
3039
3058
*/
3040
- protected PdfFormXObject drawPushButtonAppearance (float width , float height , String text , PdfFont font , float fontSize ) {
3041
- PdfStream stream = new PdfStream ().makeIndirect (getDocument ());
3042
- PdfCanvas canvas = new PdfCanvas (stream , new PdfResources (), getDocument ());
3059
+ @ Deprecated
3060
+ protected PdfFormXObject drawPushButtonAppearance (float width , float height , String text ,
3061
+ PdfFont font , float fontSize ) {
3062
+ return drawPushButtonAppearance (width , height , text , font , null , fontSize );
3043
3063
3044
- PdfFormXObject xObject = new PdfFormXObject (new Rectangle (0 , 0 , width , height ));
3064
+ }
3065
+
3066
+ /**
3067
+ * Draws the appearance for a push button.
3068
+ *
3069
+ * @param width the width of the pushbutton
3070
+ * @param height the width of the pushbutton
3071
+ * @param text the text to display on the button
3072
+ * @param font a {@link PdfFont}
3073
+ * @param fontName fontName in DR.
3074
+ * @param fontSize the size of the font
3075
+ * @return a new {@link PdfFormXObject}
3076
+ */
3077
+ protected PdfFormXObject drawPushButtonAppearance (float width , float height , String text ,
3078
+ PdfFont font , PdfName fontName , float fontSize ) {
3079
+ PdfStream stream = (PdfStream ) new PdfStream ().makeIndirect (getDocument ());
3080
+ AppearanceResources resources = new AppearanceResources ().addFontFromDefaultResources (fontName , font );
3081
+ PdfCanvas canvas = new PdfCanvas (stream , resources , getDocument ());
3082
+
3083
+ AppearanceXObject xObject = new AppearanceXObject (new Rectangle (0 , 0 , width , height ));
3045
3084
if (backgroundColor == null ) {
3046
3085
backgroundColor = ColorConstants .LIGHT_GRAY ;
3047
3086
}
@@ -3056,7 +3095,8 @@ protected PdfFormXObject drawPushButtonAppearance(float width, float height, Str
3056
3095
xObject .getResources ().addForm (form );
3057
3096
} else {
3058
3097
drawButton (canvas , 0 , 0 , width , height , text , font , fontSize );
3059
- setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , new PdfResources ()));
3098
+ xObject .addFontFromDR (fontName , font );
3099
+ setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , resources ));
3060
3100
xObject .getResources ().addFont (getDocument (), font );
3061
3101
}
3062
3102
xObject .getPdfObject ().getOutputStream ().writeBytes (stream .getBytes ());
0 commit comments