How to make a field not required?

I am trying to make a field not required using iText. I know that I can make a field required using something like below statement:

 

formFields.setFieldProperty(field, "setfflags", PdfFormField.FF_REQUIRED, null);
Is there anything similar to make an existing field not required?

Posted on StackOverflow on Dec 1, 2014 by user2296988

Please have a look at the "How to find out which fields are required?" question. It solves the same problem.

Based on your question, I assume that you have an existing PDF where you have required fields. It's really simple to change the FF_REQUIRED flag in iText 7:

public void manipulatePdf(String src, String dest) throws IOException {
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    Map fields = form.getFormFields();
    PdfFormField item;
    for (String key : fields.keySet()) {
        item = fields.get(key);
        if (item.isRequired())
                item.setRequired(false);
    }
    pdfDoc.close();
}

As you see, we get the form fields using getFormFields() method, then loop all the fields and (if necessary) apply setRequired() method with the false parameter.

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



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now