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 2015/09/06 21:17:44 UTC

svn commit: r1701517 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java

Author: msahyoun
Date: Sun Sep  6 19:17:44 2015
New Revision: 1701517

URL: http://svn.apache.org/r1701517
Log:
PDFBOX-2961: add method to get multiple widgets

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java?rev=1701517&r1=1701516&r2=1701517&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDField.java Sun Sep  6 19:17:44 2015
@@ -416,9 +416,10 @@ public abstract class PDField implements
     }
 
     /**
-     * This will get the single associated widget that is part of this field. This occurs when the Widget is embedded in
-     * the fields dictionary. Sometimes there are multiple sub widgets associated with this field, in which case you
-     * want to use getKids(). If the kids entry is specified, then the first entry in that list will be returned.
+     * This will get the single associated widget that is part of this field. This occurs when the Widget is
+     * embedded in the fields dictionary. Sometimes there are multiple sub widgets associated with this field,
+     * in which case you want to use {@link #getWidgets()}. If the kids entry is specified, then only first
+     * entry in that list will be returned.
      * 
      * @return The widget that is associated with this field.
      * @throws IOException If there is an error getting the widget object.
@@ -451,6 +452,50 @@ public abstract class PDField implements
     }
 
     /**
+     * Returns the widget annotations associated with this field.
+     * 
+     * The widget annotations are returned for terminal fields (fields which
+     * do not have other fields as kids). In case of non terminal fields an 
+     * empty List is returned. 
+     * 
+     * @return The list of widget annotations.
+     * @throws IOException 
+     */
+    public List<PDAnnotationWidget> getWidgets() throws IOException
+    {
+        List<PDAnnotationWidget> widgets = new ArrayList<PDAnnotationWidget>();
+        List<COSObjectable> kids = getKids();
+        if (kids == null)
+        {
+            // the field itself is a widget
+            widgets.add(new PDAnnotationWidget(getDictionary()));
+        }
+        else if (kids.size() > 0)
+        {
+            Object firstKid = kids.get(0);
+
+            /*
+             * If this happens the current field is not a terminal field.
+             * Return an empty list as there are no widgets associated to non
+             * terminal fields.
+             */
+            if (firstKid instanceof PDField)
+            {
+                return widgets;
+            }
+            else
+            {
+                // there are multiple widgets
+                for (COSObjectable kid : kids)
+                {
+                    widgets.add((PDAnnotationWidget) kid);
+                }
+            }
+        }
+        return widgets;
+    }
+    
+    /**
      * Get the parent field to this field, or null if none exists.
      * 
      * @return The parent field.