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/05/23 15:40:27 UTC

svn commit: r1745220 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers: PDAppearanceHandler.java PDSquareAppearanceHandler.java

Author: msahyoun
Date: Mon May 23 15:40:27 2016
New Revision: 1745220

URL: http://svn.apache.org/viewvc?rev=1745220&view=rev
Log:
PDFBOX-3353: support setting the fill color (interior color IC).

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAppearanceHandler.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDSquareAppearanceHandler.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAppearanceHandler.java?rev=1745220&r1=1745219&r2=1745220&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAppearanceHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDAppearanceHandler.java Mon May 23 15:40:27 2016
@@ -79,12 +79,12 @@ public abstract class PDAppearanceHandle
     // If at the end all annotations support the BS entry this can be handled
     // here and removed from the individual handlers.
     abstract float getLineWidth();
-    
+
     PDColor getColor()
     {
         return annotation.getColor();
     }
-    
+
     PDRectangle getRectangle()
     {
         return annotation.getRectangle();
@@ -93,8 +93,9 @@ public abstract class PDAppearanceHandle
     /**
      * Get the annotations appearance dictionary.
      * 
-     * <p>This will get the annotations appearance dictionary. If this is
-     * not existent an empty appearance dictionary will be created.
+     * <p>
+     * This will get the annotations appearance dictionary. If this is not
+     * existent an empty appearance dictionary will be created.
      * 
      * @return the annotations appearance dictionary
      */
@@ -112,8 +113,9 @@ public abstract class PDAppearanceHandle
     /**
      * Get the annotations normal appearance.
      * 
-     * <p>This will get the annotations normal appearance. If this is
-     * not existent an empty appearance entry will be created.
+     * <p>
+     * This will get the annotations normal appearance. If this is not existent
+     * an empty appearance entry will be created.
      * 
      * @return the appearance entry representing the normal appearance.
      */
@@ -124,18 +126,19 @@ public abstract class PDAppearanceHandle
 
         if (appearanceEntry.isSubDictionary())
         {
-           appearanceEntry = new PDAppearanceEntry(new COSStream());
-           appearanceDictionary.setNormalAppearance(appearanceEntry);
+            appearanceEntry = new PDAppearanceEntry(new COSStream());
+            appearanceDictionary.setNormalAppearance(appearanceEntry);
         }
 
         return appearanceEntry;
     }
-    
+
     /**
      * Get the annotations down appearance.
      * 
-     * <p>This will get the annotations down appearance. If this is
-     * not existent an empty appearance entry will be created.
+     * <p>
+     * This will get the annotations down appearance. If this is not existent an
+     * empty appearance entry will be created.
      * 
      * @return the appearance entry representing the down appearance.
      */
@@ -143,7 +146,7 @@ public abstract class PDAppearanceHandle
     {
         PDAppearanceDictionary appearanceDictionary = getAppearance();
         PDAppearanceEntry appearanceEntry = appearanceDictionary.getDownAppearance();
-        
+
         if (appearanceEntry.isSubDictionary())
         {
             appearanceEntry = new PDAppearanceEntry(new COSStream());
@@ -156,8 +159,9 @@ public abstract class PDAppearanceHandle
     /**
      * Get the annotations rollover appearance.
      * 
-     * <p>This will get the annotations rollover appearance. If this is
-     * not existent an empty appearance entry will be created.
+     * <p>
+     * This will get the annotations rollover appearance. If this is not
+     * existent an empty appearance entry will be created.
      * 
      * @return the appearance entry representing the rollover appearance.
      */
@@ -174,4 +178,19 @@ public abstract class PDAppearanceHandle
 
         return appearanceEntry;
     }
+
+    /**
+     * Get a padded rectangle.
+     * 
+     * <p>Creates a new rectangle with padding applied to each side.
+     * .
+     * @param rectangle the rectangle.
+     * @param padding the padding to apply.
+     * @return the padded rectangle.
+     */
+    PDRectangle getPaddedRectangle(PDRectangle rectangle, float padding)
+    {
+        return new PDRectangle(rectangle.getLowerLeftX() + padding, rectangle.getLowerLeftY() + padding,
+                rectangle.getWidth() - 2 * padding, rectangle.getHeight() - 2 * padding);
+    }
 }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDSquareAppearanceHandler.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDSquareAppearanceHandler.java?rev=1745220&r1=1745219&r2=1745220&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDSquareAppearanceHandler.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/annotation/handlers/PDSquareAppearanceHandler.java Mon May 23 15:40:27 2016
@@ -17,7 +17,6 @@
 
 package org.apache.pdfbox.pdmodel.interactive.annotation.handlers;
 
-
 import java.awt.geom.AffineTransform;
 import java.io.IOException;
 
@@ -52,20 +51,29 @@ public class PDSquareAppearanceHandler e
             PDAppearanceContentStream contentStream = new PDAppearanceContentStream(appearanceStream);
             PDRectangle bbox = getRectangle();
             appearanceStream.setBBox(bbox);
-            AffineTransform transform = AffineTransform.getTranslateInstance(-bbox.getLowerLeftX(), -bbox.getLowerLeftY());
+            AffineTransform transform = AffineTransform.getTranslateInstance(-bbox.getLowerLeftX(),
+                    -bbox.getLowerLeftY());
             appearanceStream.setMatrix(transform);
-            contentStream.setStrokingColor(getColor().getComponents());
+
+            contentStream.setStrokingColorOnDemand(getColor());
+            boolean hasBackground = contentStream
+                    .setNonStrokingColorOnDemand(((PDAnnotationSquareCircle) getAnnotation()).getInteriorColor());
+
+            contentStream.setLineWidthOnDemand(lineWidth);
             
-            // Acrobat doesn't write a line width command
-            // for a line width of 1 as this is default.
-            // Will do the same.
-            if (!(Math.abs(lineWidth - 1) < 1e-6))
+            // Acrobat applies a padding to each side of the bbox so the line is completely within
+            // the bbox.
+            PDRectangle borderEdge = getPaddedRectangle(bbox,lineWidth);
+            contentStream.addRect(borderEdge.getLowerLeftX() , borderEdge.getLowerLeftY(),
+                    borderEdge.getWidth(), borderEdge.getHeight());
+
+            if (!hasBackground)
+            {
+                contentStream.stroke();
+            } else
             {
-                contentStream.setLineWidth(lineWidth);
+                contentStream.fillAndStroke();
             }
-            contentStream.addRect(bbox.getLowerLeftX()+lineWidth, bbox.getLowerLeftY()+lineWidth, bbox.getWidth()-2*lineWidth, bbox.getHeight()-2*lineWidth);
-            
-            contentStream.stroke();
             contentStream.close();
         } catch (IOException e)
         {
@@ -84,32 +92,32 @@ public class PDSquareAppearanceHandler e
     {
         // TODO to be implemented
     }
-    
+
     /**
      * Get the line with of the border.
      * 
-     * Get the width of the line used to draw a border around
-     * the annotation. This may either be specified by the annotation
-     * dictionaries Border setting or by the W entry in the BS border
-     * style dictionary. If both are missing the default width is 1.
+     * Get the width of the line used to draw a border around the annotation.
+     * This may either be specified by the annotation dictionaries Border
+     * setting or by the W entry in the BS border style dictionary. If both are
+     * missing the default width is 1.
      * 
      * @return the line width
      */
-    // TODO: according to the PDF spec the use of the BS entry is annotation specific
-    //       so we will leave that to be implemented by individual handlers.
-    //       If at the end all annotations support the BS entry this can be handled
-    //       here and removed from the individual handlers.
+    // TODO: according to the PDF spec the use of the BS entry is annotation
+    // specific
+    // so we will leave that to be implemented by individual handlers.
+    // If at the end all annotations support the BS entry this can be handled
+    // here and removed from the individual handlers.
     public float getLineWidth()
     {
         PDAnnotationSquareCircle annotation = (PDAnnotationSquareCircle) getAnnotation();
-        
+
         PDBorderStyleDictionary bs = annotation.getBorderStyle();
-        
+
         if (bs != null)
         {
             return bs.getWidth();
-        }
-        else
+        } else
         {
             COSArray borderCharacteristics = annotation.getBorder();
             if (borderCharacteristics != null && borderCharacteristics.size() >= 3)
@@ -117,7 +125,7 @@ public class PDSquareAppearanceHandler e
                 return borderCharacteristics.getInt(3);
             }
         }
-        
+
         return 1;
     }
 }