2

I'm starting with new functionality in my android application which will help to fill certain PDF forms.

I find out that the best solution will be to use iText library.

I can read file, and read AcroFields from document but is there any possibility to find out that specific field is marked as required?

I tried to find this option in API documentation and on Internet but there were nothing which can help to solve this issue.

weedget
  • 21
  • 5

2 Answers2

0

Please take a look at section 13.3.4 of my book, 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:

for (Map.Entry<String,AcroFields.Item> entry : fields.entrySet()) {
    out.write(entry.getKey());
    item = entry.getValue();
    dict = item.getMerged(0);
    flags = dict.getAsNumber(PdfName.FF);
    if (flags != null && (flags.intValue() & BaseField.REQUIRED) > 0)
        out.write(" -> required\n");
}
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • well thank you! in my case I needed to wrap up getAsNumber to if because for some fields this function returns null, but the rest is working great. – weedget Feb 18 '14 at 11:32
  • Aha, this happens when no flags are set for the field. Thanks for the feedback. I'll update my answer so that you can accept it. – Bruno Lowagie Feb 18 '14 at 11:34
0

check required fields without looping for itext5.

        String src = "yourpath/pdf1.pdf";
        String dest = "yourpath/pdf2.pdf";

        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(this.reader, new FileOutputStream(dest));

        AcroFields form = this.stamper.getAcroFields();
        Map<String,AcroFields.Item> fields = form.getFields();
        AcroFields.Item item;
        PdfDictionary dict;
        PdfNumber flags;
        item=fields.get("fieldName");
        dict = item.getMerged(0);
        flags = dict.getAsNumber(PdfName.FF);
        if (flags != null && (flags.intValue() & BaseField.REQUIRED) > 0) 
        {
                System.out.println("flag  has set");
        }   
Yasitha Bandara
  • 331
  • 1
  • 4
  • 13