pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1688663 - in /pdfbox/trunk/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Date Wed, 01 Jul 2015 14:54:08 GMT
Author: msahyoun
Date: Wed Jul  1 14:54:08 2015
New Revision: 1688663

URL: http://svn.apache.org/r1688663
Log:
PDFBOX-2849: get potential On value

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java?rev=1688663&r1=1688662&r2=1688663&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDCheckbox.java
Wed Jul  1 14:54:08 2015
@@ -17,10 +17,17 @@
 package org.apache.pdfbox.pdmodel.interactive.form;
 
 import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
 
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceEntry;
 
 /**
  * A check box toggles between two states, on and off.
@@ -139,4 +146,71 @@ public final class PDCheckbox extends PD
         COSName name = value ? COSName.YES : COSName.OFF;
         dictionary.setItem(COSName.DV, name);
     }
+
+    /**
+     * Get the value which sets the check box to the On state.
+     * 
+     * <p>The On value should be 'Yes' but other values are possible
+     * so we need to look for that. On the other hand the Off value shall
+     * always be 'Off'. If not set or not part of the normal appearance keys
+     * 'Off' is the default</p>
+     *
+     * @returns the value setting the check box to the On state. 
+     *          If an empty string is returned there is no appearance definition.
+     * @throws IOException if the value could not be set
+     */
+    public String getOnValue()
+    {
+        PDAnnotationWidget widget = this.getWidgets().get(0);
+        PDAppearanceDictionary apDictionary = widget.getAppearance();
+        
+        String onValue = "";
+        if (apDictionary != null) 
+        {
+            PDAppearanceEntry normalAppearance = apDictionary.getNormalAppearance();
+            if (normalAppearance != null)
+            {
+                Set<COSName> entries = normalAppearance.getSubDictionary().keySet();
+                for (COSName entry : entries)
+                {
+                    // Specifically looking for the string 'Off' as the off value
+                    // Can't use COSName.OFF here as this will be 'OFF' in uppercase
+                    // but for form fields the correct value is 'Off' in mixed case
+                    if ("Off".compareTo(entry.getName()) != 0)
+                    {
+                        onValue = entry.getName();
+                    }
+                }
+            }
+        }
+        return onValue;
+    }
+    
+    /**
+     * Get the values which sets the check box to the On state.
+     * 
+     * <p>This is a convenience function to provide a similar method to 
+     * {@link PDRadioButton} </p>
+     *
+     * @see #getOnValue()
+     * @returns the value setting the check box to the On state. 
+     *          If an empty List is returned there is no appearance definition.
+     * @throws IOException if the value could not be set
+     */
+    public List<String> getOnValues()
+    {
+        String onValue = getOnValue();
+        
+        if (onValue.isEmpty())
+        {
+            return Collections.emptyList();
+        }
+        else
+        {
+            ArrayList<String> onValues = new ArrayList<String>();
+            onValues.add(onValue);
+            return onValues;
+        }
+    }
+    
 }

Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java?rev=1688663&r1=1688662&r2=1688663&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Wed Jul  1 14:54:08 2015
@@ -20,8 +20,12 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 
+import java.io.File;
+import java.io.IOException;
+
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.PDDocument;
+import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -31,14 +35,26 @@ import org.junit.Test;
  */
 public class PDButtonTest
 {
+    
+    
+    private static final File IN_DIR = new File("src/test/resources/org/apache/pdfbox/pdmodel/interactive/form");
+    private static final String NAME_OF_PDF = "AcroFormsBasicFields.pdf";
+    
     private PDDocument document;
     private PDAcroForm acroForm;
 
+    private PDDocument acrobatDocument;
+    private PDAcroForm acrobatAcroForm;
+    
+    
     @Before
-    public void setUp()
+    public void setUp() throws IOException
     {
         document = new PDDocument();
         acroForm = new PDAcroForm(document);
+        
+        acrobatDocument = PDDocument.load(new File(IN_DIR, NAME_OF_PDF));
+        acrobatAcroForm = acrobatDocument.getDocumentCatalog().getAcroForm();
     }
 
     @Test
@@ -73,5 +89,23 @@ public class PDButtonTest
         assertTrue(buttonField.isRadioButton());
         assertFalse(buttonField.isPushButton());
     }
+    
+    @Test
+    public void retrieveAcrobatCheckBoxProperties() throws IOException
+    {
+        PDCheckbox checkbox = (PDCheckbox) acrobatAcroForm.getField("Checkbox");
+        assertEquals(checkbox.getOnValue(), "Yes");
+        assertEquals(checkbox.getOnValues().size(),1);
+        assertEquals(checkbox.getOnValues().get(0), "Yes");
+    }
+    
+    
+    @After
+    public void tearDown() throws IOException
+    {
+        document.close();
+        acrobatDocument.close();
+    }
+    
 }
 



Mime
View raw message