52
52
import com .openhtmltopdf .util .ArrayUtil ;
53
53
import com .openhtmltopdf .util .OpenUtil ;
54
54
import com .openhtmltopdf .util .XRLog ;
55
+ import org .w3c .dom .NamedNodeMap ;
56
+ import org .w3c .dom .Node ;
55
57
56
58
57
59
public class PdfBoxForm {
@@ -263,6 +265,10 @@ private String getTextareaText(Element e) {
263
265
264
266
private String populateOptions (Element e , List <String > labels , List <String > values , List <Integer > selectedIndices ) {
265
267
List <Element > opts = DOMUtil .getChildren (e , "option" );
268
+ if (opts == null ) {
269
+ XRLog .general (Level .WARNING , "A <" +e .getTagName () + "> element does not have <option> children" );
270
+ return "" ;
271
+ }
266
272
String selected = "" ;
267
273
int i = 0 ;
268
274
@@ -297,10 +303,8 @@ private String populateOptions(Element e, List<String> labels, List<String> valu
297
303
private void processMultiSelectControl (ControlFontPair pair , Control ctrl , PDAcroForm acro , int i , Box root ) throws IOException {
298
304
PDListBox field = new PDListBox (acro );
299
305
300
- Field fObj = allFieldMap .get (ctrl .box .getElement ().getAttribute ("name" ));
301
- fObj .field = field ;
302
-
303
- field .setPartialName (fObj .partialName );
306
+ setPartialNameToField (ctrl , field );
307
+
304
308
field .setMultiSelect (true );
305
309
306
310
List <String > labels = new ArrayList <String >();
@@ -346,11 +350,8 @@ private void processMultiSelectControl(ControlFontPair pair, Control ctrl, PDAcr
346
350
*/
347
351
private void processSelectControl (ControlFontPair pair , Control ctrl , PDAcroForm acro , int i , Box root ) throws IOException {
348
352
PDComboBox field = new PDComboBox (acro );
349
-
350
- Field fObj = allFieldMap .get (ctrl .box .getElement ().getAttribute ("name" ));
351
- fObj .field = field ;
352
-
353
- field .setPartialName (fObj .partialName );
353
+
354
+ setPartialNameToField (ctrl , field );
354
355
355
356
List <String > labels = new ArrayList <String >();
356
357
List <String > values = new ArrayList <String >();
@@ -397,11 +398,8 @@ private void processSelectControl(ControlFontPair pair, Control ctrl, PDAcroForm
397
398
398
399
private void processHiddenControl (ControlFontPair pair , Control ctrl , PDAcroForm acro , int i , Box root ) throws IOException {
399
400
PDTextField field = new PDTextField (acro );
400
-
401
- Field fObj = allFieldMap .get (ctrl .box .getElement ().getAttribute ("name" ));
402
- fObj .field = field ;
403
-
404
- field .setPartialName (fObj .partialName );
401
+
402
+ setPartialNameToField (ctrl , field );
405
403
406
404
String value = ctrl .box .getElement ().getAttribute ("value" );
407
405
@@ -418,11 +416,8 @@ private void processHiddenControl(ControlFontPair pair, Control ctrl, PDAcroForm
418
416
419
417
private void processTextControl (ControlFontPair pair , Control ctrl , PDAcroForm acro , int i , Box root ) throws IOException {
420
418
PDTextField field = new PDTextField (acro );
421
-
422
- Field fObj = allFieldMap .get (ctrl .box .getElement ().getAttribute ("name" ));
423
- fObj .field = field ;
424
-
425
- field .setPartialName (fObj .partialName );
419
+
420
+ setPartialNameToField (ctrl , field );
426
421
427
422
FSColor color = ctrl .box .getStyle ().getColor ();
428
423
String colorOperator = getColorOperator (color );
@@ -575,12 +570,9 @@ private COSString getCOSStringUTF16Encoded(String value) {
575
570
576
571
private void processCheckboxControl (ControlFontPair pair , PDAcroForm acro , int i , Control ctrl , Box root ) throws IOException {
577
572
PDCheckBox field = new PDCheckBox (acro );
578
-
579
- Field fObj = allFieldMap .get (ctrl .box .getElement ().getAttribute ("name" ));
580
- fObj .field = field ;
581
-
582
- field .setPartialName (fObj .partialName );
583
-
573
+
574
+ setPartialNameToField (ctrl , field );
575
+
584
576
if (ctrl .box .getElement ().hasAttribute ("required" )) {
585
577
field .setRequired (true );
586
578
}
@@ -640,11 +632,9 @@ private void processCheckboxControl(ControlFontPair pair, PDAcroForm acro, int i
640
632
private void processRadioButtonGroup (List <Control > group , PDAcroForm acro , int i , Box root ) throws IOException {
641
633
String groupName = group .get (0 ).box .getElement ().getAttribute ("name" );
642
634
PDRadioButton field = new PDRadioButton (acro );
643
-
635
+
644
636
Field fObj = allFieldMap .get (groupName );
645
- fObj .field = field ;
646
-
647
- field .setPartialName (fObj .partialName );
637
+ setPartialNameToField (group .get (0 ).box .getElement (), fObj , field );
648
638
649
639
List <String > values = new ArrayList <String >(group .size ());
650
640
for (Control ctrl : group ) {
@@ -767,6 +757,28 @@ private void processSubmitControl(PDAcroForm acro, int i, Control ctrl, Box root
767
757
acro .getFields ().add (btn );
768
758
ctrl .page .getAnnotations ().add (widget );
769
759
}
760
+
761
+ private void setPartialNameToField (Control ctrl , PDField field ) {
762
+ Element elem = ctrl .box .getElement ();
763
+ Field fObj = allFieldMap .get (elem .getAttribute ("name" ));
764
+ setPartialNameToField (elem , fObj , field );
765
+ }
766
+
767
+ private static void setPartialNameToField (Element element , Field fObj , PDField field ) {
768
+ if (fObj != null ) {
769
+ fObj .field = field ;
770
+ field .setPartialName (fObj .partialName );
771
+ } else {
772
+ StringBuilder sb = new StringBuilder ();
773
+ NamedNodeMap attributes = element .getAttributes ();
774
+ int length = attributes .getLength ();
775
+ for (int i = 0 ; i < length ; i ++) {
776
+ Node item = attributes .item (i );
777
+ sb .append (' ' ).append (item .getNodeName ()).append ("=\" " ).append (item .getNodeValue ()).append ('"' );
778
+ }
779
+ XRLog .general (Level .WARNING , "found a <" + element .getTagName () + sb .toString () +"> element without attribute name, the element will not work without this attribute" );
780
+ }
781
+ }
770
782
771
783
public int process (PDAcroForm acro , int startId , Box root ) throws IOException {
772
784
processControlNames ();
@@ -806,7 +818,7 @@ public int process(PDAcroForm acro, int startId, Box root) throws IOException {
806
818
e .getAttribute ("type" ).equals ("hidden" )) {
807
819
808
820
processHiddenControl (pair , ctrl , acro , i , root );
809
- }else if (e .getNodeName ().equals ("input" ) &&
821
+ } else if (e .getNodeName ().equals ("input" ) &&
810
822
e .getAttribute ("type" ).equals ("radio" )) {
811
823
// We have to do radio button groups in one hit so add them to a map of list keyed on name.
812
824
List <Control > radioGroup = radioGroups .get (e .getAttribute ("name" ));
0 commit comments