Screen reader is not reading the fillable form fields created using PDF BOX

Viewed 22

I can able to create the fillable form fields using PDF Box in the desired format but when I try to run the file through NVDA or JAWS screen reader.

It's not reading the newly created form fields.

Is there any specific property values available to make fields to be reading out through screen reader.

I had included my source code below for the reference.

for (PDPage page : pages) {
        int pageIndex = pages.indexOf(page);
        PageStructElementDetail _pgstrele = _ElementsByPageUtility._ElementsByPage.get(pageIndex);
        int pageHeight = ((Float) _pgstrele.getPageHeight()).intValue();
        // Add a new AcroForm and add that to the document
        PDAcroForm acroForm = new PDAcroForm(_pdfDocumentObj);
        _pdfDocumentObj.getDocumentCatalog().setAcroForm(acroForm);
        Set<String> processedCheckBoxSet = new HashSet<String>();
        for (StructElementDetail element : _pgstrele.getStructElementDetails()) {
            try {
                if ((element.getElementType().startsWith("TB"))) {
                    System.out.println("Inside TB");
                    if (element.getIsFillableFormField()) {

                        List<Integer> _int_bbox_list = convertBBOXtoIntegerList(element.getBBOX());
                        Integer xVal = _int_bbox_list.get(0);
                        Integer yVal = pageHeight - _int_bbox_list.get(1);
                        Integer width = _int_bbox_list.get(2) - _int_bbox_list.get(0);
                        Integer height = _int_bbox_list.get(1) - _int_bbox_list.get(3);
                        System.out.println(" _int_bbox_list Values [ " + _int_bbox_list.toString() + " ] ");
                        System.out.println("Print axis Values [ xVal : " + xVal + "] [ yVal : " + yVal
                                + " ] [ width : " + width + " ] [ height : " + height + " ] ");
                        String formFieldName = null, toolTip = null, fontFamily = null;
                        long maxLength = 10, fontSize = 9;
                        Boolean required = false, multiLine = false, checkSpelling = false, combofChar = false,
                                scrollLongText = false, readOnly = false;
                        JSONArray formFieldProperties = (JSONArray) element.getFormFieldProperties();
                        for (Object eleObj : formFieldProperties) {
                            formFieldName = (String) ((JSONObject) eleObj).get("FormFieldName");
                            toolTip = (String) ((JSONObject) eleObj).get("ToolTips");
                            fontSize = (long) ((JSONObject) eleObj).get("FontSize");
                            fontFamily = (String) ((JSONObject) eleObj).get("FontFamily");
                            maxLength = (long) ((JSONObject) eleObj).get("MaxLength");
                            required = (Boolean) ((JSONObject) eleObj).get("Required");
                            multiLine = (Boolean) ((JSONObject) eleObj).get("MultiLine");
                            checkSpelling = (Boolean) ((JSONObject) eleObj).get("CheckSpelling");
                            scrollLongText = (Boolean) ((JSONObject) eleObj).get("ScrollLongText");
                            readOnly = (Boolean) ((JSONObject) eleObj).get("ReadOnly");
                            combofChar = (Boolean) ((JSONObject) eleObj).get("CombofChar");
                        }
                        addTBField(acroForm, page, formFieldName, xVal, yVal, width, height, toolTip, fontSize,
                                fontFamily, maxLength, required, multiLine, checkSpelling, scrollLongText, readOnly,
                                combofChar);
                    }
                }  else if ((element.getElementType().startsWith("SIG"))) {
                    System.out.println("Inside SIG");
                    List<Integer> _int_bbox_list = convertBBOXtoIntegerList(element.getBBOX());
                    Integer xVal = _int_bbox_list.get(0);
                    Integer yVal = pageHeight - _int_bbox_list.get(1);
                    Integer width = _int_bbox_list.get(2) - _int_bbox_list.get(0);
                    Integer height = _int_bbox_list.get(1) - _int_bbox_list.get(3);
                    String formFieldName = null, toolTip = null, fontFamily = null;
                    long fontSize = 9;
                    Boolean required = false, readOnly = false;
                    JSONArray formFieldProperties = (JSONArray) element.getFormFieldProperties();
                    for (Object eleObj : formFieldProperties) {
                        formFieldName = (String) ((JSONObject) eleObj).get("FormFieldName");
                        toolTip = (String) ((JSONObject) eleObj).get("ToolTips");
                        fontSize = (long) ((JSONObject) eleObj).get("FontSize");
                        fontFamily = (String) ((JSONObject) eleObj).get("FontFamily");
                        required = (Boolean) ((JSONObject) eleObj).get("Required");
                        readOnly = (Boolean) ((JSONObject) eleObj).get("ReadOnly");
                    }

                    addSignatureField(acroForm, page, formFieldName, xVal, yVal, width, height, toolTip, fontSize,
                            fontFamily, required, readOnly);
                }
            } catch (Exception e) {
                System.out.println("Error in Form Field");
                e.printStackTrace();
            }
        }
    }
}

// Generate Form Text fields
private static void addTBField(PDAcroForm acroForm, PDPage page, String name, float x, float y, float width,
        float height, String toolTip, long fontSize, String fontFamily, long maxLength, Boolean required,
        Boolean multiLine, Boolean checkSpelling, Boolean scrollLongText, Boolean readOnly, Boolean combofChar) {

    try {
        // Add a form field to the form.
        PDTextField textBox = new PDTextField(acroForm);
        textBox.setPartialName(name);
        acroForm.getFields().add(textBox);

        // Specify the widget annotation associated with the field
        PDAnnotationWidget widget = textBox.getWidgets().get(0);
        PDRectangle rect = new PDRectangle(x, y, width, height);
        widget.setRectangle(rect);
        widget.setPage(page);

        // set black border
        // if you prefer defaults, just delete this code block
        PDAppearanceCharacteristicsDictionary fieldAppearance = new PDAppearanceCharacteristicsDictionary(
                new COSDictionary());
        /*
         * if (combofChar) { fieldAppearance.setBorderColour(new PDColor(new float[] {
         * 0, 0, 0 }, PDDeviceRGB.INSTANCE)); }
         */
        if (checkSpelling) {
            checkSpelling = false;
        } else {
            checkSpelling = true;
        }
        if (scrollLongText) {
            scrollLongText = false;
        } else {
            scrollLongText = true;
        }

        widget.setAppearanceCharacteristics(fieldAppearance);
        widget.setPrinted(true);

        // Add the widget annotation to the page
        page.getAnnotations().add(widget);

        String defaultAppearanceString = "/" + fontFamily + " " + fontSize + " Tf 0 0 0 rg";
        textBox.setDefaultAppearance(defaultAppearanceString);

        // set the field values
        textBox.setRequired(required);
        textBox.setAlternateFieldName(toolTip);
        textBox.setComb(combofChar);
        textBox.setDoNotSpellCheck(checkSpelling);
        textBox.setDoNotScroll(scrollLongText);
        textBox.setMaxLen((int) maxLength);
        textBox.setMultiline(multiLine);

    } catch (IOException e) {
        e.printStackTrace();
    }

}
0 Answers
Related