Author: tilman
Date: Mon Jul 27 09:07:55 2026
New Revision: 1936619

Log:
PDFBOX-6223: more refactoring to avoid calculating things twice

Modified:
   
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java

Modified: 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
==============================================================================
--- 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
  Mon Jul 27 08:58:48 2026        (r1936618)
+++ 
pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/AppearanceGeneratorHelper.java
  Mon Jul 27 09:07:55 2026        (r1936619)
@@ -227,16 +227,22 @@ class AppearanceGeneratorHelper {
             PDAppearanceEntry appearance = 
appearanceDict.getNormalAppearance();
             // TODO support appearances other than "normal"
 
+            PDAppearanceCharacteristicsDictionary appearanceCharacteristics =
+                    widget.getAppearanceCharacteristics();
+            int widgetRotation = resolveRotation(appearanceCharacteristics);
+            PDRectangle newBBox = computeBBox(widget, widgetRotation);
             PDAppearanceStream appearanceStream;
-            if (isValidAppearanceStream(appearance)) {
+            // We're using the existing appearance if possible (since 2013 or 
even earlier)
+            // However, except for the file from PDFBOX-2586 we could ignore it
+            if (isValidAppearanceStream(appearance, widgetRotation, newBBox))
+            {
                 appearanceStream = appearance.getAppearanceStream();
-            } else {
-                appearanceStream = prepareNormalAppearanceStream(widget);
+            }
+            else
+            {
+                appearanceStream = prepareNormalAppearanceStream(widget, 
newBBox, widgetRotation);
                 appearanceDict.setNormalAppearance(appearanceStream);
-                // TODO support appearances other than "normal"
             }
-            PDAppearanceCharacteristicsDictionary appearanceCharacteristics =
-                    widget.getAppearanceCharacteristics();
 
             /*
              * Adobe Acrobat always recreates the complete appearance stream 
if there is an
@@ -281,7 +287,8 @@ class AppearanceGeneratorHelper {
         return apValue;
     }
 
-    private static boolean isValidAppearanceStream(PDAppearanceEntry 
appearance) {
+    private static boolean isValidAppearanceStream(PDAppearanceEntry 
appearance, int widgetRotation, PDRectangle newBBox)
+    {
         if (appearance == null) {
             return false;
         }
@@ -295,15 +302,12 @@ class AppearanceGeneratorHelper {
         return Math.abs(bbox.getWidth()) > 0 && Math.abs(bbox.getHeight()) > 0;
     }
 
-    private PDAppearanceStream 
prepareNormalAppearanceStream(PDAnnotationWidget widget) {
+    private PDAppearanceStream 
prepareNormalAppearanceStream(PDAnnotationWidget widget, PDRectangle bbox, int 
widgetRotation)
+    {
         PDAppearanceStream appearanceStream = new 
PDAppearanceStream(field.getAcroForm().getDocument());
 
-        // Calculate the entries for the bounding box and the transformation 
matrix
-        // settings for the appearance stream
-        PDRectangle bbox = computeBBox(widget);
         appearanceStream.setBBox(bbox);
-        int rotation = resolveRotation(widget);
-        AffineTransform at = calculateMatrix(bbox, rotation);
+        AffineTransform at = calculateMatrix(bbox, widgetRotation);
         if (!at.isIdentity()) {
             appearanceStream.setMatrix(at);
         }
@@ -312,10 +316,10 @@ class AppearanceGeneratorHelper {
         return appearanceStream;
     }
 
-    private static PDRectangle computeBBox(PDAnnotationWidget widget)
+    private static PDRectangle computeBBox(PDAnnotationWidget widget, int 
widgetRotation)
     {
         PDRectangle rect = widget.getRectangle();
-        Matrix matrix = 
Matrix.getRotateInstance(Math.toRadians(resolveRotation(widget)), 0, 0);
+        Matrix matrix = 
Matrix.getRotateInstance(Math.toRadians(widgetRotation), 0, 0);
         Point2D.Float point2D = matrix.transformPoint(rect.getWidth(), 
rect.getHeight());
         return new PDRectangle(Math.abs((float) point2D.getX()), 
Math.abs((float) point2D.getY()));
     }
@@ -326,10 +330,10 @@ class AppearanceGeneratorHelper {
         return new PDDefaultAppearanceString(da, dr);
     }
 
-    private static int resolveRotation(PDAnnotationWidget widget)
+    private static int resolveRotation(PDAppearanceCharacteristicsDictionary 
characteristicsDictionary)
     {
-        PDAppearanceCharacteristicsDictionary characteristicsDictionary = 
widget.getAppearanceCharacteristics();
-        if (characteristicsDictionary != null) {
+        if (characteristicsDictionary != null)
+        {
             // 0 is the default value if the R key doesn't exist
             return characteristicsDictionary.getRotation();
         }

Reply via email to