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/19 22:00:03 UTC

svn commit: r1646865 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/ test/java/org/apache/pdfbox/pdmodel/interactive/form/

Author: msahyoun
Date: Fri Dec 19 21:00:03 2014
New Revision: 1646865

URL: http://svn.apache.org/r1646865
Log:
PDFBOX-2516 handle text streams for field value

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/PDTextField.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.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=1646865&r1=1646864&r2=1646865&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 Fri Dec 19 21:00:03 2014
@@ -99,7 +99,7 @@ public abstract class PDFieldTreeNode im
      * @param key the key to look up
      * @return COS value for the given key
      */
-    public COSBase getInheritableAttribute(COSName key)
+    protected COSBase getInheritableAttribute(COSName key)
     {
         return getInheritableAttribute(getDictionary(), key);
     }    
@@ -112,7 +112,7 @@ public abstract class PDFieldTreeNode im
      * @param key the key to look up
      * @return COS value for the given key
      */
-    public COSBase getInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    protected COSBase getInheritableAttribute(COSDictionary fieldDictionary, COSName key)
     {
         COSBase value = fieldDictionary.getDictionaryObject(key);
         
@@ -136,7 +136,7 @@ public abstract class PDFieldTreeNode im
      * @param key the key to look up
      * @param value the new attributes value
      */
-    public void setInheritableAttribute(COSName key, COSBase value)
+    protected void setInheritableAttribute(COSName key, COSBase value)
     {
         setInheritableAttribute(getDictionary(), key, value);
     }  
@@ -148,7 +148,7 @@ public abstract class PDFieldTreeNode im
      * @param key the key to look up
      * @param value the new attributes value
      */
-    public void setInheritableAttribute(COSDictionary fieldDictionary, COSName key, COSBase value)
+    protected void setInheritableAttribute(COSDictionary fieldDictionary, COSName key, COSBase value)
     {
         if (fieldDictionary.getItem(key) != null)
         {
@@ -169,7 +169,7 @@ public abstract class PDFieldTreeNode im
      *
      * @param key the key to look up
      */
-    public void removeInheritableAttribute(COSName key)
+    protected void removeInheritableAttribute(COSName key)
     {
         removeInheritableAttribute(getDictionary(), key);
     }      
@@ -180,7 +180,7 @@ public abstract class PDFieldTreeNode im
      * @param fieldDictionary field object
      * @param key the key to look up
      */
-    public void removeInheritableAttribute(COSDictionary fieldDictionary, COSName key)
+    protected void removeInheritableAttribute(COSDictionary fieldDictionary, COSName key)
     {
         if (fieldDictionary.getItem(key) != null)
         {
@@ -196,6 +196,35 @@ public abstract class PDFieldTreeNode im
         }
     }
     
+    /**
+     * Get a text as text stream.
+     * 
+     * Some dictionary entries allow either a text or a text stream.
+     * 
+     * @param cosBaseEntry the potential text or text stream
+     * @return the text stream
+     * @throws IOException if the field dictionary entry is not a text type
+     */
+    protected PDTextStream getAsTextStream(COSBase cosBaseEntry) throws IOException
+    {
+        if (cosBaseEntry == null)
+        {
+            return null;
+        }
+        else
+        {
+            PDTextStream textStream = PDTextStream.createTextStream(cosBaseEntry);
+            // This will happen if the entry was not a COSString or COSStream
+            if (textStream == null)
+            {
+                throw new IOException("Invalid field value. Unexpected type " + cosBaseEntry.getClass().getName());
+            }
+            else
+            {
+                return textStream;
+            }
+        }
+    }
     
     
     /**

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java?rev=1646865&r1=1646864&r2=1646865&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDTextField.java Fri Dec 19 21:00:03 2014
@@ -16,10 +16,14 @@
  */
 package org.apache.pdfbox.pdmodel.interactive.form;
 
+import java.io.IOException;
+
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.pdmodel.common.PDTextStream;
 
 /**
  * A text field is a box or space for text fill-in data typically entered from a keyboard.
@@ -138,17 +142,18 @@ public final class PDTextField extends P
      * getValue gets the value of the "V" entry.
      * 
      * @return The value of this entry.
+     * @throws IOException 
      * 
      */
     @Override
-    public String getValue()
+    public String getValue() throws IOException
     {
-        COSBase fieldValue = getInheritableAttribute(getDictionary(), COSName.V);
-        if (fieldValue instanceof COSString)
+        PDTextStream textStream = getAsTextStream(getInheritableAttribute(getDictionary(), COSName.V));
+
+        if (textStream != null) 
         {
-            return ((COSString) fieldValue).getString();
+            return textStream.getAsString();
         }
-        // TODO handle PDTextStream, IOException in case of wrong type
         return null;
     }
 }

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java?rev=1646865&r1=1646864&r2=1646865&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestFields.java Fri Dec 19 21:00:03 2014
@@ -177,6 +177,13 @@ public class TestFields extends TestCase
             // do not test for the full content as this is a rather long xml string
             assertEquals(((PDVariableText)field).getRichTextValue().length(),338);
             
+            // get a rich text field with a text stream for the value
+            field = form.getField("LongRichTextField");
+            assertNotNull(field);
+            assertEquals(field.getDictionary().getDictionaryObject(
+                    COSName.V).getClass().getName(),
+                    "org.apache.pdfbox.cos.COSStream");
+            assertEquals(((PDTextField)field).getValue().length(),145396);
             
         }
         finally