简体   繁体   中英

PDFBox invalid option in radio

When trying to fill the form of this pdf ( http://vaielab.com/Test/2.pdf ) with this code

PDDocument pdfDocument = PDDocument.load(new File("2.pdf"));
pdfDocument.setAllSecurityToBeRemoved(true);
PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
if (acroForm != null) {             
    PDField field = (PDField) acroForm.getField("rad2");
    try {
        field.setValue("0");
    } catch (Exception e) {
        System.out.println(e);
    }
}
pdfDocument.save("output.pdf");
pdfDocument.close();

I get this error: value '0' is not a valid option for the field rad2, valid values are: [Yes] and Off

But value "0" should be a valid option, and if I do a dump_data_fields with pdftk, I get this:

FieldType: Button
FieldName: rad2
FieldFlags: 49152
FieldJustification: Left
FieldStateOption: 0
FieldStateOption: 1
FieldStateOption: Off
FieldStateOption: Yes

I also tried the value "1" but get the exact same error.

I'm using pdfbox 2.0.20

This is because of the Opt values in Root/AcroForm/Fields/[7]/Opt , that one has two "Yes" entries only. The PDButton.setValue() code in PDFBox updates this field differently when /Opt is set. The best here would be not to set it, or remove these entries by calling field.setExportValues(null) . Then valid settings would be 0, 1 and "Off".

PDFDebugger 字段截图

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM