Search code examples
javapdfpdfbox

How to set multiple option for PDListBox in PDFBox?


I am using PDFBox library to set the Values in PDF.

code block through I am trying to set values in ListBox

public class App {
public static void main(String[] args) {
    App app = new App();
    app.test();
}
public void test(){
    try {
        InputStream resource = getClass().getResourceAsStream("/2.pdf");
        PDDocument pdfDocument = PDDocument.load(resource);
        PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        PDListBox listBox = (PDListBox) acroForm.getField("multiple_listbox");
        List<String> list = Arrays.asList("option 2", "option 1");
        System.out.println("Set value in listBox: " + list);
        listBox.setValue(list);
        System.out.println("Get Value from list box: " + listBox.getValue());
        pdfDocument.save("pdfupdate.pdf");
        pdfDocument.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

in result it shows only first value is selected from List<String> in PDF.
check result here:

How can I set multiple value selected in PDF?
PDFBox version : 2.0.6
Source pdf : https://www.dropbox.com/s/5cbicbuwu0kjwlq/2.pdf?dl=0


Solution

  • This is a bug in PDFBox up to 2.0.6, and has been fixed in issue PDFBOX-3828. The fix is in version 2.0.7 which has been released in fall 2017.