Page 1 of 1

Set checkbox values and text values using docx4j

PostPosted: Fri Jul 12, 2013 5:21 am
by hinke
Is it possible to have a MS Word template with components such as Checkboxes/Textfield/Dropdown boxes and set the values for each one of those components?

I would like to tag each component in a Word document and then be able to find it and set the value using docx4j.

Example xml generated by MS Word:

Code: Select all
<w:p w:rsidR="006E5C2E" w:rsidRDefault="008F3BB7" w:rsidP="00685763">
         <w:sdt>
            <w:sdtPr>
               <w:tag w:val="checkbox.two" />
               <w:id w:val="-1510903451" />
               <w14:checkbox>
                  <w14:checked w14:val="1" />
                  <w14:checkedState w14:val="2612" w14:font="MS Gothic" />
                  <w14:uncheckedState w14:val="2610" w14:font="MS Gothic" />
               </w14:checkbox>
            </w:sdtPr>
            <w:sdtEndPr />
            <w:sdtContent>
               <w:r w:rsidR="00046DC2">
                  <w:rPr>
                     <w:rFonts w:ascii="MS Gothic" w:eastAsia="MS Gothic" w:hAnsi="MS Gothic" w:hint="eastAsia" />
                  </w:rPr>
                  <w:t>☒</w:t>
               </w:r>
            </w:sdtContent>
         </w:sdt>
         <w:r w:rsidR="0054722E">
            <w:t xml:space="preserve"> </w:t>
         </w:r>
         <w:r w:rsidR="00685763">
            <w:t>Checkbox Two</w:t>
         </w:r>
      </w:p>

Re: Set checkbox values and text values using docx4j

PostPosted: Fri Jul 12, 2013 7:05 am
by jason
Yes, it is possible (in that the checkbox is accessible via docx4j's JAXB object model), but at present you'll need to write the code to find/set those values yourself.

Some references:

The issue tracking support for Word 2010 content control extensions:- https://github.com/plutext/docx4j/issues/54

Support in SdtPr for checkbox:- https://github.com/plutext/docx4j/blob/ ... SdtPr.java at line 160

The checkbox object itself:- https://github.com/plutext/docx4j/blob/ ... ckbox.java

docx4j's code for resolving bindings (which currently ignores Word 2010 content control extensions eg checkbox):- https://github.com/plutext/docx4j/tree/ ... atastorage

Code which accesses the checkbox property https://github.com/plutext/docx4j/blob/ ... elper.java at 190