1 31 package org.pdfbox.pdmodel.interactive.form; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSDictionary; 35 import org.pdfbox.cos.COSName; 36 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget; 37 38 import java.io.IOException ; 39 40 import java.util.List ; 41 42 49 public class PDFieldFactory 50 { 51 private static final int RADIO_BITMASK = 32768; 52 private static final int PUSHBUTTON_BITMASK = 65536; 53 private static final int RADIOS_IN_UNISON_BITMASK = 33554432; 54 55 private static final String FIELD_TYPE_BTN = "Btn"; 56 private static final String FIELD_TYPE_TX = "Tx"; 57 private static final String FIELD_TYPE_CH = "Ch"; 58 private static final String FIELD_TYPE_SIG = "Sig"; 59 60 63 private PDFieldFactory() 64 { 65 } 67 68 79 public static PDField createField( PDAcroForm acroForm, COSDictionary field) throws IOException 80 { 81 PDField pdField = new PDUnknownField( acroForm, field ); 82 if( isButton(pdField) ) 83 { 84 int flags = pdField.getFieldFlags(); 85 COSArray kids = (COSArray)field.getDictionaryObject( COSName.getPDFName( "Kids" ) ); 90 if( kids != null || isRadio(flags) ) 91 { 92 pdField = new PDRadioCollection( acroForm, field ); 93 } 94 else if( isPushButton( flags ) ) 95 { 96 pdField = new PDPushButton( acroForm, field ); 97 } 98 else 99 { 100 pdField = new PDCheckbox( acroForm, field ); 101 } 102 103 } 104 else if (isChoiceField(pdField)) 105 { 106 pdField = new PDChoiceField( acroForm, field ); 107 } 108 else if (isTextbox(pdField)) 109 { 110 pdField = new PDTextbox( acroForm, field ); 111 } 112 else if( isSignature( pdField ) ) 113 { 114 pdField = new PDSignature( acroForm, field ); 115 } 116 else 117 { 118 } 120 return pdField; 121 } 122 123 131 private static boolean isRadio( int flags ) 132 { 133 return (flags & RADIO_BITMASK) > 0; 134 } 135 136 144 private static boolean isPushButton( int flags ) 145 { 146 return (flags & PUSHBUTTON_BITMASK) > 0; 147 } 148 149 156 private static boolean isChoiceField(PDField field) throws IOException 157 { 158 return FIELD_TYPE_CH.equals(field.findFieldType()); 159 } 160 161 169 private static boolean isButton(PDField field) throws IOException 170 { 171 String ft = field.findFieldType(); 172 boolean retval = FIELD_TYPE_BTN.equals( ft ); 173 List kids = field.getKids(); 174 if( ft == null && kids != null && kids.size() > 0) 175 { 176 Object obj = kids.get( 0 ); 179 COSDictionary kidDict = null; 180 if( obj instanceof PDField ) 181 { 182 kidDict = ((PDField)obj).getDictionary(); 183 } 184 else if( obj instanceof PDAnnotationWidget ) 185 { 186 kidDict = ((PDAnnotationWidget)obj).getDictionary(); 187 } 188 else 189 { 190 throw new IOException ( "Error:Unexpected type of kids field:" + obj ); 191 } 192 retval = isButton( new PDUnknownField( field.getAcroForm(), kidDict ) ); 193 } 194 return retval; 195 } 196 197 203 private static boolean isSignature(PDField field) throws IOException 204 { 205 return FIELD_TYPE_SIG.equals(field.findFieldType()); 206 } 207 208 214 private static boolean isTextbox(PDField field) throws IOException 215 { 216 return FIELD_TYPE_TX.equals(field.findFieldType()); 217 } 218 } | Popular Tags |