Author: msahyoun
Date: Tue Mar 24 20:10:31 2026
New Revision: 1932515
Log:
PDFBOX-6150: use display value for appearance
Modified:
pdfbox/branches/3.0/pdfbox/pom.xml
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
Modified: pdfbox/branches/3.0/pdfbox/pom.xml
==============================================================================
--- pdfbox/branches/3.0/pdfbox/pom.xml Tue Mar 24 20:10:04 2026
(r1932514)
+++ pdfbox/branches/3.0/pdfbox/pom.xml Tue Mar 24 20:10:31 2026
(r1932515)
@@ -902,6 +902,19 @@
</configuration>
</execution>
<execution>
+ <id>PDFBOX-6150</id>
+ <phase>generate-test-resources</phase>
+ <goals>
+ <goal>wget</goal>
+ </goals>
+ <configuration>
+
<url>https://issues.apache.org/jira/secure/attachment/13080392/test-form.pdf</url>
+
<outputDirectory>${project.build.directory}/pdfs</outputDirectory>
+ <outputFileName>PDFBOX-6150.pdf</outputFileName>
+
<sha512>da6c630d2683937bce60f70302176f16531b36300b69d2d8585c15819b7965bf6c0c2f6fe101743d66b3dbb70e7eb0383614b669878641a480b9ea1a6d02510f</sha512>
+ </configuration>
+ </execution>
+ <execution>
<id>PDFBOX-6178</id>
<phase>generate-test-resources</phase>
<goals>
Modified:
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
==============================================================================
---
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
Tue Mar 24 20:10:04 2026 (r1932514)
+++
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoice.java
Tue Mar 24 20:10:31 2026 (r1932515)
@@ -212,6 +212,18 @@ public abstract class PDChoice extends P
}
/**
+ * This will check if the field has dedicated display and export values.
+ *
+ * @return true if export and display values are different
+ */
+ public boolean hasSeparateExportAndDisplayValues()
+ {
+ List<String> exportValues = getOptionsExportValues();
+ List<String> displayValues = getOptionsDisplayValues();
+ return !exportValues.equals(displayValues);
+ }
+
+ /**
* This will get the indices of the selected options - the 'I' key.
* <p>
* This is only needed if a choice field allows multiple selections and
Modified:
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java
==============================================================================
---
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java
Tue Mar 24 20:10:04 2026 (r1932514)
+++
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDComboBox.java
Tue Mar 24 20:10:31 2026 (r1932515)
@@ -81,9 +81,19 @@ public final class PDComboBox extends PD
AppearanceGeneratorHelper apHelper;
apHelper = new AppearanceGeneratorHelper(this);
List<String> values = getValue();
-
+
if (!values.isEmpty())
{
+ if (hasSeparateExportAndDisplayValues())
+ {
+ List<String> displayValues = getOptionsDisplayValues();
+ int index = getOptions().indexOf(values.get(0));
+ if (index != -1 && index < displayValues.size())
+ {
+ apHelper.setAppearanceValue(displayValues.get(index));
+ return;
+ }
+ }
apHelper.setAppearanceValue(values.get(0));
}
else
Modified:
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java
==============================================================================
---
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java
Tue Mar 24 20:10:04 2026 (r1932514)
+++
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/ControlCharacterTest.java
Tue Mar 24 20:10:31 2026 (r1932515)
@@ -22,14 +22,10 @@ import static org.junit.jupiter.api.Asse
import java.io.File;
import java.io.IOException;
import java.util.List;
-import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.pdfbox.Loader;
-import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdfparser.PDFStreamParser;
-import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -94,7 +90,7 @@ class ControlCharacterTest
PDField field = acroForm.getField("pdfbox-tab");
field.setValue("TAB\tTAB");
- List<String> pdfboxValues = getStringsFromStream(field);
+ List<String> pdfboxValues = TestUtils.getStringsFromStream(field);
pdfboxValues.forEach(token -> assertEquals("TAB", token));
}
@@ -117,8 +113,8 @@ class ControlCharacterTest
PDField field = acroForm.getField("pdfbox-" + nameSuffix);
field.setValue(value);
- List<String> pdfboxValues = getStringsFromStream(field);
- List<String> acrobatValues =
getStringsFromStream(acroForm.getField("acrobat-" + nameSuffix));
+ List<String> pdfboxValues = TestUtils.getStringsFromStream(field);
+ List<String> acrobatValues =
TestUtils.getStringsFromStream(acroForm.getField("acrobat-" + nameSuffix));
assertEquals(pdfboxValues, acrobatValues);
}
@@ -128,22 +124,4 @@ class ControlCharacterTest
{
document.close();
}
-
- private List<String> getStringsFromStream(PDField field) throws IOException
- {
- PDAnnotationWidget widget = field.getWidgets().get(0);
- PDFStreamParser parser = new
PDFStreamParser(widget.getNormalAppearanceStream());
-
- List<Object> tokens = parser.parse();
-
- // TODO: improve the string output to better match
- // trimming as Acrobat adds spaces to strings
- // where we don't
- return tokens.stream() //
- .filter(COSString.class::isInstance) //
- .map(COSString.class::cast) //
- .map(COSString::getString) //
- .map(String::trim) //
- .collect(Collectors.toList());
- }
}
Modified:
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
==============================================================================
---
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
Tue Mar 24 20:10:04 2026 (r1932514)
+++
pdfbox/branches/3.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceTest.java
Tue Mar 24 20:10:31 2026 (r1932515)
@@ -20,9 +20,12 @@ import static org.junit.jupiter.api.Asse
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import org.apache.pdfbox.Loader;
import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
@@ -143,5 +146,39 @@ class PDChoiceTest
assertEquals(options, choiceField.getOptions());
}
+ /*
+ * Set here the value of a choice field to a value with a display value
that is different
+ * from the export value and check that the correct display value is used
in the appearance stream.
+ * See PDFBOX-6150
+ */
+ @Test
+ void PDFBox6150() throws IOException
+ {
+ File pdfFile = new File("target/pdfs/PDFBOX-6150.pdf");
+
+ if (!pdfFile.exists())
+ {
+ return; // Skip test if PDF not available
+ }
+
+ // Load document, set value, and save to memory
+ try (PDDocument document = Loader.loadPDF(pdfFile))
+ {
+ PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
+ PDChoice field = (PDChoice) acroForm.getField("shipping_country");
+
+ field.setValue("DE");
+
+ assertTrue("DE".equals(field.getValue().get(0)), "The fields value
should be set to DE");
+
+ // Read the content of the normal appearance stream and check that
it contains the display value for DE
+ // which is Deutschland
+ List<String> content = TestUtils.getStringsFromStream(field);
+ boolean hasContent =
content.stream().anyMatch("Deutschland"::equals);
+ assertTrue(hasContent, "The content should contain the display
value for DE which is Deutschland");
+
+ document.close();
+ }
+ }
}