简体   繁体   English

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

[英]set/unset checkbox value with docx4j in MS Word document

I'm trying to set/unset checkbox value with docx4j in MS Word document. 我正在尝试使用MS Word文档中的docx4j设置/取消复选框值。

在此处输入图片说明

Using code from this post: docx4j checking checkboxes I received following XML of this element from my document: 使用本文中的代码: docx4j检查复选框我从文档中收到以下该元素的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>

How could I unset value of this checkbox? 如何取消设置此复选框的值?

Thank You! 谢谢!

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);
        }

}

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. 要取消选中该复选框,您需要更改默认标志,或者将新的子节点<w:checked w:val="false"/><w:checkBox>节点。

Eg for your example the XML for an unchecked checkbox (overriding the default value) would look like this: 例如,对于您的示例, 未选中复选框的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>

With docx4j the code would look as follows: 使用docx4j时,代码如下所示:

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. 第一个问题的答案已经说明如何检索CTFFCheckBox实例。 If you have a FldChar instance you can retrieve the checkbox instance via FldChar#getFfData()#getNameOrEnabledOrCalcOnExit() which returns a list of JAXBElement elements. 如果您有FldChar实例,则可以通过FldChar#getFfData()#getNameOrEnabledOrCalcOnExit()检索复选框实例,该实例返回JAXBElement元素的列表。 One of the JAXBElement elements has a CTFFCheckbox instance in its value ( JAXBElement#getValue ). JAXBElement元素之一的值( JAXBElement#getValue )中有一个CTFFCheckbox实例。

In my work I had to write code to set/unset two kinds of checkboxes in MS Word documents (.docx): CTFFCheckBox and CTSdtCheckbox. 在我的工作中,我不得不编写代码来设置/取消设置MS Word文档(.docx)中的两种复选框:CTFFCheckBox和CTSdtCheckbox。 I ended up using XPath and docx4j to find the checkboxes and change their values. 我最终使用XPath和docx4j查找复选框并更改了它们的值。 Here is the sample code to flip all CTFFCheckBox in a document: 以下是示例代码,用于翻转文档中的所有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());
        }
    }

To flip a CTSdtCheckbox I had to also modify the text symbol representing the checkbox. 要翻转CTSdtCheckbox,我还必须修改代表该复选框的文本符号。 After locating the checkbox with one XPath expression I located the text symbol using another XPath expression relative to the checkbox: 在使用一个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)));
        }       
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM