@@ -876,7 +876,7 @@ public static PdfButtonFormField createPushButton(PdfDocument doc, Rectangle rec
876
876
field .fontSize = fontSize ;
877
877
field .backgroundColor = ColorConstants .LIGHT_GRAY ;
878
878
879
- PdfFormXObject xObject = field .drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), caption , font , fontSize );
879
+ PdfFormXObject xObject = field .drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), caption , font , null , fontSize );
880
880
annot .setNormalAppearance (xObject .getPdfObject ());
881
881
882
882
PdfDictionary mk = new PdfDictionary ();
@@ -1962,6 +1962,7 @@ public boolean regenerateField() {
1962
1962
1963
1963
Object [] fontAndSize = getFontAndSize (asNormal );
1964
1964
PdfFont localFont = (PdfFont ) fontAndSize [0 ];
1965
+ PdfName localFontName = (PdfName ) fontAndSize [2 ];
1965
1966
float fontSize = normalizeFontSize ((float ) fontAndSize [1 ], localFont , bBox , value );
1966
1967
1967
1968
//Apply Page rotation
@@ -2043,14 +2044,16 @@ public boolean regenerateField() {
2043
2044
bBox = new PdfArray (rect );
2044
2045
}
2045
2046
//Create appearance
2046
- PdfFormXObject appearance = null ;
2047
+ AppearanceXObject appearance = null ;
2047
2048
if (asNormal != null ) {
2048
- appearance = new PdfFormXObject (asNormal );
2049
+ appearance = new AppearanceXObject (asNormal );
2050
+
2049
2051
appearance .setBBox (new PdfArray (new float []{0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()}));
2050
2052
}
2051
2053
if (appearance == null ) {
2052
- appearance = new PdfFormXObject (new Rectangle (0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()));
2054
+ appearance = new AppearanceXObject (new Rectangle (0 , 0 , bBox .toRectangle ().getWidth (), bBox .toRectangle ().getHeight ()));
2053
2055
}
2056
+ appearance .addFontFromDR (localFontName , localFont );
2054
2057
appearance .put (PdfName .Matrix , matrix );
2055
2058
//Create text appearance
2056
2059
if (PdfName .Tx .equals (type )) {
@@ -2099,20 +2102,19 @@ public boolean regenerateField() {
2099
2102
apDic = widgets .get (0 ).getPdfObject ().getAsDictionary (PdfName .AP );
2100
2103
}
2101
2104
}
2102
- if (img != null ) {
2103
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , 0 );
2104
- } else if (form != null ) {
2105
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , 0 );
2105
+ if (img != null || form != null ) {
2106
+ appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , null , null , 0 );
2106
2107
} else {
2107
2108
PdfStream asNormal = null ;
2108
2109
if (apDic != null ) {
2109
2110
asNormal = apDic .getAsStream (PdfName .N );
2110
2111
}
2111
2112
Object [] fontAndSize = getFontAndSize (asNormal );
2112
2113
PdfFont localFont = (PdfFont ) fontAndSize [0 ];
2114
+ PdfName localFontName = (PdfName ) fontAndSize [2 ];
2113
2115
float fontSize = (float ) fontAndSize [1 ];
2114
- appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value , localFont , fontSize );
2115
- appearance . getResources (). addFont ( getDocument (), localFont );
2116
+ appearance = drawPushButtonAppearance (rect .getWidth (), rect .getHeight (), value ,
2117
+ localFont , localFontName , fontSize );
2116
2118
}
2117
2119
2118
2120
if (apDic == null ) {
@@ -2632,7 +2634,7 @@ protected String generateDefaultAppearanceString(PdfFont font, float fontSize, C
2632
2634
}
2633
2635
2634
2636
protected Object [] getFontAndSize (PdfDictionary asNormal ) throws IOException {
2635
- Object [] fontAndSize = new Object [2 ];
2637
+ Object [] fontAndSize = new Object [3 ];
2636
2638
PdfDictionary normalResources = null ;
2637
2639
PdfDictionary defaultResources = null ;
2638
2640
PdfDocument document = getDocument ();
@@ -2648,6 +2650,7 @@ protected Object[] getFontAndSize(PdfDictionary asNormal) throws IOException {
2648
2650
if ((normalFontDic != null || defaultFontDic != null ) && defaultAppearance != null ) {
2649
2651
Object [] dab = splitDAelements (defaultAppearance .toUnicodeString ());
2650
2652
PdfName fontName = new PdfName (dab [DA_FONT ].toString ());
2653
+ fontAndSize [2 ] = fontName ;
2651
2654
PdfDictionary requiredFontDictionary = null ;
2652
2655
if (normalFontDic != null && null != normalFontDic .getAsDictionary (fontName )) {
2653
2656
requiredFontDictionary = normalFontDic .getAsDictionary (fontName );
@@ -2759,7 +2762,10 @@ protected void drawTextAppearance(Rectangle rect, PdfFont font, float fontSize,
2759
2762
PdfResources resources = appearance .getResources ();
2760
2763
PdfCanvas canvas = new PdfCanvas (stream , resources , getDocument ());
2761
2764
2762
- setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , resources ));
2765
+ //TODO seems that FontName shall be passed
2766
+ if (getDefaultAppearance () == null ) {
2767
+ setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , resources ));
2768
+ }
2763
2769
2764
2770
float height = rect .getHeight ();
2765
2771
float width = rect .getWidth ();
@@ -3157,11 +3163,12 @@ protected void drawPdfA2CheckAppearance(float width, float height, String value,
3157
3163
* @param fontSize the size of the font
3158
3164
* @return a new {@link PdfFormXObject}
3159
3165
*/
3160
- protected PdfFormXObject drawPushButtonAppearance (float width , float height , String text , PdfFont font , float fontSize ) {
3166
+ protected PdfFormXObject drawPushButtonAppearance (float width , float height , String text ,
3167
+ PdfFont font , PdfName fontName , float fontSize ) {
3161
3168
PdfStream stream = (PdfStream ) new PdfStream ().makeIndirect (getDocument ());
3162
3169
PdfCanvas canvas = new PdfCanvas (stream , new PdfResources (), getDocument ());
3163
3170
3164
- PdfFormXObject xObject = new PdfFormXObject (new Rectangle (0 , 0 , width , height ));
3171
+ AppearanceXObject xObject = new AppearanceXObject (new Rectangle (0 , 0 , width , height ));
3165
3172
drawBorder (canvas , xObject , width , height );
3166
3173
3167
3174
if (img != null ) {
@@ -3173,8 +3180,11 @@ protected PdfFormXObject drawPushButtonAppearance(float width, float height, Str
3173
3180
xObject .getResources ().addForm (form );
3174
3181
} else {
3175
3182
drawButton (canvas , 0 , 0 , width , height , text , font , fontSize );
3176
- setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , new PdfResources ()));
3177
- xObject .getResources ().addFont (getDocument (), font );
3183
+ xObject .addFontFromDR (fontName , font );
3184
+ if (getDefaultAppearance () == null ) {
3185
+ setDefaultAppearance (generateDefaultAppearanceString (font , fontSize , color , new PdfResources ()));
3186
+ xObject .getResources ().addFont (getDocument (), font );
3187
+ }
3178
3188
}
3179
3189
xObject .getPdfObject ().getOutputStream ().writeBytes (stream .getBytes ());
3180
3190
@@ -3223,6 +3233,7 @@ protected void drawCheckBox(PdfCanvas canvas, float width, float height, float f
3223
3233
DrawingUtil .drawCross (canvas , width , height , borderWidth );
3224
3234
return ;
3225
3235
}
3236
+ //TODO what is current font actually?
3226
3237
PdfFont ufont = getFont ();
3227
3238
if (fontSize <= 0 ) {
3228
3239
fontSize = approximateFontSizeToFitBBox (ufont , new Rectangle (width , height ), text );
0 commit comments