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 2014/12/16 22:24:49 UTC

svn commit: r1646086 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form: PDFieldTreeNode.java PDVariableText.java

Author: msahyoun
Date: Tue Dec 16 21:24:49 2014
New Revision: 1646086

URL: http://svn.apache.org/r1646086
Log:
PDFBOX-2516 support get/set rich text attributes DS, RV

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java?rev=1646086&r1=1646085&r2=1646086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDFieldTreeNode.java Tue Dec 16 21:24:49 2014
@@ -92,6 +92,18 @@ public abstract class PDFieldTreeNode im
         dictionary = field;
         parent = parentNode;
     }
+
+    /**
+     * Returns the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param key the key to look up
+     * @return COS value for the given key
+     */
+    public COSBase getInheritableAttribute(COSName key)
+    {
+        return getInheritableAttribute(getDictionary(), key);
+    }    
+    
     
     /**
      * Returns the given attribute, inheriting from parent nodes if necessary.
@@ -102,7 +114,8 @@ public abstract class PDFieldTreeNode im
      */
     public COSBase getInheritableAttribute(COSDictionary fieldDictionary, COSName key)
     {
-        COSBase value = fieldDictionary.getCOSName(key);
+        COSBase value = fieldDictionary.getDictionaryObject(key);
+        
         if (value != null)
         {
             return value;
@@ -117,6 +130,16 @@ public abstract class PDFieldTreeNode im
         return null;
     }
     
+    /**
+     * Sets the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param key the key to look up
+     * @param value the new attributes value
+     */
+    public void setInheritableAttribute(COSName key, COSBase value)
+    {
+        setInheritableAttribute(getDictionary(), key, value);
+    }  
     
     /**
      * Sets the given attribute, inheriting from parent nodes if necessary.
@@ -140,6 +163,16 @@ public abstract class PDFieldTreeNode im
             }
         }
     }
+
+    /**
+     * Removes the given attribute, inheriting from parent nodes if necessary.
+     *
+     * @param key the key to look up
+     */
+    public void removeInheritableAttribute(COSName key)
+    {
+        removeInheritableAttribute(getDictionary(), key);
+    }      
     
     /**
      * Removes the given attribute, inheriting from parent nodes if necessary.

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java?rev=1646086&r1=1646085&r2=1646086&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDVariableText.java Tue Dec 16 21:24:49 2014
@@ -222,7 +222,7 @@ public abstract class PDVariableText ext
      */
     public COSString getDefaultAppearance()
     {
-        COSBase daValue = getInheritableAttribute(getDictionary(),COSName.DA);
+        COSBase daValue = getInheritableAttribute(COSName.DA);
         defaultAppearance = (COSString) daValue;
         return defaultAppearance;
     }
@@ -250,9 +250,45 @@ public abstract class PDVariableText ext
             getDictionary().removeItem(COSName.DA);
         }
     }
+    
+    /**
+     * Get the default style string.
+     * 
+     * The default style string defines the default style for
+     * rich text fields.
+     * 
+     * @return the DD element of the dictionary object
+     */
+    public COSString getDefaultStyleString()
+    {
+        COSBase defaultStyleString = getDictionary().getDictionaryObject(COSName.DS);
+        return (COSString) defaultStyleString;
+    }
+
+    /**
+     * Set the default style string.
+     * 
+     * Providing null as the value will remove the default style string.
+     * 
+     * @param defaultStyleString a string describing the default style.
+     */
+    public void setDefaultStyleString(String defaultStyleString)
+    {
+        if (defaultStyleString != null)
+        {
+            getDictionary().setItem(COSName.DS, new COSString(defaultStyleString));
+        }
+        else
+        {
+            getDictionary().removeItem(COSName.DS);
+        }
+    }    
 
     /**
      * This will get the 'quadding' or justification of the text to be displayed.
+     * 
+     * This is an inheritable attribute.
+     * 
      * 0 - Left(default)<br/>
      * 1 - Centered<br />
      * 2 - Right<br />
@@ -263,17 +299,13 @@ public abstract class PDVariableText ext
     public int getQ()
     {
         int retval = 0;
-        COSNumber number = (COSNumber)getDictionary().getDictionaryObject( COSName.Q );
+
+        COSNumber number = (COSNumber)getInheritableAttribute(COSName.Q );
+        
         if( number != null )
         {
             retval = number.intValue();
         }
-        else
-        {
-            // the Q value is inheritable
-            // the acroform should provide a Q default value
-            retval = getAcroForm().getQ();
-        }
         return retval;
     }
 
@@ -290,13 +322,60 @@ public abstract class PDVariableText ext
     @Override
     public Object getDefaultValue()
     {
-        return getDictionary().getDictionaryObject(COSName.DV);
+        return getInheritableAttribute(COSName.DV);
     }
 
+    /**
+     * Set the fields default value.
+     *
+     * @param value the fields value
+     */
     public void setDefaultValue(Object value)
     {
         // Text fields don't support the "DV" entry.
         throw new RuntimeException( "Text fields don't support the \"DV\" entry." );
     }
-
+    
+    /**
+     * Get the fields rich text value.
+     * 
+     * @return the rich text value string
+     */
+    public String getRichTextValue()
+    {
+        COSBase richTextValue = getDictionary().getDictionaryObject(COSName.RV);
+        
+        if (richTextValue instanceof COSString)
+        {
+            return ((COSString) richTextValue).getString();
+        }
+        // TODO stream instead of string
+        return "";
+    }
+    
+    /**
+     * Set the fields rich text value.
+     * 
+     * Setting the rich text value will not generate the appearance
+     * for the field.
+     * 
+     * You can set {@link PDAcroForm#setNeedAppearances(Boolean)} to
+     * signal a conforming reader to generate the appearance stream.
+     * 
+     * Providing null as the value will remove the default style string.
+     * 
+     * @param richTextValue a rich text string
+     */
+    public void setRichTextValue(String richTextValue)
+    {
+        // TODO stream instead of string
+        if (richTextValue != null)
+        {
+            getDictionary().setItem(COSName.RV, new COSString(richTextValue));
+        }
+        else
+        {
+            getDictionary().removeItem(COSName.RV);
+        }        
+    }
 }