Search in sources :

Example 1 with PDListBox

use of com.tom_roush.pdfbox.pdmodel.interactive.form.PDListBox in project PdfBox-Android by TomRoush.

the class MainActivity method fillForm.

/**
 * Fills in a PDF form and saves the result
 */
public void fillForm(View v) {
    try {
        // Load the document and get the AcroForm
        PDDocument document = PDDocument.load(assetManager.open("FormTest.pdf"));
        PDDocumentCatalog docCatalog = document.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();
        // Fill the text field
        PDTextField field = (PDTextField) acroForm.getField("TextField");
        field.setValue("Filled Text Field");
        // Optional: don't allow this field to be edited
        field.setReadOnly(true);
        PDField checkbox = acroForm.getField("Checkbox");
        ((PDCheckBox) checkbox).check();
        PDField radio = acroForm.getField("Radio");
        ((PDRadioButton) radio).setValue("Second");
        PDField listbox = acroForm.getField("ListBox");
        List<Integer> listValues = new ArrayList<>();
        listValues.add(1);
        listValues.add(2);
        ((PDListBox) listbox).setSelectedOptionsIndex(listValues);
        PDField dropdown = acroForm.getField("Dropdown");
        ((PDComboBox) dropdown).setValue("Hello");
        String path = root.getAbsolutePath() + "/FilledForm.pdf";
        tv.setText("Saved filled form to " + path);
        document.save(path);
        document.close();
    } catch (IOException e) {
        Log.e("PdfBox-Android-Sample", "Exception thrown while filling form fields", e);
    }
}
Also used : PDField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDField) PDCheckBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckBox) ArrayList(java.util.ArrayList) PDComboBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDComboBox) PDAcroForm(com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm) IOException(java.io.IOException) PDDocumentCatalog(com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog) PDRadioButton(com.tom_roush.pdfbox.pdmodel.interactive.form.PDRadioButton) PDDocument(com.tom_roush.pdfbox.pdmodel.PDDocument) PDTextField(com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField) PDListBox(com.tom_roush.pdfbox.pdmodel.interactive.form.PDListBox)

Aggregations

PDDocument (com.tom_roush.pdfbox.pdmodel.PDDocument)1 PDDocumentCatalog (com.tom_roush.pdfbox.pdmodel.PDDocumentCatalog)1 PDAcroForm (com.tom_roush.pdfbox.pdmodel.interactive.form.PDAcroForm)1 PDCheckBox (com.tom_roush.pdfbox.pdmodel.interactive.form.PDCheckBox)1 PDComboBox (com.tom_roush.pdfbox.pdmodel.interactive.form.PDComboBox)1 PDField (com.tom_roush.pdfbox.pdmodel.interactive.form.PDField)1 PDListBox (com.tom_roush.pdfbox.pdmodel.interactive.form.PDListBox)1 PDRadioButton (com.tom_roush.pdfbox.pdmodel.interactive.form.PDRadioButton)1 PDTextField (com.tom_roush.pdfbox.pdmodel.interactive.form.PDTextField)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1