Page 1 of 1

Bind checkbox in docx

PostPosted: Thu Apr 03, 2014 1:03 am
by nicky
Hi :)

again me with a problem ;) A few weeks ago i tried to bind checkboxes in my docx-document. Worked fine...

Yesterday i created a bigger document (6 pages) with many bound fields (texts and checkboxes). I did the bind with docx4j but all my checkboxes have been replaced with "true" or "false" texts :(

So, back to the tests a few weeks ago... they still worked. I created another testdocument and it didn't work there too... after some tests and many trials i have two nearly ident docx-files, the ident xml data file and the ident sourcecode but one time the checkbox bind works fine, another time not.

Here is the source:
Code: Select all
String input_DOCX = "C:/tmp/doctest/checkboxtest.docx";
//String input_DOCX = "C:/tmp/doctest/checkboxtest2.docx";
String input_XML = "C:/tmp/doctest/xmldatawritertest.xml";
// resulting docx
String OUTPUT_DOCX = "C:/tmp/doctest/OUT_test.docx;

// Load input_template.docx
WordprocessingMLPackage wordMLPackage = Docx4J.load(new File(input_DOCX));

// Open the xml stream
FileInputStream xmlStream = new FileInputStream(new File(input_XML));
Docx4J.bind(wordMLPackage, xmlStream, Docx4J.FLAG_BIND_INSERT_XML   | Docx4J.FLAG_BIND_BIND_XML);
// Save the document as docx
Docx4J.save(wordMLPackage, new File(OUTPUT_DOCX), Docx4J.FLAG_NONE);


checkboxtest.docx works fine, checkboxtest2.docx doesn't. The funny thing is, if i click in generated docx of checkboxtest2.docx on the left side of the checkbox MS Word 2010 crashes or switches the text to a checkbox?!

Very strange... any idea? Is there a better way to bind checkboxes?

BR
Nicky

Re: Bind checkbox in docx

PostPosted: Thu Apr 03, 2014 2:15 am
by nicky
Ok, i found out something more... the reason why the first docx works is because the value when i was creating the docx-file was true and when i bind it, it is also true.

I also found a dirty workaround to solve my problem: Just set ASCII-character codes into the xml-datafile and it works too ;)

[☐] U+2610 "& # 9 7 4 4 ;" BALLOT BOX
[☑] U+2611 "& # 9 7 4 5 ;" BALLOT BOX WITH CHECK
[☒] U+2612 "& # 9 7 4 6 ;" BALLOT BOX WITH X

More codes can be found here: http://www.nwmtwd.com/misc_character_sets.html

Then it's just text and no control any more... so no data can be entered into the xml by checking / unchecking the box.

Re: Bind checkbox in docx

PostPosted: Mon Apr 07, 2014 8:25 pm
by jason
It would be good to add support for checkbox, for example:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w14:checkbox>
          <w14:checked w14:val="0"/>
          <w14:checkedState w14:val="2612" w14:font="MS Gothic"/>
          <w14:uncheckedState w14:val="2610" w14:font="MS Gothic"/>
        </w14:checkbox>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


At present that element is ignored, so the behaviour you are seeing is a consequence of the way plain text is bound.