pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1773458 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Date Fri, 09 Dec 2016 19:53:35 GMT
Author: msahyoun
Date: Fri Dec  9 19:53:35 2016
New Revision: 1773458

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

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

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1773458&r1=1773457&r2=1773458&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Fri Dec  9 19:53:35 2016
@@ -38,6 +38,8 @@ 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.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 +267,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 +696,31 @@ 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;
+    }
 }



Mime
View raw message