pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1783283 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Date Thu, 16 Feb 2017 20:59:36 GMT
Author: msahyoun
Date: Thu Feb 16 20:59:36 2017
New Revision: 1783283

URL: http://svn.apache.org/viewvc?rev=1783283&view=rev
Log:
PDFBOX-3682: for radio button with /Opt entry use name for value even it's not numeric

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java?rev=1783283&r1=1783282&r2=1783283&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDButton.java
Thu Feb 16 20:59:36 2017
@@ -26,7 +26,7 @@ import org.apache.pdfbox.pdmodel.common.
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
-import java.util.HashSet;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Set;
 
@@ -299,7 +299,7 @@ public abstract class PDButton extends P
     public Set<String> getOnValues()
     {
         // we need a set as the field can appear multiple times
-        Set<String> onValues = new HashSet<String>();
+        Set<String> onValues = new LinkedHashSet<String>();
         
         if (getExportValues().size() > 0)
         {
@@ -383,21 +383,14 @@ public abstract class PDButton extends P
         {
             // the value is the index of the matching option
             int optionsIndex = options.indexOf(value);
-            getCOSObject().setName(COSName.V, String.valueOf(optionsIndex));
-    
-            
-            // update the appearance state (AS)
-            for (int i = 0; i < widgets.size(); i++)
+
+            // get the values the options are pointing to as
+            // this might not be numerical
+            // see PDFBOX-3682
+            if (optionsIndex != -1)
             {
-                PDAnnotationWidget widget = widgets.get(i);
-                if (value.compareTo(options.get(i)) == 0)
-                {
-                    widget.getCOSObject().setName(COSName.AS, String.valueOf(i));
-                }
-                else 
-                {
-                    widget.getCOSObject().setItem(COSName.AS, COSName.Off);
-                }
+                String[] onValues = getOnValues().toArray(new String[getOnValues().size()]);
+                updateByValue(onValues[optionsIndex]);
             }
         }
     }

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java?rev=1783283&r1=1783282&r2=1783283&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
(original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDButtonTest.java
Thu Feb 16 20:59:36 2017
@@ -145,7 +145,63 @@ public class PDButtonTest
             }
         }
     }
+
+    @Test
+    /**
+     * PDFBOX-3682
+     * 
+     * Test a radio button with options.
+     * Special handling for a radio button with /Opt and the On state not being named
+     * after the index.
+     * 
+     * @throws IOException
+     */
+    public void testOptionsAndNamesNotNumbers()
+    {
+        URL url;
+        PDDocument pdfDocument = null;
         
+        try
+        {
+            url = new URL("https://issues.apache.org/jira/secure/attachment/12852207/test.pdf");
+            InputStream is = url.openStream();
+            
+            pdfDocument = PDDocument.load(is);
+            
+            pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton").setValue("c");
+            PDRadioButton radioButton = (PDRadioButton) pdfDocument.getDocumentCatalog().getAcroForm().getField("RadioButton");
+            radioButton.setValue("c");
+
+            // test that the old behavior is now invalid
+            assertFalse("This shall no longer be 2", "2".equals(radioButton.getValueAsString()));
+            assertFalse("This shall no longer be 2", "2".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
+            
+            // test for the correct behavior
+            assertTrue("This shall be c", "c".equals(radioButton.getValueAsString()));
+            assertTrue("This shall be c", "c".equals(radioButton.getWidgets().get(2).getCOSObject().getNameAsString(COSName.AS)));
+            
+            
+        }
+        catch (IOException e)
+        {
+            fail("Unexpected IOException " + e.getMessage());
+        }
+        finally
+        {
+            if (pdfDocument != null)
+            {
+                try
+                {
+                    pdfDocument.close();
+                }
+                catch (IOException e)
+                {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+    
     @Test
     public void retrieveAcrobatCheckBoxProperties() throws IOException
     {



Mime
View raw message