Francisco Navarro Morales Francisco Navarro Morales - 2 months ago 20
Java Question

get all acrosfields avalibles in a pdf document itext7

I am trying to automatize the modification of an pdf template according to some data computing (using Java)

I have no experience with pdf modification and I am being trying to use

itext7
to do this.

I have been reading how to add text to a pdf and even here I saw how to field Acrosfield if they exist using a "key"

Nevertheless, I didn't made the pdf template I am using (which is modifiable) so I don't know if the fields with you can manually fill are made with Acrosfields or another tecnology and I don'w know what are the keys or each field If they have one...

I saw this question; where it is says how to get all the fields and their values but when I try the code that appear in the only answer I get;

main.java:[40,0] error: illegal start of type
main.java:[40,19] error: ')' expected
main.java:[40,30] error: <identifier> expected
3 errors


In this part:

for (String fldName : fldNames) {
System.out.println( fldName + ": " + fields.getField( fldName ) );
}


After try a bit, I have been finding more information but I can't find a way to get these "keys" if it's possible...

Answer

If you want a full answer to your question, you will have to provide the PDF so that we can inspect it, but these are already some answers that will get you in the right direction.

When you refer to How to fill out a pdf file programmatically? (AcroForm technology), you refer to the iText 7 version of How to fill out a pdf file programmatically? (AcroForm technology) which is the answer to the same question, but for developers who use iText 5. As you can see, there's a big difference between iText 5 and iText 7. See Forms: iText 5 vs iText 7.

However, when you refer to How do I get all the fields and value from a PDF file using iText? you get an answer that is to be used with iText 5 only. If you are using iText 7, that code won't work because it's iText 5 code.

The code you need can be found here: How to get specific types from AcroFields? Like PushButtonField, RadioCheckField, etc

PdfReader reader = new PdfReader(src);
PdfDocument pdfDoc = new PdfDocument(reader);
// Get the fields from the reader (read-only!!!)
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
// Loop over the fields and get info about them
Set<String> fields = form.getFormFields().keySet();
for (String key : fields) {
    writer.print(key + ": ");
    PdfName type = form.getField(key).getFormType();
    if (0 == PdfName.Btn.compareTo(type)) {
        if(((PdfButtonFormField)form.getField(key)).isPushButton()){
            writer.println("Pushbutton");
        } else {
            if(((PdfButtonFormField)form.getField(key)).isRadio()){
                writer.println("Radiobutton");                   
            }else {
                writer.println("Checkbox");
            }
        }
    } else if (0 == PdfName.Ch.compareTo(type)) {
        writer.println("Choicebox");
    } else if (0 == PdfName.Sig.compareTo(type)) {
        writer.println("Signature");
    } else if (0 == PdfName.Tx.compareTo(type)) {
        writer.println("Text");
    }else {
        writer.println("?");
    }
}

This code will loop over all the fields, and write the key to the System.out, as well as the type of field that corresponds with that key. You can also use RUPS to inspect your PDF.

You mention:

main.java:[40,0] error: illegal start of type
main.java:[40,19] error: ')' expected
main.java:[40,30] error: <identifier> expected

It isn't clear to me if this is a compiler error or a run-time error.

  • If it's a compiler error, you are simply missing a ) somewhere, in which case your problem is totally unrelated to iText (which is what I suspect: you just have a very simple programming error).
  • If it's a run-time error, there is something wrong with your PDF. In PDF, a string is delimited by parentheses. Maybe a bracket is missing somewhere in your PDF (but I doubt that: you'd get a different type of error).

In short: try a good IDE, and that IDE will tell give you an indication of where a parenthesis is missing. If you don't find that location immediately, clean up your code by adding indentation and spaces. That should make it clear where you forget a ).

Comments