Write value to RadioButtonField

I want to write a value to a radiobuttonfield. I tried it like using a TextBoxField. But when i try to call setValue() i get an NPE.

Here a Code Snippet:

private PdfFileResult execSetFormFields(PdfFileInput pdfFileInput, FormFieldsInput formFieldsInput) {
    File targetFile = pdfFileInput.getPdfFlowFile().getFile();
    FlowFileInfo targetFlowFileInfo = new FlowFileInfo();
    targetFlowFileInfo.setFileExtension(PDF_FILE_EXTENSION);

    try (Document pdfDocument = new Document(targetFile.getPath())) {
        for (PdfFormFieldWithSettings fieldWithSettings : formFieldsInput.getFormFieldsWithSettings()) {
            var targetFormField = pdfDocument.getForm().get(fieldWithSettings.getFieldName());
            processFormField(targetFormField, fieldWithSettings);
        }
        pdfDocument.save(targetFile.getPath());
        return new PdfFileResult(FlowFile.of(targetFlowFileInfo, targetFile));
    }
}


private void processFormField(WidgetAnnotation targetFormField, PdfFormFieldWithSettings fieldWithSettings) {
    if (targetFormField != null && fieldWithSettings != null) {
        PdfFormFieldSettings settings = fieldWithSettings.getPdfFormFieldSettings();

        if (targetFormField instanceof TextBoxField){
            ((TextBoxField) targetFormField).setValue(fieldWithSettings.getValue());
        }
        else if(targetFormField instanceof CheckboxField){
            ((CheckboxField) targetFormField).setValue(fieldWithSettings.getValue());
        }
        else if(targetFormField instanceof ComboBoxField){
            ((ComboBoxField) targetFormField).setValue(fieldWithSettings.getValue());
        }
        else if(targetFormField instanceof ChoiceField){
            ((ChoiceField) targetFormField).setValue(fieldWithSettings.getValue());
        }
        else if(targetFormField instanceof PasswordBoxField){
            ((PasswordBoxField) targetFormField).setValue(fieldWithSettings.getValue());
        }
        else if(targetFormField instanceof RadioButtonField){
            ((RadioButtonField) targetFormField).setValue(fieldWithSettings.getValue());
        }

        if (fieldWithSettings.isSetFieldSettings()) {
            targetFormField.setReadOnly(settings.getSetFieldReadOnly());
            targetFormField.setRequired(settings.getSetFieldRequired());
        }
    }
}

How to proper write to a radiobuttonfield? And is this casting explicitly needed?
Here my Exception:

60 more Caused by: java.lang.NullPointerException: Cannot invoke “com.aspose.pdf.RadioButtonOptionField.updateAppearances()” because “” is null at com.aspose.pdf.RadioButtonField.updateAppearances(Unknown Source) at com.aspose.pdf.Field.le(Unknown Source) at com.aspose.pdf.RadioButtonField.setSelected(Unknown Source)

Thanks!

@mlaemmle

Would you kindly share the sample PDF for our reference as well? We will test the scenario in our environment and log an investigation ticket for further analysis in our issue management system.

Hi,

i just send you a message via PM.

@mlaemmle

Yes, we received the sample file and tried to test the case using the code snippet you shared. We were unable to execute it due to some undefined and missing objects. Can you please share a narrowed down version of the code snippet for our reference so that we can use it to test the case? It would be helpful to address the case accordingly.

@asad.ali

surely this code snippet does not work on its own, pdfFileInput and formFieldsInput has to be provided or mocked by you. what is important is method processFormField(). i don’t know what to provide for you that it is working on your machine :wink:

@mlaemmle

We were able to replicate the issue by using below minimal code snippet:

Document doc = new Document(dataDir + "Aspose_DEMO.pdf");
com.aspose.pdf.WidgetAnnotation targetFormField = doc.getForm().get("GRP_HR_PERSONNEL_SEVERELY_DISABLED");
((RadioButtonField) targetFormField).setValue("ja");

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFJAVA-43722

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Is there also another approach to set RadioButtons? Maybe another method?

@mlaemmle

Another approach is using Facades API.

Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form(fileNameTempPDF);
pdfForm.FillField("Beruf_copy1", "test");