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 2020/11/15 13:16:06 UTC

svn commit: r1883439 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java

Author: msahyoun
Date: Sun Nov 15 13:16:06 2020
New Revision: 1883439

URL: http://svn.apache.org/viewvc?rev=1883439&view=rev
Log:
PDFBOX-4617: enable getting the selected index

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

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java?rev=1883439&r1=1883438&r2=1883439&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java Sun Nov 15 13:16:06 2020
@@ -23,6 +23,7 @@ import java.util.Set;
 
 import org.apache.pdfbox.cos.COSDictionary;
 import org.apache.pdfbox.cos.COSName;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
 
 /**
  * Radio button fields contain a set of related buttons that can each be on or off.
@@ -82,6 +83,31 @@ public final class PDRadioButton extends
     }
 
     /**
+     * This will get the selected index.
+     * <p>
+     * A RadioButton might have multiple same value options which are not selected jointly if
+     * they are not set in unison {@link #isRadiosInUnison()}.</p>
+     * 
+     * <p>
+     * The method will return the first selected index or -1 if no option is selected.</p>
+     * 
+     * @return the first selected index or -1.
+     */
+    public int getSelectedIndex()
+    {
+        int idx = 0;
+        for (PDAnnotationWidget widget : getWidgets())
+        {
+            if (!COSName.Off.equals(widget.getAppearanceState()))
+            {
+                return idx;
+            }
+            idx ++;
+        }
+        return -1;
+    }
+
+    /**
      * This will get the selected export values.
      * <p>
      * A RadioButton might have an export value to allow field values

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java?rev=1883439&r1=1883438&r2=1883439&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java Sun Nov 15 13:16:06 2020
@@ -40,7 +40,6 @@ import org.apache.pdfbox.pdmodel.interac
 
 import org.junit.Test;
 
-
 /**
  * This will test the functionality of Radio Buttons in PDFBox.
  */
@@ -308,5 +307,80 @@ public class TestRadioButtons
         {
             IOUtils.closeQuietly(testPdf);
         }
-   }
+    }
+
+        /**
+     * PDFBOX-4617 Enable getting selected index
+     * 
+     * @throws IOException
+     */
+    @Test
+    public void testPDFBox4617IndexNoneSelected() throws IOException
+    {
+        String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12848122/SF1199AEG%20%28Complete%29.pdf";
+
+        PDDocument testPdf = null;
+        try
+        {
+            testPdf = PDDocument.load(new URL(sourceUrl).openStream());
+            PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
+            PDRadioButton field = (PDRadioButton) acroForm.getField("Checking/Savings");
+            assertEquals("if there is no value set the index shall be -1", -1, field.getSelectedIndex());
+        }
+        finally
+        {
+            IOUtils.closeQuietly(testPdf);
+        }
+    }
+
+    /**
+     * PDFBOX-4617 Enable getting selected index for value being set by option
+     * 
+     * @throws IOException
+     */
+    @Test
+    public void testPDFBox4617IndexForSetByOption() throws IOException
+    {
+        String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12848122/SF1199AEG%20%28Complete%29.pdf";
+
+        PDDocument testPdf = null;
+        try
+        {
+            testPdf = PDDocument.load(new URL(sourceUrl).openStream());
+            PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
+            PDRadioButton field = (PDRadioButton) acroForm.getField("Checking/Savings");
+            field.setValue( "Checking");
+            assertEquals("the index shall be equal with the first entry of Checking which is 0", 0, field.getSelectedIndex());
+        }
+        finally
+        {
+            IOUtils.closeQuietly(testPdf);
+        }
+    }
+
+    /**
+     * PDFBOX-4617 Enable getting selected index for value being set by index
+     * 
+     * @throws IOException
+     */
+    @Test
+    public void testPDFBox4617IndexForSetByIndex() throws IOException
+    {
+        String sourceUrl = "https://issues.apache.org/jira/secure/attachment/12848122/SF1199AEG%20%28Complete%29.pdf";
+
+        PDDocument testPdf = null;
+        try
+        {
+            testPdf = PDDocument.load(new URL(sourceUrl).openStream());
+            PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
+            PDRadioButton field = (PDRadioButton) acroForm.getField("Checking/Savings");
+            field.setValue(4);
+            assertEquals("setting by the index value should return the corresponding export", "Checking", field.getValue());
+            assertEquals("the index shall be equals with the set value of 4", 4, field.getSelectedIndex());
+        }
+        finally
+        {
+            IOUtils.closeQuietly(testPdf);
+        }
+    }
 }
\ No newline at end of file