Author: lehmi
Date: Sun Oct 14 14:34:18 2012
New Revision: 1398092
URL: http://svn.apache.org/viewvc?rev=1398092&view=rev
Log:
PDFBOX-1374: added handling of combo and edit flag within a acroform combobox
as proposed by Anselm Kruis
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceField.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java?rev=1398092&r1=1398091&r2=1398092&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSName.java Sun
Oct 14 14:34:18 2012
@@ -669,6 +669,11 @@ public final class COSName extends COSBa
/**
* A common COSName value.
*/
+ public static final COSName I = new COSName("I");
+
+ /**
+ * A common COSName value.
+ */
public static final COSName ID = new COSName("ID");
/**
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceField.java
URL:
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceField.java?rev=1398092&r1=1398091&r2=1398092&view=diff
==============================================================================
---
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceField.java
(original)
+++
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDChoiceField.java
Sun Oct 14 14:34:18 2012
@@ -24,8 +24,6 @@ import org.apache.pdfbox.cos.COSDictiona
import org.apache.pdfbox.cos.COSInteger;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
-import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
-import org.apache.pdfbox.pdmodel.interactive.form.PDVariableText;
/**
* A class for handling the PDF field as a choicefield.
@@ -35,6 +33,15 @@ import org.apache.pdfbox.pdmodel.interac
*/
public class PDChoiceField extends PDVariableText
{
+ /**
+ * A Ff flag.
+ */
+ public static final int FLAG_COMBO = 1 << 17;
+
+ /**
+ * A Ff flag.
+ */
+ public static final int FLAG_EDIT = 1 << 18;
/**
* @see
org.apache.pdfbox.pdmodel.interactive.form.PDField#PDField(PDAcroForm,COSDictionary)
@@ -58,8 +65,11 @@ public class PDChoiceField extends PDVar
public void setValue(String optionValue) throws IOException
{
int indexSelected = -1;
- COSArray options = (COSArray)getDictionary().getDictionaryObject(
"Opt" );
- if( options.size() == 0 )
+ COSArray options = (COSArray)getDictionary().getDictionaryObject(
COSName.OPT );
+ int fieldFlags = getFieldFlags();
+ boolean isEditable = (FLAG_COMBO & fieldFlags) != 0 && (FLAG_EDIT &
fieldFlags) != 0;
+
+ if( options.size() == 0 && ! isEditable )
{
throw new IOException( "Error: You cannot set a value for a choice
field if there are no options." );
}
@@ -68,7 +78,8 @@ public class PDChoiceField extends PDVar
// YXJ: Changed the order of the loops. Acrobat produces PDF's
// where sometimes there is 1 string and the rest arrays.
// This code works either way.
- for( int i=0; i<options.size() && indexSelected == -1; i++ ) {
+ for( int i=0; i<options.size() && indexSelected == -1; i++ )
+ {
COSBase option = options.getObject( i );
if( option instanceof COSArray )
{
@@ -80,7 +91,7 @@ public class PDChoiceField extends PDVar
//have the parent draw the appearance stream with the
value
super.setValue( value.getString() );
//but then use the key as the V entry
- getDictionary().setItem( COSName.getPDFName( "V" ),
key );
+ getDictionary().setItem( COSName.V, key );
indexSelected = i;
}
}
@@ -95,13 +106,17 @@ public class PDChoiceField extends PDVar
}
}
}
- if( indexSelected == -1 )
+ if( indexSelected == -1 && isEditable )
+ {
+ super.setValue( optionValue );
+ }
+ else if( indexSelected == -1 )
{
throw new IOException( "Error: '" + optionValue + "' was not an
available option.");
}
else
{
- COSArray indexArray =
(COSArray)getDictionary().getDictionaryObject( "I" );
+ COSArray indexArray =
(COSArray)getDictionary().getDictionaryObject( COSName.I );
if( indexArray != null )
{
indexArray.clear();