简体   繁体   中英

I can't value fields iText 2.1.7

We have created PDF files via a template and we fill the fields with values from the database. "Name field" = "value". Now we want to read the value of one of the fields in the PDF file we created earlier.

CODE

reader = new PdfReader("C:\\temp\\letter.pdf");

baos = new ByteArrayOutputStream();

stamper = new PdfStamper(reader, baos);

form = stamper.getAcroFields();

System.out.println(form.getField("CUSTOMER-NAME"));

But when I do that returns "null" and it does not recognize any field. It is as if the fields does not exist.

Any help would appreciated. Thanks.

Hi, I have 2 file types. One has been flattened :-( and another one no. For the last file, i am using this code and it work:

PdfReader reader = new PdfReader(FILE);
PdfDictionary root = reader.getCatalog();
PdfDictionary form1 = root.getAsDict(PdfName.ACROFORM);
PdfArray fields = form1.getAsArray(PdfName.FIELDS);

PdfDictionary page;
PdfArray annots;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
    page = reader.getPageN(i);
    annots = page.getAsArray(PdfName.ANNOTS);
    for (int j = 0; j < annots.size(); j++) {
        fields.add(annots.getAsIndirectObject(j));
    }
}

AcroFields form2 = reader.getAcroFields();

Thanks a lot!!! Regards, Muni

As bruno suggested - if you only want to read, only use the PdfReader...

Try the following code and see whether something is printed out:

PdfReader reader = new PdfReader("C:\\temp\\letter.pdf");
AcroFields acroFields = reader.getAcroFields();
Map<String, AcroFields.Item> fields = acroFields.getFields();
Iterator<Entry<String, Item>> it = fields.entrySet().iterator();

while (it.hasNext()) {
    Entry<String, Item> entry = it.next();
    String value = acroFields.getField(entry.getKey());
    System.out.println(entry.getKey()+":"+value);
}

There could be many reasons why your field is not found:

  • the PDF doesn't contain PDF form fields at all (pdf has been flattened)
  • the field name is a different one (eg Customer-Name)
  • you field has a parent (so the name is parent.CUSTOMER-NAME)
  • the form fields were not correctly inserted
  • ...

Can you see the fields in eg adobe reader?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM