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!