ingomohr / docwriter

API to write docx documents
MIT License
1 stars 0 forks source link

Add Support for Checking/Unchecking Checkboxes in SimpleDocxProcessor #4

Open ingomohr opened 5 years ago

ingomohr commented 5 years ago

Discussed w/ msc

Snippet (for the impl idea)

private void checkCheckBox(MainDocumentPart pMainDocumentPart, int pIndex)
            throws JAXBException, XPathBinderAssociationIsPartialException {

        final CTFFCheckBox checkBox = findCheckBoxWithIndex(pMainDocumentPart, pIndex);
        if (checkBox.getChecked() == null) {
            checkBox.setChecked(new BooleanDefaultTrue());
        }
        final BooleanDefaultTrue checked = checkBox.getChecked();
        checked.setVal(true);
    }

    private CTFFCheckBox findCheckBoxWithIndex(MainDocumentPart pMainDocumentPart, int pIndex)
            throws XPathBinderAssociationIsPartialException, JAXBException {
        final List<Object> checkBoxElements = pMainDocumentPart.getJAXBNodesViaXPath("//w:checkBox", false);
        for (int i = 0; i < checkBoxElements.size(); i++) {
            if (i == pIndex) {
                @SuppressWarnings("unchecked")
                final JAXBElement<CTFFCheckBox> checkBoxElement = (JAXBElement<CTFFCheckBox>) checkBoxElements.get(i);
                return checkBoxElement.getValue();
            }
        }
        throw new IllegalArgumentException(String.format("There is no check box with index %d.", pIndex));
    }