Example usage for org.apache.pdfbox.pdmodel.interactive.form PDCheckBox getOnValues

List of usage examples for org.apache.pdfbox.pdmodel.interactive.form PDCheckBox getOnValues

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.interactive.form PDCheckBox getOnValues.

Prototype

public Set<String> getOnValues() 

Source Link

Document

Get the values to set individual buttons within a group to the on state.

Usage

From source file:com.formkiq.core.service.generator.pdfbox.PdfEditorServiceImpl.java

License:Apache License

/**
 * Create {@link FormJSONField} from {@link PDField}.
 *
 * @param pdffield {@link PdfFieldMatch}
 * @param relatedTextMap {@link Map} of {@link PdfTextField}
 * @return {@link FormJSONField}/*from  w  w w.ja v a2s  . c  o  m*/
 */
private FormJSONField createFormField(final PdfFieldMatch pdffield,
        final Map<UUID, List<PdfTextField>> relatedTextMap) {

    FormJSONField field = new FormJSONField();
    field.setRequired(FormJSONRequiredType.OPTIONAL);

    PDField pdfield = pdffield.getField();

    field.setLabel(getLabel(pdffield, relatedTextMap));

    if (pdfield instanceof PDComboBox) {

        PDComboBox box = (PDComboBox) pdfield;

        field.setType(SELECTBOX);
        field.setOptions(box.getOptions());

    } else if (pdfield instanceof PDCheckBox) {

        PDCheckBox c = (PDCheckBox) pdfield;

        field.setOptions(c.getOnValues().stream().map(s -> s + "[" + s + "]").collect(Collectors.toList()));

        if (pdffield.getWidgets().size() == c.getOnValues().size()) {

            List<String> options = field.getOptions();
            for (int i = 0; i < options.size(); i++) {

                PdAnnotationWidgetMatch wm = pdffield.getWidgets().get(i);
                if (!wm.getMatches().isEmpty()) {
                    String v = extractLabelAndValue(options.get(i)).getRight();
                    options.set(i, wm.getMatches().get(0).getTextField().getText().trim() + "[" + v + "]");
                }
            }

        }

        field.setType(SWITCH);

    } else if (pdfield instanceof PDSignatureField) {

        field.setType(SIGNATURE);

    } else {

        field.setType(TEXTBOX);
    }

    return field;
}

From source file:com.formkiq.core.service.generator.pdfbox.PdfEditorServiceImpl.java

License:Apache License

/**
 * Adjust Matches for {@link PDCheckBox} fields.
 *
 * @param pdField {@link PDField}//from w  w w .j a va 2 s  . c  o  m
 * @param match {@link PdfFieldMatch}
 */
private void updatePDCheckBoxMatches(final PDField pdField, final PdfFieldMatch match) {

    if (pdField instanceof PDCheckBox) {

        PDCheckBox c = (PDCheckBox) pdField;

        List<String> onValues = c.getOnValues().stream().map(s -> s.trim().toUpperCase())
                .collect(Collectors.toList());

        if (pdField.getWidgets().size() == onValues.size() && onValues.size() > 1) {

            List<PdfTextFieldMatch> bestmatch = new ArrayList<>();

            for (int i = 0; i < match.getWidgets().size(); i++) {

                PdAnnotationWidgetMatch w = match.getWidgets().get(i);
                String onvalue = onValues.get(i).trim().toUpperCase();

                // check to see if 1st match matches Checkbox Value
                if (!w.getMatches().isEmpty()
                        && (w.getMatches().get(0).getTextField().getText().toUpperCase().startsWith(onvalue))
                        || w.getMatches().get(0).getTextField().getText().toUpperCase().endsWith(onvalue)) {

                    bestmatch.add(w.getMatches().get(0));

                    // check to see if 2nd match matches Checkbox Value
                } else if (w.getMatches().size() > 1
                        && (w.getMatches().get(1).getTextField().getText().toUpperCase().startsWith(onvalue))
                        || w.getMatches().get(1).getTextField().getText().toUpperCase().endsWith(onvalue)) {

                    bestmatch.add(w.getMatches().get(1));
                }
            }

            if (bestmatch.size() == onValues.size()) {

                for (PdfTextFieldMatch bm : bestmatch) {
                    bm.setMatch(0);
                }

                for (PdAnnotationWidgetMatch m : match.getWidgets()) {
                    Collections.sort(m.getMatches(), this.matchComparator);
                }
            }
        }
    }
}