问题
I'm trying to set/unset checkbox value with docx4j in MS Word document.
Using code from this post: docx4j checking checkboxes I received following XML of this element from my document:
<w:fldChar w:fldCharType="begin" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:ns21="urn:schemas-microsoft-com:office:powerpoint" xmlns:ns23="http://schemas.microsoft.com/office/2006/coverPageProps" xmlns:dsp="http://schemas.microsoft.com/office/drawing/2008/diagram" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:odx="http://opendope.org/xpaths" xmlns:odgm="http://opendope.org/SmartArt/DataHierarchy" xmlns:dgm="http://schemas.openxmlformats.org/drawingml/2006/diagram" xmlns:ns17="urn:schemas-microsoft-com:office:excel" xmlns:c="http://schemas.openxmlformats.org/drawingml/2006/chart" xmlns:odi="http://opendope.org/components" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:ns9="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:ns32="http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:ns30="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns:ns12="http://schemas.openxmlformats.org/drawingml/2006/chartDrawing" xmlns:ns31="http://schemas.openxmlformats.org/drawingml/2006/compatibility" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:odq="http://opendope.org/questions" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xdr="http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing" xmlns:odc="http://opendope.org/conditions" xmlns:oda="http://opendope.org/answers">
<w:ffData>
<w:name w:val=""/>
<w:enabled/>
<w:calcOnExit w:val="false"/>
<w:checkBox>
<w:sizeAuto/>
<w:default w:val="true"/>
</w:checkBox>
</w:ffData>
How could I unset value of this checkbox?
Thank You!
回答1:
You need to find your check box element, and when you have it the rest is trivial. Example.
for (Object o2 : contentControl.getSdtPr().getRPrOrAliasOrLock()) {
o2 = XmlUtils.unwrap(o2);
if (o2 instanceof CTSdtCheckbox) {
CTSdtCheckbox cTSdtCheckbox = (CTSdtCheckbox) o2;
CTOnOff ctOnOff = new CTOnOff();
ctOnOff.setVal("1!);
cTSdtCheckbox.setChecked(ctOnOff);
}
}
回答2:
To unset the checkbox you either need to change the default flag or add a new child node <w:checked w:val="false"/>
to the <w:checkBox>
node.
E.g. for your example the XML for an unchecked checkbox (overriding the default value) would look like this:
<w:fldChar w:fldCharType="begin">
<w:ffData>
<w:name w:val=""/>
<w:enabled/>
<w:calcOnExit w:val="false"/>
<w:checkBox>
<w:sizeAuto/>
<w:default w:val="true"/>
<w:checked w:val="false"/>
</w:checkBox>
</w:ffData>
With docx4j the code would look as follows:
final CTFFCheckBox checkbox = // retrieve your checkbox
final BooleanDefaultTrue booleanFalse = new BooleanDefaultTrue();
booleanFalse.setVal(false);
checkbox.setChecked(booleanFalse); // alternatively call checkbox.setDefault(booleanFalse);
How to retrieve the CTFFCheckBox
instance was already explained in the answer of your first question. If you have a FldChar
instance you can retrieve the checkbox instance via FldChar#getFfData()#getNameOrEnabledOrCalcOnExit()
which returns a list of JAXBElement
elements. One of the JAXBElement
elements has a CTFFCheckbox
instance in its value (JAXBElement#getValue
).
回答3:
In my work I had to write code to set/unset two kinds of checkboxes in MS Word documents (.docx): CTFFCheckBox and CTSdtCheckbox. I ended up using XPath and docx4j to find the checkboxes and change their values. Here is the sample code to flip all CTFFCheckBox in a document:
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(template);
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
List<Object> list = mainDocumentPart.getJAXBNodesViaXPath("//w:checkBox",false);
for (Object c : list) {
JAXBElement<CTFFCheckBox> element = (JAXBElement<CTFFCheckBox>)c;
CTFFCheckBox checkBox = element.getValue();
BooleanDefaultTrue checkedVal = checkBox.getChecked();
BooleanDefaultTrue defaultVal = checkBox.getDefault();
if (checkedVal != null){
checkedVal.setVal(!checkedVal.isVal());
} else {
defaultVal.setVal(!defaultVal.isVal());
}
}
To flip a CTSdtCheckbox I had to also modify the text symbol representing the checkbox. After locating the checkbox with one XPath expression I located the text symbol using another XPath expression relative to the checkbox:
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(template);
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
List<Object> list = mainDocumentPart.getJAXBNodesViaXPath("//w14:checkbox", false);
for (Object c : list) {
JAXBElement<CTSdtCheckbox> element = (JAXBElement<CTSdtCheckbox>)c;
CTSdtCheckbox checkbox = element.getValue();
List<Object> list2 = mainDocumentPart.getJAXBNodesViaXPath("../..//w:t", element, false);
Text chkSymbol = ((JAXBElement<Text>) list2.get(0)).getValue();
CTOnOff checkedVal = checkbox.getChecked();
if (checkedVal.getVal().compareTo("0") == 0) {
checkedVal.setVal("1");
chkSymbol.setValue(new String(Character.toChars(0x2612)));
} else {
checkedVal.setVal("0");
chkSymbol.setValue(new String(Character.toChars(0x2610)));
}
}
来源:https://stackoverflow.com/questions/29540560/set-unset-checkbox-value-with-docx4j-in-ms-word-document