How to find out which fields are required?

Tags: formsInspect fieldsfield propertiesiText 7

I'm starting with new functionality in my android application which will help to fill certain PDF forms. I found out that the best solution will be to use iText library. I can read file, and read AcroFields from a document but is there any possibility to find out that specific field is marked as required?

Posted on StackOverflow on Feb 17, 2013 by weedget

Please take a look at section 13.3.4 of "iText in Action - Second Edition", entitled "AcroForms revisited". Listing 13.15 shows a code snippet from the InspectForm example that checks whether or not a field is a password or a multi-line field.

With some minor changes, you can adapt that example to check for required fields:

public void manipulatePdf(String src, String desttxt) throws IOException {
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(desttxt));
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src));
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    Map<String, PdfFormField> fields = form.getFormFields();
    PdfFormField item;
    for (String key : fields.keySet()) {
        item = fields.get(key);
        if (item.isRequired())
            out.write(" -> required");

Click this link if you want to see how to answer this question in iText 5.