You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2016/12/09 19:28:35 UTC

svn commit: r1773457 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java

Author: msahyoun
Date: Fri Dec  9 19:28:34 2016
New Revision: 1773457

URL: http://svn.apache.org/viewvc?rev=1773457&view=rev
Log:
PDFBOX-3396, PDFBOX-3587: always translate the appearance, apply scaling if the appearance is using an XForm 

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1773457&r1=1773456&r2=1773457&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java Fri Dec  9 19:28:34 2016
@@ -38,6 +38,7 @@ import org.apache.pdfbox.pdmodel.PDPageC
 import org.apache.pdfbox.pdmodel.PDResources;
 import org.apache.pdfbox.pdmodel.common.COSArrayList;
 import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.fdf.FDFCatalog;
 import org.apache.pdfbox.pdmodel.fdf.FDFDictionary;
 import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
@@ -265,12 +266,40 @@ public final class PDAcroForm implements
                     
                     // translate the appearance stream to the widget location if there is 
                     // not already a transformation in place
-                    boolean needsTransformation = isNeedsTransformation(appearanceStream);
-                    if (needsTransformation)
+                    boolean needsTranslation = resolveNeedsTranslation(appearanceStream);
+                    
+                    // scale the appearance stream - mainly needed for images
+                    // in buttons and signatures
+                    boolean needsScaling = resolveNeedsScaling(appearanceStream);
+                    
+                    Matrix transformationMatrix = new Matrix();
+                    boolean transformed = false;
+                    
+                    if (needsTranslation)
                     {
-                        Matrix translationMatrix = Matrix.getTranslateInstance(widget.getRectangle().getLowerLeftX(),
+                    	transformationMatrix.translate(widget.getRectangle().getLowerLeftX(),
                                 widget.getRectangle().getLowerLeftY());
-                        contentStream.transform(translationMatrix);
+                    	transformed = true;
+                    }
+
+                    if (needsScaling)
+                    {                    
+	                    PDRectangle bbox = appearanceStream.getBBox();
+	                    PDRectangle fieldRect = widget.getRectangle();
+	                    
+	                    if (bbox.getWidth() - fieldRect.getWidth() != 0 && bbox.getHeight() - fieldRect.getHeight() != 0)
+	                    {
+	                    	float xScale = fieldRect.getWidth() / bbox.getWidth();
+	                    	float yScale = fieldRect.getHeight() / bbox.getHeight();
+	                    	Matrix scalingMatrix = Matrix.getScaleInstance(xScale, yScale);
+	                    	transformationMatrix.concatenate(scalingMatrix);
+	                    	transformed = true;
+	                    }
+                    }
+
+                    if (transformed)
+                    {
+                    	contentStream.transform(transformationMatrix);
                     }
                     
                     contentStream.drawForm(fieldObject);
@@ -666,20 +695,34 @@ public final class PDAcroForm implements
     }
     
     /**
-     * Check if there is a transformation needed to place the annotations content.
+     * Check if there is a translation needed to place the annotations content.
      * 
      * @param appearanceStream
-     * @return the need for a transformation.
+     * @return the need for a translation transformation.
      */
-    private boolean isNeedsTransformation(PDAppearanceStream appearanceStream)
+    private boolean resolveNeedsTranslation(PDAppearanceStream appearanceStream)
     {
-        // Check if there is a XObject defined as this is an indication that there should already be a transformation
-        // in place.
-        // TODO: A more reliable approach might be to parse the content stream
-        if (appearanceStream.getResources() != null && appearanceStream.getResources().getXObjectNames().iterator().hasNext())
-        {
-            return false;
-        }
+        // TODO: implement special cases for files in PDFBOX-3396
         return true;
     }
+    
+    /**
+     * Check if there needs to be a scaling transformation applied.
+     * 
+     * @param appearanceStream
+     * @return the need for a scaling transformation.
+     */    
+    private boolean resolveNeedsScaling(PDAppearanceStream appearanceStream)
+    {
+        // Check if there is a transformation within the XObjects content
+    	PDResources resources = appearanceStream.getResources();
+        if (resources != null && resources.getXObjectNames().iterator().hasNext())
+        {        	
+            return true;
+        }
+        return false;
+    }
+    
+    
+    
 }