我试图在MS Word文档中使用docx4j设置/取消设置复选框值。

使用本文中的代码:docx4j checking checkboxes,我从我的文档中收到了以下这个元素的XML:
<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>
如何取消此复选框的值?
谢谢!
发布于 2016-03-18 12:46:50
您需要找到您的复选框元素,当您拥有它时,其余的都是微不足道的。举例说明。
for (Object o2 : contentControl.getSdtPr().getRPrOrAliasOrLock()) {
Object o2Unwrapped = XmlUtils.unwrap(o2);
if (o2Unwrapped instanceof CTSdtCheckbox) {
CTSdtCheckbox cTSdtCheckbox = (CTSdtCheckbox) o2Unwrapped;
CTOnOff ctOnOff = new CTOnOff();
ctOnOff.setVal("1");
cTSdtCheckbox.setChecked(ctOnOff);
}
}发布于 2019-03-28 09:41:43
在我的工作中,我不得不编写代码来设置/撤消MS Word文档(.docx)中的两种复选框: CTFFCheckBox和CTSdtCheckbox。最后,我使用XPath和docx4j查找复选框并更改它们的值。下面是翻转文档中所有CTFFCheckBox的示例代码:
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());
}
}要翻转CTSdtCheckbox,我还必须修改表示复选框的文本符号。在使用一个XPath表达式定位复选框之后,我使用相对于复选框的另一个XPath表达式定位文本符号:
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)));
}
}发布于 2017-07-19 11:48:50
若要取消复选框,您需要更改默认标志或将新的子节点<w:checked w:val="false"/>添加到<w:checkBox>节点。
例如,对于您的示例,未选中的XML复选框(覆盖默认值)如下所示:
<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>使用docx4j,代码如下所示:
final CTFFCheckBox checkbox = // retrieve your checkbox
final BooleanDefaultTrue booleanFalse = new BooleanDefaultTrue();
booleanFalse.setVal(false);
checkbox.setChecked(booleanFalse); // alternatively call checkbox.setDefault(booleanFalse);如何检索CTFFCheckBox实例已经解释了您的第一个问题的in the answer。如果您有一个FldChar实例,则可以通过FldChar#getFfData()#getNameOrEnabledOrCalcOnExit()检索复选框实例,后者返回JAXBElement元素的列表。其中一个JAXBElement元素的值为CTFFCheckbox实例(JAXBElement#getValue)。
https://stackoverflow.com/questions/29540560
复制相似问题