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:06:56 UTC
svn commit: r1883438 - in /pdfbox/trunk/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:06:56 2020
New Revision: 1883438
URL: http://svn.apache.org/viewvc?rev=1883438&view=rev
Log:
PDFBOX-4617: enable getting the selected index
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java?rev=1883438&r1=1883437&r2=1883438&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDRadioButton.java Sun Nov 15 13:06:56 2020
@@ -22,6 +22,8 @@ import java.util.Set;
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.PDAppearanceEntry;
/**
* Radio button fields contain a set of related buttons that can each be on or off.
@@ -81,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/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java?rev=1883438&r1=1883437&r2=1883438&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/TestRadioButtons.java Sun Nov 15 13:06:56 2020
@@ -271,4 +271,61 @@ public class TestRadioButtons
assertTrue("no export values are selected", field.getSelectedExportValues().isEmpty());
}
}
+
+ /**
+ * 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";
+
+ try (PDDocument testPdf = Loader.loadPDF(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());
+ }
+ }
+
+ /**
+ * 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";
+
+ try (PDDocument testPdf = Loader.loadPDF(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());
+ }
+ }
+
+ /**
+ * 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";
+
+ try (PDDocument testPdf = Loader.loadPDF(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());
+ }
+ }
}
\ No newline at end of file